6

This is my view:

if request.method == 'POST':
        form = TeacherRegister(request.POST)
        #Gets school object from email domain.
        email = form['email'].value().split('@')[1]
        try:
            school = School.objects.get(email_domain = email)
        except ObjectDoesNotExist:
            #add custom error message here

        if form.is_valid():
            user, Teacher = form.save()
            Teacher.school = school
            Teacher.save()
            user.groups.add(Group.objects.get(name='Teacher'))
            #user.is_active to stop users logging in without confirming their emails

            user.is_active = False
            user.save()
            #Sends confirmation link.
            send_confirmation_email(request, user)

            args = {'email': user.email,
                    'link': user.Teacher.school.email_website}

            return render(request, 'email/token_sent.html', args)

        else:
            args = {'form': form,}
            return render(request, 'users/teachers.html', args)

These lines are what I am trying to work with:

email = form['email'].value().split('@')[1]
try:
    school = School.objects.get(email_domain = email)
except ObjectDoesNotExist:
    #add custom error message here

This is the HTML I have for the email field:

<div class="required field" >
            {{ form.email.label }}
            {{ form.email }}
            {{ form.email.help_text }}
           <!-- <label>Email</label>
           <input placeholder="Email" type="email" name="email" autofocus="" required="" id="id_email"> -->
        </div>

How can I get it to say, if no school object is returned, something along the lines of 'School not found, check your email'?

Thanks

2 Answers 2

8

You need to perform validation on the form side. Implement clean_email method in the form:

def clean_email(self):
    email = self.cleaned_data.get('email')
    email = email.split('@')[1] 
    try:
       school = School.objects.get(email_domain = email)
    except ObjectDoesNotExist:
       raise forms.ValidationError(''School not found, check your email')
    return email

Now in template you can show this error right after email field:

 {{ form.email.label }}
 {{ form.email }}
 {{ form.email.errrors }}
 {{ form.email.help_text }}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this sorted it out!
3

You can try Django's built-in messages framework. Try this:

try:
    school = School.objects.get(email_domain = email)
except ObjectDoesNotExist:
    messages.error(request, 'School not found, check your email')

And then somewhere above your form, add this:

{% if messages %}
    {% for message in messages %}
        <div class="alert {% if message.tags %} alert-{{ message.tags }}{% endif %}">{{ message|safe }}</div>
    {% endfor %}
{% endif %}

Hope it helps!

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.