Q:

How to display form errors in modal box(context_processors, base.html)?

I have register function, it is displayed on every pages in my project using context_processors.
How to display errors if my form is not valid in my modal box in base.html?
Now errors is displayed in: /register/ subpage.
views.py
def UserRegistration(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/showcase/')
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            user = User.objects.create_user(username=form.cleaned_data['username'], email= form.cleaned_data['email'], password = form.cleaned_data['password'])
            user.save()
            klient = ClientProfile(user=user, name= form.cleaned_data['name'], address= form.cleaned_data['address'], zip_code=form.cleaned_data['zip_code'], city=form.cleaned_data['city'], tel= form.cleaned_data['tel'] )
            klient.save()
            return HttpResponseRedirect('/')

        return render_to_response('registration.html', {'form':form}, context_instance=RequestContext(request)) 

    else:
        form = RegistrationForm()
        context= {'form':form}
        return render_to_response('registration.html', context, context_instance=RequestContext(request))

context_processors.py
from content.forms import *

def include_register_form(request):
    form = RegistrationForm()
    return {'register_form':form}

base.html
<div class="modalBox" id="modalRegister">
   <div class="modalBox_iks"></div>
   <div class="titleShowcase">Register</div>
   <form method="POST" action="/register/">{%csrf_token%}
{{register_form}}
<input type="submit" value="Register"> 
</form>
</div> 

It is possible?

A:

what version of django are you using? If you are in django 1.5, use FormView.
view.py
class UserRegistration(FormView):
    template_name = 'form_registration.html'
    def render_to_response(self, context):

        if self.request.user.is_authenticated():
            return redirect('other_page')
        return super(UserRegistration, self).render_to_response(context)

    def form_valid(self, form):
        # Here you know that you form is valid
        user = User.objects.create_user(username=form.cleaned_data['username'], email=form.cleaned_data['email'], password = form.cleaned_data['password'])
        user.save()
        klient = ClientProfile(user=user, name= form.cleaned_data['name'], address= form.cleaned_data['address'], zip_code=form.cleaned_data['zip_code'], city=form.cleaned_data['city'], tel= form.cleaned_data['tel'] )
        klient.save()
        return redirect('home') #I'm not sure that this really redirect you

Now write a 'form_registration.html' template where you show the errors. see here
And in your 'base.html'
<div class="modalBox" id="modalRegister">
   <div class="modalBox_iks"></div>
   <div class="titleShowcase">Register</div>
   <form method="POST" id="ajax_form"action="/register/">{%csrf_token%}
    {% include "form_registration.html" with form=form %}
    <input type="submit" id="ajax_form_submit"value="Register"> 
    </form>
</div> 

Now, for your ajax, you can use this jquery plug-in.
You can set the 'target' option to override with the server response
Maybe that javascript help you:
<script>
    $('#modalRegister').click("#ajax_form_submit", function(event){
        event.preventDefault();
        $('#ajax_form').ajax_form(target:'#ajax_form').submit();
    })
</script>

I've used that plugin, it works fine.
hope that help!

