Django - 使用上下文重定向 [英] Django - Redirect with context

查看:25
本文介绍了Django - 使用上下文重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注册页面,它接收用户的用户名和密码并将其保存在数据库中.如果用户成功注册,我想将他重定向到登录"页面,其中的值表示注册成功.

I have a Sign Up page which takes a user's username and password and saves it in the database. If the user is successfully signed up, I want to redirect him to a 'Sign In' page, with a value denoting that Signup was successful.

重定向页面上的预期输出:

Expected output on the redirected page:

您已成功注册.

登录

用户名:---

密码:---

因此,我正在从注册页面重定向到登录页面,但我想传递一个值,表明用户来自注册页面并已成功注册.

So, I am redirecting from the signup to the signin page, but I want to pass a value showing that the user has come from the Signup page and is successfully signed up.

我该如何执行此操作?

推荐答案

使用消息框架 在第一个视图中添加消息并在第二个视图中显示.

Use the messages framework to add a message in the first view and display it in the second one.

或者简单地将一个项目放入会话中,然后再次弹出:

Or simply put an item in the session, and pop it out again:

request.session['signup'] = True
return redirect('wherever')

...

signup = request.session.pop('signup', False)
if signup:
    ... do something ...

这篇关于Django - 使用上下文重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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