定制视图与django注册 [英] Customized views with django-registration

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

问题描述

我需要做一个非常简单的修改 - 要求某些视图仅在用户未通过身份验证时显示 - django注册默认视图。例如,如果我已经登录,我不希望用户再次访问/注册页面。

I need to make a very simple modification -- require that certain views only show up when a user is not authenticated -- to django-registration default views. For example, if I am logged in, I don't want users to be able to visit the /register page again.

所以,我认为这里的想法是我想从django注册子类化注册视图。这只是我不知道如何进行的地方。这是正确的方向吗?我应该在这里测试用户的身份验证状态吗?欢迎提示和建议!

So, I think the idea here is that I want to subclass the register view from django-registration. This is just where I'm not sure how to proceed. Is this the right direction? Should I test the user's authentication status here? Tips and advice welcomed!

修改

我认为这是正确的轨道这里: Django:从登录页面重定向登录用户

I think this is the right track here: Django: Redirect logged in users from login page

编辑2

解决方案:

创建另一个应用程序,例如 custom_registration ,并编写一个这样的视图(我也使用自定义窗体):

Create another app, for example, custom_registration, and write a view like this (mine uses a custom form as well):

from registration.views import register
from custom_registration.forms import EduRegistrationForm

def register_test(request, success_url=None,
             form_class=EduRegistrationForm, profile_callback=None,
             template_name='registration/registration_form.html',
             extra_context=None):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/')
    else:
        return register(request, success_url, form_class, profile_callback, template_name, extra_context)

我不得不使用相同的功能参数,但否则只包括测试,如果我们通过它,继续主要功能。

I had to use the same function parameters, but otherwise just include the test, and if we pass it, continue to the main function.

不要忘记把它放在你的URLConf(再次,这包括一些关于我的自定义表单的东西):

Don't forget to put this in your URLConf either (again, this includes some stuff about my custom form as well):

顶级URLConf

(r'^accounts/', include('custom_registration.urls')),
(r'^accounts/', include('registration.urls')),

custom_registration.views

custom_registration.views

from django.conf.urls.defaults import *
from custom_registration.views import register_test
from custom_registration.forms import EduRegistrationForm

urlpatterns = patterns('',
    url(r'^register/$', register_test, {'form_class': EduRegistrationForm}, name='registration.views.register'),
)


推荐答案

据我所知,django注册是使用基于函数的视图,所以你不能真正地对它们进行子类化。我通常遵循的方法是覆盖原始视图(当然没有修改django注册应用程序)。这样做如下:

As far as I remember django-registration is using function-based views, so you can not really subclass them. The approach I usually follow is "overwriting" the original views (without modifying the django-registration app of course). This works like this:


  1. 创建另一个应用程序(您可以将其称为custom_registration或任何您想要的)

  2. 这个程序需要包含另一个 urls.py ,而在另一个 views.py

  3. 将原始注册视图代码复制到新的 views.py 并修改它,添加一个您的 urls.py 指向此视图(使用与此视图的django注册相同的网址格式)

  4. 包含到您的项目 urls.py 的新应用程序 urls.py 之前包括原始的django注册应用程序。这可能是这样的:例如:

  1. Create another app (you could call it custom_registration or whatever you want)
  2. This app need to contain another urls.py and in your case another views.py
  3. Copy the original register view code to your new views.py and modify it, add a pattern to your urls.py to point to this view (use the same url pattern as in django-registration for this view)
  4. Put an include to your projects urls.py of your new app urls.py before your are including the original django-registration app. This could look like this for example:




urlpatterns = patterns('', 
    ...
    url(r'^accounts/', include('custom_registration.urls')),
    url(r'^accounts/', include('registration.backends.default.urls')),
    ... 
)


这是因为 / accounts / register 的第一个匹配的url模式将指向您的新应用程序,所以它永远不会尝试请致电原始应用程式。

This simply works since the first matching url pattern for /accounts/register will point to your new app, so it will never try to call the one from the original app.

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

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