On Mon, Mar 11, 2013 at 1:28 PM, sri <[email protected]> wrote:
> Hi,
>
> I am relatively new to Django and trying to create a Django application
> using 1.5 and created a pop up modal login and registration form using
> twitter bootstrap. The example i used to create the drop down
> login/registration form
> ishttp://mifsud.me/adding-dropdown-login-form-bootstraps-navbar/[1] .Now,
> the problem i am having is that, how do i show any error messages (password
> invalid etc) on the pop up login/registration drop down modal box.
>
> Here is my base.html file:
>
>             <ul class="nav nav-pills">
>           <li class="dropdown">
>             <a class="btn btn-success dropdown-toggle" href="#"
> data-toggle="dropdown"><i class="icon-user"></i>Login</a>
>             <div class="dropdown-menu" style="padding:15px; left:-100px;
> padding-bottom:0px">
>                 <form action="/user/login/?next={{request.path}}"
> method="post" class="form-horizontal">{% csrf_token %}
>                     <input id="id_email" required placeholder="Email"
> style="margin-bottom: 15px;" type="text" name="email" size="30" />
>                     <input id="id_password" required placeholder="Password"
> style="margin-bottom: 15px;" type="password" name="password" size="30" />
>                     <input class="btn btn-primary" style="clear: left;
> width: 100%; height: 32px; font-size: 13px;" type="submit" name="commit"
> value="Sign In" />
>                 </form>
>             </div>
>           </li>
>           <li class="dropdown">
>             <a class="btn btn-success dropdown-toggle" href="#"
> data-toggle="dropdown">Register</a>
>             <div class="dropdown-menu" style="padding:15px; left:-150px;
> padding-bottom:0px">
>                 <form action="/user/register/?next={{request.path}}"
> method="post" class="form-horizontal" name="register_form">{% csrf_token %}
>                     {{ register_form.non_field_errors }}
>                     <input id="id_email" required placeholder="Email"
> style="margin-bottom: 15px;" type="text" name="email" size="30" />
>                     <input id="id_password" required placeholder="Password"
> style="margin-bottom: 15px;" type="password" name="password1" size="30" />
>                     <input id="id_password_confirm" required
> placeholder="Password Again" style="margin-bottom: 15px;" type="password"
> name="password2" size="30" />
>                     <input class="btn btn-primary" style="clear: left;
> width: 100%; height: 32px; font-size: 13px;" type="submit" name="commit"
> value="Register" />
>                 </form>
>             </div>
>           </li>
>
> And my views.py looks like below for login and registration views:
>
> def login_view(request):
>   if request.method == 'POST':
>      username = request.POST['email']
>      password = request.POST['password']
>      user = authenticate(username=username, password=password)
>      if user is not None and user.is_active:
>         login(request, user)
>         return HttpResponseRedirect(request.GET.get("next"))
>   else:
>     return  HttpResponseRedirect(reverse('homepage'))
>
> def register_view(request):
>    if request.method == 'POST':
>       form = UserCreationForm(data=request.POST, files=request.FILES)
>       if form.is_valid():
>         new_user = form.save()
>         new_user = authenticate(username=request.POST['username'],
>                                 password=request.POST['password1'])
>         login(request, new_user)
>      return HttpResponseRedirect(request.GET.get("next"))
>    else:
>       return HttpResponseRedirect(reverse('homepage'))
>
> def homepage(request):
>    form = UserCreationForm()
>    return render_to_response('base.html',
>         {
>          'register_form': form},
>        context_instance=RequestContext(request))
>
> And my forms.py looks like below for registration:
>
>  class UserCreationForm(forms.ModelForm):
>
>    password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
>    password2 = forms.CharField(label='Password confirmation',
> widget=forms.PasswordInput)
>
>    class Meta:
>     model = MyUser
>     fields = ('email',)
>
>    def clean_password2(self):
>     # Check that the two password entries match
>     password1 = self.cleaned_data.get("password1")
>     password2 = self.cleaned_data.get("password2")
>     if password1 and password2 and password1 != password2:
>         raise forms.ValidationError("Passwords don't match")
>     return password2
>
>    def save(self, commit=True):
>     # Save the provided password in hashed format
>     user = super(UserCreationForm, self).save(commit)
>     user.set_password(self.cleaned_data["password1"])
>     if commit:
>         user.save()
>     return user
>
> Can anyone help me how i can pass the registration validation error messages
> into the drop down modal form and also the login password validation error
> messages. What i would like to do is displat the error message in the drop
> down login/registration box itself.
>
> Thanks
>

Hi Sri

Django's form objects have individual field objects, accessible in the
template. These field objects have attributes for any validation error
messages, and the form itself has an attribute for any non field
related validation error messages.

Normally, Django would output these for you when you output the form.
However, you've hard-coded the HTML for the form, which makes it
impossible for Django to output the related error messages.

This page shows how forms are normally output:

https://docs.djangoproject.com/en/1.5/topics/forms/#displaying-a-form-using-a-template

And this section of the same page outlines what you must do if you
choose not to allow Django to do most of it for you:

https://docs.djangoproject.com/en/1.5/topics/forms/#customizing-the-form-template

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to