Django - Ajax注册 [英] Django - Ajax registration

查看:136
本文介绍了Django - Ajax注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图允许注册(使用这个 django-registration register 视图)到一个模式对话框中的一个应用程序。

I am trying to allow registration (using this django-registration register view) to one of my applications from a modal dialog.

由于这个表单是在一个模式框中,我想获得成功的json响应(而不是默认重定向)。

Since this form is in a modal box, I'd like to get an json reponse on success (instead of the default redirection)

我如何使用这个视图( django-registration register )来管理注册并发送一个json响应成功?

How can I use this view (django-registration register) to manage the registration and send back a json response on success ?

我知道如何使ajax / json响应,问题是如何使用没有重定向行为的django注册视图或将其包装到另一个视图来管理响应。

I know how to make ajax/json responses, the question is how to use the django-registration view without the redirection behavior or wrap it into an other view to manage the response.

推荐答案

首先,您需要更改urls.py来包装现有的视图另一个功能。要做到这一点,您必须在后端文件夹中创建一个新的后端程序包,并在保持一切不变的情况下更改urls.py,或者您可以继续修改后端程序包中现有的urls.py。

First you need to change the urls.py to wrap the existing view with another functionality. To do that you have to create a new backend package in backends folder and change urls.py there while keeping everything else intact, or you could just go ahead and modify the existing urls.py in the backend package.

我没有测试过,但它应该可以工作。

I have not tested this, but it should work.

点击URL到新视图:

# urls.py
url(r'^register/$', register_wrap,
    {'backend': 'registration.backends.default.DefaultBackend'},
    name='registration_register'),

# your new view that wraps the existing one
def register_wrap(request, *args, **kwargs):

    # call the standard view here
    response = register(request, *args, **kwargs)

    # check if response is a redirect
    if response.status_code == 302:
        # this was redirection, send json response instead
    else:
        # just return as it is
        return response

如果你需要这个更多的意见,你可以创建一个装饰器使用这个。

If you are going to need this for more views you can just create a decorator using this.

这篇关于Django - Ajax注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆