简体   繁体   English

Django使用参数/上下文重定向视图

[英]Django redirect views with parameter/context

I have register view in my django app, that redirects to the homepage: 我在django应用程序中注册了视图,该视图重定向到主页:

def register(request):
   ...register...
   return redirect("/")


def homepage(request):
    users = User.objects.all().order_by('username')
    return render(request, 'index.html', {'users': users})

I want to add variable new_user that would tell the homepage if user is new. 我想添加变量new_user ,如果用户是新用户,它将告诉主页。 I would pass variable from register view to the homepage view and than into the template where i would handle it: 我会将变量从寄存器视图传递到主页视图,而不是传递到我将要处理它的模板中:

{% if new_user %}
<h1>Welcome</h1>
{% endif %}

But i dont know how to pass this variable from register view to homepage and than into the template. 但是我不知道如何将这个变量从寄存器视图传递到首页,而不是传递到模板中。 Help is well appreciated! 非常感谢您的帮助!

A simple way to achieve that would be to set a parameter in query: 一种简单的实现方法是在查询中设置参数:

def register(request):
   ...register...
   return redirect('/?new')


def homepage(request):
    is_new_user = 'new' in request.GET
    ....

Try this hope this will help you. 尝试此希望对您有所帮助。

def register(request):
   ...register...
   users = User.objects.all().order_by('username')
   return redirect("/", {'users'=users })


def homepage(request,users):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM