如何在模态框中显示表单错误(context_processors,base.html)?

时间:2013-06-10 11:31:33

标签: django

我有注册功能,它使用context_processors显示在项目的每个页面上。

如果我的表单在base.html的模态框中无效,如何显示错误? 现在错误显示在:/register/子页面。

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> 

有可能吗?

2 个答案:

答案 0 :(得分:2)

您需要使用Ajax调用发布数据,显示模态中的错误需要Ajax调用。 或者您可以使用third party app启用Ajax表单验证

答案 1 :(得分:1)

你正在使用什么版本的django?如果你在django 1.5中,请使用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

现在写一个'form_registration.html'模板,显示错误。见here

在你的'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> 

现在,对于您的ajax,您可以使用this jquery plug-in。 您可以将“target”选项设置为使用服务器响应覆盖

也许javascript可以帮助您:

<script>
    $('#modalRegister').click("#ajax_form_submit", function(event){
        event.preventDefault();
        $('#ajax_form').ajax_form(target:'#ajax_form').submit();
    })
</script>

我使用过该插件,效果很好。

希望有所帮助!