0

With out calling register.html template. Placed that code in base.html. How to do validation in base.html using django? How can i acheive this. Please help me.

If i call register.html template validation is working fine. i want to do achieve in base.html.

base.html

<form method="post" action="{% url 'register' %}"> {% csrf_token %}
            <table border="0"> 
                <label for="name">Name:</label>
              <input type="text" id="username" name="username" value="">
              {{ form.errors.name }}
               <label for="mail">Email:</label>
              <input type="text" id="email" name="email">
              {{ form.errors.email }}
              <label for="password1">Password:</label>
              <input type="password" id="password1" name="password1">
              {{ form.errors.password1 }}

              <label for="password2">Password:</label>
              <input type="password" id="password2" name="password2">
              {{ form.errors.password1 }}
            </table>
        <button type="submit" value="Register">Register</button>
        <button type="button" onclick="window.location.href='/' ">Login</button>
    </form>

forms.py

class RegistrationForm(forms.Form):

    username = forms.RegexField(regex=r'^\w+$', widget=forms.TextInput(attrs=dict(required=True, max_length=30)), label=_("Username"), error_messages={ 'invalid': _("This value must contain only letters, numbers and underscores.") })
    email = forms.EmailField(widget=forms.TextInput(attrs=dict(required=True, max_length=30)), label=_("Email address"))
    password1 = forms.CharField(widget=forms.PasswordInput(attrs=dict(required=True, max_length=30, render_value=False)), label=_("Password"))
    password2 = forms.CharField(widget=forms.PasswordInput(attrs=dict(required=True, max_length=30, render_value=False)), label=_("Password (again)"))

    def clean_username(self):
        try:
            user = User.objects.get(username__iexact=self.cleaned_data['username'])
        except User.DoesNotExist:
            return self.cleaned_data['username']
        raise forms.ValidationError(_("The username already exists. Please try another one."))

    def clean(self):
        if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
            if self.cleaned_data['password1'] != self.cleaned_data['password2']:
                raise forms.ValidationError(_("The two password fields did not match."))
        return self.cleaned_data
Venkatesh Panabaka
  • 2,064
  • 4
  • 19
  • 27
  • "register.html" and "base.html" are not _pages_, they are _templates_. You dont handle requests in templates, you handle them in views. Once you understand this, you should be able to either answer the question by yourself or at least re-word it in a meaningfull way. – bruno desthuilliers Feb 03 '17 at 12:46
  • sorry i modified my question. can you help me how to do that. i am in starting stage in django. – Venkatesh Panabaka Feb 03 '17 at 12:49
  • You still don't understand. You don't "call" templates (you render them), and that's not where validation happens. Please re-read my previous comment. – bruno desthuilliers Feb 03 '17 at 12:53
  • Can you include views.py in your question? – Blake Gibbs Feb 03 '17 at 15:08

0 Answers0