I’m a Django newbie.
I’d like to restrict user to use a specific domain (e.g. @gmail.com ) to sign up my Django website, but how to customize the EmailField in my registration form to do that?
FYI, here’s my codes.
forms.py
class RegistrationForm(ModelForm):
username = forms.CharField(label=(u'User Name'))
email = forms.EmailField(label = (u'Email Adress'))
class Meta:
model = UserProfile
exclude = ('user',)
register.html
{% extends "base.html" %}
{% block content %}
<form action = "" method ="post">
{% csrf_token%}
{% if form.errors %} <p>Please correct the following fields</p> {% endif %}
<div class ="register_div">
{% if form.username.errors %}<p class="error">{{ form.username.errors }}</p>{% endif %}
<p><label for="username"{% if form.username.errors %} class="error"{% endif %}>Username:</label></p>
<p>{{ form.username }}</p>
</div>
<div class ="register_div">
{% if form.email.errors %}<p class = "error">{{ form.email.errors }}</p>{% endif %}
<p><label for ="email"{% if form.email.errors %} class="error"{% endif %}>Email:<label><p>
<p>{{ form.email }}</p>
</div>
<p><input type="submit" alt="register" /></p>
</form>
{% endblock %}
You can write your own clean functions for the form:
More at: https://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-a-specific-field-attribute