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 6001613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:51:35+00:00 2026-05-23T00:51:35+00:00

I have several function that need to have a ‘redirect’ filter. The redirect filter

  • 0

I have several function that need to have a ‘redirect’ filter. The redirect filter goes something like this —

1) if a user is not logged in and has no session data, redirect to login page.

2) if a user is logged in and has already filled out the page, redirect to user home.

3) if a user is logged in and has not already filled out the page, stay on the page.

4) if a user is not logged in and has session data, stay on the page

I’ve started to convert the functions into a class-based approach to make it more efficient and less code (previously my view functions were pretty massive). This is my first stab at trying make something class-based, and this is what I have so far —

def redirect_filter(request):
    if request.user.is_authenticated():
        user = User.objects.get(email=request.user.username)
        if user.get_profile().getting_started_boolean:
            return redirect('/home/') ## redirect to home if a logged-in user with profile filled out
        else:
            pass  ## otherwise, stay on the current page
    else
        username = request.session.get('username')
        if not username:  ## if not logged in, no session info, redirect to user login
            return redirect('/account/login')
        else:
            pass  ## otherwise, stay on the current page

def getting_started_info(request, positions=[]):
    location = request.session.get('location')
    redirect_filter(request)
    if request.method == 'POST':
        form = GettingStartedForm(request.POST)
        ...(run the function)...
    else:
        form = GettingStartedForm() # inital = {'location': location}
    return render_to_response('registration/getting_started_info1.html', {'form':form, 'positions': positions,}, context_instance=RequestContext(request))

Obviously, this view is not fully working yet. How would I convert this into something that’s functional?

Also, I have three variables that will need to be reused in several of the getting_started functions:

user = User.objects.get(email=request.user.username)
profile = UserProfile.objects.get(user=user)
location = profile.location

Where would I put these variable definitions so I can reuse them in all the functions, and how would I call them?
Thank you.

  • 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-05-23T00:51:36+00:00Added an answer on May 23, 2026 at 12:51 am

    Django actually already includes a login_required decorator that makes handling user authentication trivial. Just include the following at the top of your view.py page:

    from django.contrib.auth.decorators import login_required
    

    and then add

    @login_required 
    

    before any views that require a login. It even handles redirecting the user to the appropriate page once they log in.

    More info here:
    https://docs.djangoproject.com/en/dev/topics/auth/#the-login-required-decorator

    This should greatly simplify your views, and may result in not having to write a separate class, since all that’s left is a simple re-direct.

    As for the variables, each request already contains a request.user object with information on the user. You can do a search in the docs for Request and response objects to learn more.

    You can use that user object to get the profile variable by extending the user module. Set AUTH_PROFILE_MODULE = ‘myapp.UserProfile’ in your Settings, which will allow you to access a users profile as follows:

    user.get_profile().location. 
    

    More about that here:
    http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/

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

Sidebar

Related Questions

I have several event handlers that I bind like this: $('#MyDiv').click(function () {...}); $('#SomeDiv').mouseenter(function
I have several functions that may or may not need to be repeated a
I have a legacy function that looks like this: int Random() const { return
I have a C function that takes several arguments of double indirected pointers. something
I have a data-upload function that load some data into several tables and processes
I have several methods that I need to wrap in new methods in basically
I have this function here that assembles an API call to eBay. It used
I have several modules (mainly C) that need to be redesigned (using C++). Currently,
I have a web application running on Tomcat. There are several calculations that need
I have a function that is called by several GUI components, and always needs

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.