D
D
Dennis2016-01-13 17:50:42
Django
Dennis, 2016-01-13 17:50:42

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 %}

URLs.py:
url(r'^$', 'interior_app.views.home', name='home'),
url(r'^subscribe/$', 'interior_app.views.subscribe', name='subscribe')

models.py:
class Subscriber(models.Model):
    email = models.EmailField('', max_length=100, null=True, blank=True)

forms.py:
class SubscriberForm(forms.ModelForm):
  class Meta:
    model = Subscriber
    fields = ['email']

admin.py:
class SubscriberAdmin(admin.ModelAdmin):
    list_display = ('email',)

admin.site.register(Subscriber, SubscriberAdmin)

views.py:
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)

I enter an email into the form, I press the button and nothing happens.
There is nothing in the admin panel, no success is displayed.
Thanks!!!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav Sklyar, 2016-01-13
@VladSkliar

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 question

Ask a Question

731 491 924 answers to any question