Django注册:如何防止登录用户注册? [英] Django-Registration: How to prevent logged in user from registering?

查看:58
本文介绍了Django注册:如何防止登录用户注册?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用django-registration.我有两个问题:

I've just started to use django-registration. I have two questions:

  1. 如何防止已登录的用户进入注册页面?

  1. How do you prevent a logged in user from going to the register page?

激活后如何自动登录用户?

How do you automatically sign in a user after activation?

我宁愿不更改应用程序本身中的任何代码.

I prefer not changing any code in the app itself.

对于问题2,我已经阅读了文档,其中写着侦听适当信号的函数;您的函数应将用户的backend属性设置为正确的身份验证后端,然后调用django.contrib.auth.login()来登录用户."我对django不太了解,无法理解这意味着什么或如何实现.你们能帮我/指出正确的方向吗?

For question 2, I've already read the docs where it says to write "a function which listens for the appropriate signal; your function should set the backend attribute of the user to the correct authentication backend, and then call django.contrib.auth.login() to log the user in." I don't know django well enough to understand what this means or how to implement this. Could you guys help/point me in the right direction?

尝试发出一些信号,但仍无法正常工作,不确定出现了什么问题:

Tried doing some signals, does not yet work, not sure what's wrong:

def loginActivationCallback(sender, user, request, **kwargs):
    print user
    print "registered"

user_registered.connect(loginActivationCallback)

此外,因为我使用的是Django 1.5,所以我没有进行pip install django-registration(不完全支持1.5),而是将注册文件夹复制到了我的项目中.不确定这是否会影响信号.

Also because I'm using Django 1.5, I didn't do pip install django-registration(does not fully support 1.5), but instead copied the registration folder into my project. Not sure if this affects the signals.

推荐答案

简单地,您可以在注册视图中查看

Simply what you can do is check in your register view

 if request.user.is_authenticated:
     #redirect user to the profile page 
     return HttpResponseRedirect('/profile/')

这篇关于Django注册:如何防止登录用户注册?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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