简体   繁体   中英

Django redirect views with parameter/context

I have register view in my django app, that redirects to the homepage:

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. 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):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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