0

On the one page i have some content and several forms, so my view looks like this:

def mainpageview(request):
  args = {}
  callbackform = CallBackForm
  mailform = Mail
  args.update(csrf(request))
  args['mainpage'] = Main.objects.get(pk=1)
  args['callbackform'] = callbackform
  args['mailform'] = mailform 
  return render (request, 'main/index.html', args)

def mailform(request):
  if request.POST:
    mailform = Mail(request.POST)
      if mailform.is_valid():
        mail = mailform.cleaned_data['email']
        message = "Email: " + mail
        send_mail(...)
        return HttpResponseRedirect('/callbackresult/')
      else:
        mailform = Mail()
        return HttpResponseRedirect('/', {'mailform':mailform})
    else:
      mailform = Mail(request.POST)
    return render(request, 'main/index.html', {mailform:mailform})

def callback(request):
  if request.POST:
    callbackform = CallBackForm(request.POST)
      if callbackform.is_valid():
        name = callbackform.cleaned_data['name']
        phone = callbackform.cleaned_data['phone']
        message = "Name: " + name + "Phone: " + phone
        send_mail(...)
        return HttpResponseRedirect('/callbackresult/')
      else:
        return HttpResponseRedirect('/')
  else:
    return HttpResponseRedirect('/')

This is how looks my forms:

class CallBackForm(forms.Form):
  name = forms.CharField()
  phone = forms.CharField()

class Mail(forms.Form):
  email = forms.EmailField()

I add action parameter for every form, so this is example of my template:

<form action="/mail/" method="POST">
   {% csrf_token %}
   {{error.mailform}}
   {{ mailform }}
   <input type="submit" class="success button" value="Subscribe">
</form>

Everything works fine, but i can't get how to write form view to show form error({{error.mailform}}). Hope you can help me with it.

3 Answers 3

1

You are creating a new form when the form is not valid. The new form contains no data for the current request so of course will not have any errors.

Try this:

if request.POST:
    mailform = Mail(request.POST)
    if mailform.is_valid():
        mail = mailform.cleaned_data['email']
        message = "Email: " + mail
        send_mail(...)
        return HttpResponseRedirect('/callbackresult/')
    else:
        return render(request, 'template.html', {'mailform':mailform})
else:
    mailform = Mail()
    return render(request, 'template.html', {'mailform':mailform})

Its also better to use render() when returning the form with errors since you are not actually redirecting the user to a different page

Sign up to request clarification or add additional context in comments.

Comments

0

I used this way to display error in the frontend. When error you need to pass the form object into template.

mailroom = Mail(request.POST)
if mailroom.is_valid():
    # save here.
else:
    return render(request, 'main/index.html', {'mailroom': mailroom})

and in template {{ mailform.errors }} will give you the errors.

Comments

0

django.contrib provide message to these problems.

from django.contrib import messages
# then in your form view, take callback form for example
callbackform = CallBackForm(request.POST)
    if callbackform.is_valid():
        messages.success(request,"success")
    else:
        messages.error(request,"error")
# and in the template
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}

by default, django.contrib.messages is in your 'INSTALLED_APPS', if not, you need to check it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.