Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8393521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:48:45+00:00 2026-06-09T19:48:45+00:00

views.py def login_user(request): if request.POST: sform = LoginForm(request.POST) rform = UserForm() if sform.is_valid(): username

  • 0

views.py

def login_user(request):
    if request.POST:
        sform = LoginForm(request.POST)
        rform = UserForm()
        if sform.is_valid():
            username = request.POST.get('username')
            password = request.POST.get('password')
            user = authenticate(username = username,password = password)
            if user is not None:
                if user.is_active:
                    login(request,user)
                    return redirect(request.path)
                else:
                    state = "Your account is not active."
                    #or first set the user to is active and then log in 
                    return redirect('acitvate_account',user = user)
                return render_to_response('home.html',{'state':state,'user':user},context_instance = RequestContext(request))
            else:
                state = "Incorrect Username or Password"
                #sform = LoginForm()
                form = LoginForm()
                return render_to_response('register.html',{'state':state,'sform':sform,'rform':rform,'form':form},context_instance = RequestContext(request))
        else:
            state = "Invalid form"
            dLoginForm = LoginForm()
            rform = PartialSignupForm()
            return render_to_response('register.html',{'sform':sform,'rform':rform,'form':form},context_instance = RequestContext(request))
    else:
        dLoginForm = LoginForm()
        rform = PartialSignupForm()
        return render_to_response('category.html',{'dLoginForm':dLoginForm,'rform':rform},context_instance = RequestContext(request))

urls.py

urlpatterns += patterns('beenthere.views',
    #beenthere urls
    (r'^$','index'),
    (r'^home2/$','category'),
    (r'^accounts/login/$','login_user'),
    (r'^accounts/register/$','register_user'),
    (r'^home/(?P<user>\w*)$','home'),
    (r'^logout/','logout_user'),

    )

doubt

how am i suppose to redirect the same url from which request is sent , i tried using request.path but it redirects me to the /accounts/login/ url , which is not desired.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:48:46+00:00Added an answer on June 9, 2026 at 7:48 pm

    What you need to do is track what the referrer was when you displayed the login form, possibly storing that value in the users session, then redirect the user back to that url once they’ve successfully logged in.

    Something along the lines of:

    def login_user(request):
        if request.POST:
            sform = LoginForm(request.POST)
            rform = UserForm()
            if sform.is_valid():
                username = request.POST.get('username')
                password = request.POST.get('password')
                user = authenticate(username = username,password = password)
                if user is not None:
                    if user.is_active:
                        login(request,user)
                        return redirect(request.path)
                    else:
                        state = "Your account is not active."
                        #or first set the user to is active and then log in 
                        return redirect(req.session['after_login_url'],user = user)
                    return render_to_response('home.html',{'state':state,'user':user},context_instance = RequestContext(request))
                else:
                    state = "Incorrect Username or Password"
                    #sform = LoginForm()
                    form = LoginForm()
                    return render_to_response('register.html',{'state':state,'sform':sform,'rform':rform,'form':form},context_instance = RequestContext(request))
            else:
                state = "Invalid form"
                dLoginForm = LoginForm()
                rform = PartialSignupForm()
                return render_to_response('register.html',{'sform':sform,'rform':rform,'form':form},context_instance = RequestContext(request))
        else:
            dLoginForm = LoginForm()
            rform = PartialSignupForm()
            req.session['after_login_url'] = req.META.get('HTTP_REFERER')
            return render_to_response('category.html',{'dLoginForm':dLoginForm,'rform':rform},context_instance = RequestContext(request))
    

    Of course you should also add checks to make sure that the HTTP_REFERER header exists, and make sure the after_login_url key exists (and possibly has a sane value, in case an external site links directly to your log in page, or similar), and have a safe fall back if any of these tests fail (like maybe a user’s profile page, or your sites index page).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in views.py : def login(request): if 'username' in request.POST and 'password' in request.POST: user=User.objects.get(Username__exact=request.POST['username'],Password__exact=request.POST['password'])
This is my login views: def login(request): redirect_to = request.REQUEST.get(next) if request.method == 'POST':
my app has a basic login function in views: def login_page(request): username = request.POST['username']
My views.py: @login_required def some_views(request): if request.method == 'POST': form = AddressCreateFrom(request.POST) if form.is_valid():
@login_required def post_review(request): if request.method == 'POST': formset = ReviewForm(request.POST) if formset.is_valid(): formset.save(commit=False) #formset.author
to have autocompletion on an input,I do this: in views.py : def getpositions(request): if
In my django views i have the following def create(request): query=header.objects.filter(id=a)[0] a=query.criteria_set.all() logging.debug(a.details) I
I have used the inlineformset, the views method is as follows: @login_required def patron_edit_phone(request,
I want to test this view: def register(request): handle user registration code variable is
I have a registration page with the following code below: in the views: def

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.