English 中文(简体)
• 如何具体指明在django中需要再补给吗?
原标题:How to specify the login_required redirect url in django?

我有以下看法:

@login_required
def myview():
    # do something
    # respond something
    pass

我怎么能具体说明重新调整这种观点功能的准确数字?

最佳回答

LOGIN -URL

参考:

问题回答

您认为,如果我没有在环境中宣布的话,你就可以这样做。 y

from django.contrib.auth.decorators import login_required

@login_required(login_url= /example url you want redirect/ ) #redirect when user is not logged in
def myview(request):
    do something
    return something #returns when user is logged in

default login url is: /accounts/login/
if you want to change it then go to settings.py

LOGIN_URL= /path/to/url 
LOGIN_REDIRECT_URL= /path/to/redirecturl 

this from documentation should be helpful: https://docs.djangoproject.com/en/1.5/topics/auth/default/#the-login-required-decorator

@login_required(login_url= /accounts/login/ )
def my_view(request):
    ...

Go to your setting.py You can add this anywhere in your settings.py file but i prefer to place it at the bottom. LOGIN_URL = /login/

NOTE: /login/ 是URL部分,使用户进入标识页。 完整的URL类似于“Mexample.com/login/”。

您也可以从中选取。

例如

path( login/ , login_view, name= login_name ),

之后,将作出裁定

@login_required(login_url= login_name )

我们有两种办法,首先是今后维持守则的最佳做法,第二种做法是,当卢爱阵更改标志时,就是一种 has。

初步办法

设定。 进口逆向-拉齐,并将LOGIN_URL设定为标志性空间

from django.urls import reverse_lazy
LOGIN_URL = reverse_lazy( login )

您认为,在每项职能之前,您的进口记录需要填写,并要求登录。

from django.contrib.auth.decorators import login_required

@login_required()
def view_name(request):
    pass

Second approach
This approach is not reliable since you won t have a defined variable holding your login URL namespace

您认为,在每项职能之前,您的进口记录需要填写,并要求登录。

from django.contrib.auth.decorators import login_required

@login_required(login_url= /path/to/login/ )
def view_name(request):
    pass

在这两种情况下,静态是你必须进口原木。

In django project settings

增 编

LOGIN_REDIRECT_URL =  path/to/url 

进口 一、导 言

`@login_required(login_url=LOGIN_REDIRECT_URL)`

你想要限制安理会工作的观点

Django 4+使这种情况非常容易。

ur p p p p

path( login/ , login_view, name= login_name ),

然后,在环境中,你们都需要做的是,在底线上增加:

LOGIN_URL =  login_name 

如今,所有您的<代码>@login_required decorators将自动寄出不贴在您网页上的任何人。 如果你将来改变这种ur势,情况就会改变。 旧档案从提及姓名而不是绝对的尿道开始不断更新。

我建议against采取上述任何途径/方针,因为一旦你改变这条道路,你就需要改变一切。 参考日志——在您的环境下一次。 页: 1

我的签字/签字页忽视了“?next=......部分路程,只是转向主页,......。

By including: <input type="hidden" name="next" value="{{ request.GET.next }}">

在我提交纽顿之后,该回头公司进行了适当的工作。





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签