0

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.

Ali Akhtari
  • 1,211
  • 2
  • 21
  • 42
  • 1
    Please refer to https://stackoverflow.com/questions/806835/django-redirect-to-previous-page-after-login#1711592 – gizq Aug 06 '18 at 20:44
  • 3
    @gizq I checked that link, I want to redirect to previous page in views.py not in my template. – Ali Akhtari Aug 06 '18 at 20:45

2 Answers2

1

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.

casol
  • 496
  • 1
  • 7
  • 12
0

Then, what about this?

It should help.

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

gizq
  • 187
  • 11