[Answer]-Django contact form doesn't show up

0πŸ‘

βœ…

After two days, I finally got the solution. The error was in the urls.py file. (So next time I will include urls.py as well) Thanks for helping guys!

I was using a TemplateView:

url(r'^$', TemplateView.as_view(template_name='contact/contact-form.html'), name="contact-form"),

Instead of adding the view I created so that it would be possible to show and process the form.

url(r'^$', 'contact.views.contact', name="contact"),
πŸ‘€manosim

1πŸ‘

Its not good practice to use template_name inside url.
try with this :

url(r'^/contact$','appname.views.contact',name='contact')
πŸ‘€hizbul25

0πŸ‘

This should work..

from django.core.context_processors import csrf

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            email = form.cleaned_data['email']
            message = form.cleaned_data['message']

            recipients = ['myemail@outlook.com']

            send_mail(name, email, message, recipients)
            return HttpResponseRedirect(reverse('contact-thanks'))
    else:
        form = ContactForm()

    args = {'form' : form}
    args.update(csrf(request))
    return render(request, 'contact/contact-form.html', args)
πŸ‘€Anish Shah

Leave a comment