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

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

问题描述

我正在尝试允许注册(使用这个 django-registration register view) 到我的一个应用程序的模式对话框.

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-registration 视图或将其包装到其他视图中来管理响应.

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 以使用另一个功能包装现有视图.为此,您必须在 backends 文件夹中创建一个新的后端包并在那里更改 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 指向新视图:

Point url to the new view:

# 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天全站免登陆