使用 auth.views 进行 django 身份验证

发布于 2024-07-21 21:38:02 字数 1005 浏览 4 评论 0原文

用户注册后和注销后应重定向到登录页面。 在这两种情况下,都必须显示一条消息来指示相关消息。

使用 django.contrib.auth.views.login 如何发送这些 {{ info }} 消息。

一个可能的选择是将 auth.views 复制到新的注册模块并包含所有必要的内容。 但这似乎还不够干燥。

最好的方法是什么。

更新:问题阐述:

在正常情况下,当您想要向某些用户指示您可以使用的操作的响应时,

request.user.message_set.create()

这将创建一条显示在其中一个模板中并自动删除的消息。

但是,此消息系统仅适用于继续具有相同会话 ID 的登录用户。 在注册的情况下,用户未经过身份验证,并且在由于会话更改而注销的情况下,无法使用该系统。

除此之外,django.contrib.auth.views 中的内置 loginlogout 函数返回“HttpResponseRedirect”,这使得不可能将另一个变量添加到模板中。

我尝试在请求对象本身上设置一些内容

request.info='Registered'

,并在不同的视图中检查它,

try:
   info = request.info:
   del request.info
except:
   info = ''

#later
render_to_response('app/file',{'info':info})

即使这也不起作用。

显然,我可以定义一个 Registered.html 并在其中添加此静态消息,但我懒得编写另一个模板并尝试干实现它。

我意识到“注册”消息和“注销”消息的情况是不同的。 我使用的 DRY 方法,我将写为答案。

User should be redirected to the Login page after registration and after logout. In both cases there must be a message displayed indicating relevant messages.

Using the django.contrib.auth.views.login how do I send these {{ info }} messages.

A possible option would be to copy the auth.views to new registration module and include all essential stuff. But that doesn't seem DRY enough.

What is the best approach.

Update: Question elaboration:

For normal cases when you want to indicate to some user the response of an action you can use

request.user.message_set.create()

This creates a message that is displayed in one of the templates and automatically deletes.

However this message system only works for logged in users who continue to have same session id. In the case of registration, the user is not authenticated and in the case of logout since the session changes, this system cannot be used.

Add to that, the built in login and logout functions from django.contrib.auth.views return a 'HttpResponseRedirect' which make it impossible to add another variable to the template.

I tried setting things on the request object itself

request.info='Registered'

and check this in a different view

try:
   info = request.info:
   del request.info
except:
   info = ''

#later
render_to_response('app/file',{'info':info})

Even this didn't work.

Clearly I can define a registered.html and add this static message there, but I was being lazy to write another template and trying to implement it DRY.

I realized that the cases were different for "registered" message and "logged out" message. And the DRY approach I used, I shall write as an answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

权谋诡计 2024-07-28 21:38:02

如果消息是静态的,您可以为这些视图使用自己的模板:

(r'^accounts/login/

来自 docs< /a>.

, 'django.contrib.auth.views.login', {'template_name': 'myapp/login.html'}

来自 docs< /a>.

If the messages are static you can use your own templates for those views:

(r'^accounts/login/

From the docs.

, 'django.contrib.auth.views.login', {'template_name': 'myapp/login.html'}

From the docs.

半世蒼涼 2024-07-28 21:38:02

我认为解决这个问题的最佳方案是使用“flash”类型的基于会话的消息传递系统。 有几个浮动: django-flash 看起来真的很好,我使用 django-session-messages 这非常简单。 希望当我们到达 Django 1.2 时,这将被内置。

I think the best solution to this problem is to use a "flash"-type session-based messaging system. There are several floating around: django-flash seems really nice, I use django-session-messages which is very simple. Hopefully by the time we get to Django 1.2 this'll be baked-in.

爱本泡沫多脆弱 2024-07-28 21:38:02

您可以使用请求上下文处理器将此类信息添加到呈现的每个模板的上下文中。

这是做这种事情的“零影响”方式。 您不更新任何视图函数,因此它满足 DRY 的一些定义。

请参阅 http://docs.djangoproject.com/en/dev/ ref/templates/api/#id1

首先,编写您自己的login.html 模板。

其次,编写您自己的上下文函数以提供必须插入模板中的任何附加信息。

第三,更新设置以将上下文处理器添加到 TEMPLATE_CONTEXT_PROCESSORS 设置中。

You have Request Context Processors to add this kind of information to the context of every template that gets rendered.

This is the "zero impact" way to do this kind of thing. You don't update any view functions, so it meets some definitions of DRY.

See http://docs.djangoproject.com/en/dev/ref/templates/api/#id1

First, write your own login.html template.

Second, write your own context function to provide any additional information that must be inserted into the template.

Third, update settings to addy your context processor to the TEMPLATE_CONTEXT_PROCESSORS setting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文