Form error messages not displayed

45 views
Skip to first unread message

sarfaraz ahmed

unread,
Jul 16, 2014, 5:26:35 PM7/16/14
to django...@googlegroups.com
I am trying to display form.erros. I am using bootstrap modal and using ajax form submit to send data async. Everything works fine... I can see the error messages returned in code. But it automatically goes to default error pages of django when form.is_valid returns false. When I print form in debug mode I see the specific error. I am using Werkzeug  for debugging. 

Here is my view.py code

def register_user(request):
    #args=UserCreationForm()
   
    args={}
    args.update(csrf(request))
    
    if request.method=='POST':   
        form = UserCreationForm(request.POST)
        print 'step 2'
        args['form']=form
        if form.is_valid:
            form.save()
            print 'step 3'
            return HttpResponseRedirect('/accounts/register_success/')
        else:

            return render_to_response('register.html',args,context_instance=RequestContext(request))
           
            #assert false
            #return render_to_response('register.html',args,context_instance=RequestContext(request))
    else:
        args['form'] = UserCreationForm()
    return render_to_response('register.html',args,context_instance=RequestContext(request))
    

======================================================================================
template code
<form id="signup" class="form-horizontal"  action='/accounts/register/' method='post' role='form'>{%csrf_token%}
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title">Register</h4>
</div>           
    <div class="modal-body" id="modalbody">
        {%if form.errors %}
        <p>{{errors}}</p>
        {%endif%}
        <div class="form-group">
            <p>{{form.username.errors.as_text}}</p>
            <label for="username" class="col-sm-3 control-label">Choose Username</label>
            <div class="col-sm-5">
                <input type="text" name="username" class="form-control" value="" id="username" placeholder="Choose Username">
            </div>
        </div>
        
        <div class="form-group">
            <p>{{form.password1.errors.as_text }}</p>
            <label for="password1" class="col-sm-3 control-label">Choose Password</label>
            <div class="col-sm-5">
  
                <input type="password" name="password1" class="form-control" value="" id="password1" placeholder="Choose Password">

            </div>
        </div>
        
        <div class="form-group">
            <p>{{form.password2.errors.as_text }}</p>
            <label for="password2" class="col-sm-3 control-label">Re-Enter Password</label>
            <div class="col-sm-5">
                <input type="password" name="password2" class="form-control" value="" id="password2" placeholder="Re-Enter Password">
            </div>
        </div>
     
    </div>
<div class="modal-footer">
    <div class="form-group">
        <div class="col-lg-4">
            <div class="pull-left">
                <input type ="submit"  value="Register" class="btn btn-success btn-lg">
            </div>
        </div>
    </div>
</div>
</form> 

<script>
 
       $(document).ready(function() { 
           // bind 'myForm' and provide a simple callback function
   
        var form_options = {                     
                            target: '#modalcontent'
    
                          ,success: function() {
                               
                                                }                    
                    
                            
                            ,type: 'post'
                            //,clearForm: true
                            } 
        $('#signup').ajaxForm(form_options);
       
        });
  
 
       
</script> 

Please help

Tom Evans

unread,
Jul 16, 2014, 6:26:29 PM7/16/14
to django...@googlegroups.com
<p>{{ form.errors }}</p>

> {%endif%}


Cheers

Tom

sarfaraz ahmed

unread,
Jul 17, 2014, 12:44:08 AM7/17/14
to django...@googlegroups.com, teva...@googlemail.com
I tried {{form.errors}} in template.. but it still does not show up. When I used the Werkeuq and print form it see the precise error in form. Not sure what i am doing wrong.

Regards,
Sarfaraz Ahmed
 
Reply all
Reply to author
Forward
0 new messages