Ali Akhtari
Ali Akhtari

Reputation: 1297

Redirect to previous page after Sign up in view.py : Django 2

I build a signup and login page and I want that user after has been signed up or logged in redirect to the page that was been. this is my views.py :

def signup(request):
    if request.method == 'POST':
        try:
            try:
                user = User.objects.get(username=request.POST['username'])
                return render(request, 'accounts/SignUp.html',
                              {'error': 'This UserName Has Already Exist, Pleas Try Another UserName.'})
            except:
                user = User.objects.get(email=request.POST['email'])
                return render(request, 'accounts/SignUp.html',
                              {'error': 'This Email Has Already Registered, Pleas Try Another Email.'})

        except User.DoesNotExist:
            user = User.objects.create_user(username=request.POST['username'], email=request.POST['email'],
                                            password=request.POST['pass'],
                                            first_name=request.POST['fname'], last_name=request.POST['lname'])
            auth.login(request, user)
            return render(request, 'StartPage/StartPage.html', {'error': 'Conjurations! You have Signed Up '
                                                                         'Successfully.'})

    else:
        return render(request, 'accounts/SignUp.html')  

now how can I make redirect to the page that user has been before clicked for signup? In addition, I'm sorry for writing mistakes in my question.

Upvotes: 0

Views: 60

Answers (2)

casol
casol

Reputation: 516

Maybe you could submit a form with a given URL then retrieve variable in view.py, for example you can use a hidden input and pass current URL as a value:

# html
<input type="hidden" name="" value="">

# views.py
request.POST.get('url')
redirect_to_url = request.POST.get('url')

You can use the redirect() function

def singup(request):
    ...
    return redirect(redirect_to_url)

Check also build_absolute_uri(), You can get full url using request.build_absolute_uri method.

Upvotes: 1

gizq
gizq

Reputation: 197

Then, what about this?

It should help.

https://github.com/thoas/django-backward

Upvotes: 0

Related Questions