Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将django根url设置为登录url?_Python_Django_Login_Django Registration - Fatal编程技术网

Python 如何将django根url设置为登录url?

Python 如何将django根url设置为登录url?,python,django,login,django-registration,Python,Django,Login,Django Registration,使用django注册时,url让我有点困惑 假设我希望我的InIndex页面localhost:8000是一个登录页面,但django注册的登录url有一个/login前缀,我是否必须像这样将/login放在我的url中 url(r'^/login/$', include('registration.backends.default.urls')), 在本例中,localhost:8000将为空,这是否意味着部署此项目后,可以像something.com一样查看url 如何将索引页设置为实际的

使用django注册时,url让我有点困惑

假设我希望我的InIndex页面localhost:8000是一个登录页面,但django注册的登录url有一个/login前缀,我是否必须像这样将/login放在我的url中

url(r'^/login/$', include('registration.backends.default.urls')),
在本例中,localhost:8000将为空,这是否意味着部署此项目后,可以像something.com一样查看url


如何将索引页设置为实际的django注册登录页不,实际上,您需要在希望对用户进行身份验证的视图中包含@login\u required decorator,例如

# views.py

from django.contrib.auth.decorators import login_required
from django.http import HttpResponse

@login_required
def index(request):
    return HttpResponse('Only registers will see this page')

# now you can add the stuff that you would want only the registered users to view, 
# so basically if a new visitors tries to visit your site say at www.abc.com, he will first be redirected to the www.abc.com/login/ where he needs to create an account, once authenticated, he will then be redirected to the index page

Reference

我不知道这是最好的方法,但我想你可以进入django注册(registration.auth_url.py)并编辑登录url,使其为r'^$,而不是r'^login/$。这通常不是最好的做法,但应该是可行的。

如果您希望您的实际登录页面是www.example.com,请执行以下操作:

url(r'^$', include('registration.backends.default.urls')),
如果您希望登录页面有自己的页面,请执行以下操作:

url(r'^/login/$', include('registration.backends.default.urls')),

因此,当您访问www.example.com/login时,它将进入登录页面。

您的意思是我的url实际上指向配置文件页面,但这需要登录,因此查看器将首先重定向到url/login页面,对吗?我过去经常这样做,但这是唯一的方法吗?我的意思是url(r’^$)应该指向什么?它应该指向r’^/login/$,因为您需要在“login/”上处理登录,在/registration/上处理注册,并且您的代码将是干净的和可扩展的。这应该是网站的索引页或主页,它应该由您的一个视图处理,如果您需要注册用户才能查看该页面,只需添加@login\u required decorator,用户将被重定向到/login/page,然后才能查看您的索引页面。