Answer the question
In order to leave comments, you need to log in
Subscription form not working (Django)?
Good afternoon!
home.html:
<form class="subscribe-form" action="{% url 'subscribe' %}" method="POST">{% csrf_token %}
{{ form }}
<button class="btn btn-main btn-lg" type="submit">Подписаться!</button>
</form>
{% if success %}
<div class="subscribe-result">
{{ success }}
</div>
{% endif %}
url(r'^$', 'interior_app.views.home', name='home'),
url(r'^subscribe/$', 'interior_app.views.subscribe', name='subscribe')
class Subscriber(models.Model):
email = models.EmailField('', max_length=100, null=True, blank=True)
class SubscriberForm(forms.ModelForm):
class Meta:
model = Subscriber
fields = ['email']
class SubscriberAdmin(admin.ModelAdmin):
list_display = ('email',)
admin.site.register(Subscriber, SubscriberAdmin)
def home(request):
portfolios = PortfolioObject.objects.all()
photos = []
for portfolio in portfolios:
for obj in portfolio.photo_set.all():
photos.append(obj)
form = SubscriberForm()
context = {"photos": photos[::2], "form": form}
return render(request, "home.html", context)
def subscribe(request):
print request
success = ''
if request.method == "POST":
print request.POST
form = SubscriberForm(request.POST)
print form
if form.is_valid():
form.save()
success = "Ваш Email успешно отправлен"
form = SubscriberForm()
else:
form = SubscriberForm()
context = {"photos": photos[::2], "form": form, "success": success}
return render(request, "home.html", context)
Answer the question
In order to leave comments, you need to log in
Check out the subcribe view. You have photos passed in the context, but you didn't create photos[::2] . Either create or remove. How do you start a project? If .manage.py runserver check the console, there might be an error, and check if the post request is coming. In general, try to do it not through the button but through the input.
Here is an example from the documentation
<form action="/your-name/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question