gpt4 book ai didi

Passing 'redirect_field_name' in urls.py not taking effect(在urls.py中传递‘reDirect_field_name’未生效)

转载 作者:bug小助手 更新时间:2023-10-25 10:58:16 26 4
gpt4 key购买 nike



In this section of the Django documentation, it says that passing in redirect_field_name via the url conf, as in

在Django文档的这一节中,它说明了通过url conf传递reDirect_field_name,如下所示



url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'redirect_field_name': '/albums/all/'}),")

URL(r‘^ACCOUNTS/LOGIN/$’,‘django.contri.auth.views.LOGIN’,{‘REDIRECT_FIELD_NAME’:‘/albots/all/’}),“)



should override the next value, which is where it goes after a successful login (it's stated between the two green boxes).

应该覆盖下一个值,该值是成功登录后的位置(在两个绿色框之间显示)。



Mine keeps going to the default (settings.LOGIN_URL, which is "/accounts/profile/", and I don't understand why.

我的设置一直是默认的(settings.LOGIN_URL,即“/ACCOUNS/PROFILE/”),我不明白为什么。



Printing next in the login template results in the empty string: <P>next={{ next }}</P>

在登录模板中打印Next会导致空字符串:

Next={{Next}}



Temporarily giving up on this, I eliminated the {'redirect_field_name', ... from that line in urls.py, and instead changed LOGIN_URL in settings.py to /albums/all/. I restarted the server, yet it's still going to /accounts/login.

暂时放弃这个,我消除了重定向字段名,.从urls.py中的那一行,改为将settings.py中的LOGIN_URL更改为/albums/all/。我重新启动了服务器,但它仍然转到/accounts/login。



I see this question, but there's no reference to urls.py: Django- why inbuilt auth login function not passing info about user to after successful login url

我看到了这个问题,但没有引用urls.py:django-为什么内置的auth登录函数在成功登录url后没有将用户信息传递给



What am I missing?

我遗漏了什么?


更多回答
优秀答案推荐

From what I understand from the documentation, redirect_field_name is used as a key to GET, where the value is the url to redirect to. Looking at the source here also suggests the same. If this is the case redirect_field_name shouldn't be set to a url. According to the documentation for login_required further up in the link you sent, next is passed to the view in the query string. Try passing in the url you wish to redirect to there.

根据我对文档的理解,redirect_field_name被用作GET的键,其中值是要重定向到的URL。看看这里的来源也表明了同样的情况。如果是这种情况,则不应将redirect_field_name设置为URL。根据您发送的链接中的login_required文档,next将在查询字符串中传递给视图。尝试传入您希望重定向到那里的URL。


更多回答

Thank you for the help @crhodes. I don't understand how you mean to pass in this url to the view. How else to do it, except via its line in urls.py? This is how the documentation says to pass in the template_name parameter to the views.login() function (as in the source code you linked to): (r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'myapp/login.html'}),. Since another parameter in that same function is redirect_field_name, I thought using the same method of passing would work, but obviously it doesn't. Can you please elaborate on what your suggesting?

谢谢你的帮助@crhodes。我不明白您的意思是如何将此url传递给视图。除了通过urls.py中的代码行之外,还能怎么做呢?文档介绍了如何将TEMPLATE_NAME参数传递给views.login()函数(与您链接的源代码中一样):(r‘^ACCOUNTS/LOGIN/$’,‘django.contri.auth.views.login’,{‘TEMPLATE_NAME’:‘myapp/login.html’}),。由于同一函数中另一个参数是REDIRECT_FIELD_NAME,我认为使用相同的传递方法会起作用,但显然不起作用。您能详细说明您的建议吗?

From what I understand there are two options, either you specify the url in the querystring like so: ?next=/albums/all/. Or, you specify a redirect_field_name and customize the login template as the docs note: Note that if you provide a value to redirect_field_name, you will most likely need to customize your login template as well, since the template context variable which stores the redirect path will use the value of redirect_field_name as its key rather than "next"

据我所知,有两种选择,要么你在查询字符串中指定URL,就像这样:?下一个=/albums/all/.或者,您指定一个redirect_field_name并自定义登录模板,如文档注释所示:请注意,如果您为redirect_field_name提供一个值,则很可能还需要自定义登录模板,因为存储重定向路径的模板上下文变量将使用redirect_field_name的值作为其键,而不是“next”。

I understand the manually adding ?next=... to the url. I don't know how to "specify a redirect_field_name" As far as customizing the template, I can only guess that it means changing <input type="hidden" name="next" value="{{ name }}" /> to ...name="next" value="{{ redirect_field_name }}" />. If you would be willing to continue this in chat, it would be helpful.

我理解手动添加?NEXT=...发送到URL。我不知道如何“指定REDIRECT_FIELD_NAME”就定制模板而言,我只能猜测这意味着将更改为...NEXT“VALUE=”{{REDIRECT_FIELD_NAME}}“/>。如果你愿意在聊天中继续这个话题,那将是很有帮助的。

If you change the redirect_field_name the template would change as you suggested. However I believe it would be input type="hidden" name="{{ redirect_field_name }}" value="/albums/all/" />. If you look at the source I linked the redirect_to variable storing the url is first checked for in the POST to see if the redirect_field_name is there, otherwise it falls back on the GET and the url being in the querystring.

如果更改REDIRECT_FIELD_NAME,模板将按照您的建议进行更改。然而,我相信它将是输入type=“隐藏”名称=“{{REDIRECT_FIELD_NAME}}”值=“/albumes/all/”/>。如果您查看我链接的reDirect_to变量的源代码,那么首先在POST中检查url,以查看reDirect_field_name是否在那里,否则它将依赖于GET和查询字符串中的url。

Oh! It's a name. I don't care about the name! I want the value to be /albums/all/. Right? Well that certainly clears things up a bit. So I could just forget about this redirect_field_name variable entirely, and just hard-code the value right into the template. That doesn't seem like "the right way" to do it, but it's a step in the right direction. (I wrote name="next" value="{{ name }}"/> in my previous comment, should be value="{{ next }}"). Let me give it a try!

噢!这是个名字。我不在乎名字!我希望该值为/albots/all/。对吗?好吧,这肯定会让事情变得更清晰一些。因此,我可以完全忘记REDIRECT_FIELD_NAME变量,并将值硬编码到模板中。这似乎不是“正确的方式”,但这是朝着正确方向迈出的一步。(我在前面的评论中写了name=“Next”Value=“{{name}}”/>,应该是Value=“{{Next}}”)。让我试一试吧!

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com