naive_user
naive_user

Reputation: 119

Redirecting to Home Page if session not found

I'm creating a form where data is coming from previous form and i'm storing that data in session. If user directly visits second page than I want the user to redirect to first page, but I can't find any way to do it. Is it possible?

my code(second page)

def get(self, request):
    a = request.session['a']

since a is not set if user directly visits this page I want him to redirect to first page if session not found.

Upvotes: 0

Views: 555

Answers (1)

user2390182
user2390182

Reputation: 73480

You can redirect the user if the key is missing in the session:

from django.shortcuts import redirect

def get(self, request):
    if 'a' not in request.session:
        return redirect("name_of_first_view")
    # ...

Upvotes: 2

Related Questions