Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting separate functions into class-based
    primarykey
    data
    text
    <p>I have several function that need to have a 'redirect' filter. The redirect filter goes something like this --</p> <p>1) if a user is not logged in and has no session data, redirect to login page.</p> <p>2) if a user is logged in and has already filled out the page, redirect to user home.</p> <p>3) if a user is logged in and has not already filled out the page, stay on the page.</p> <p><em><strong>4) if a user is not logged in and has session data, stay on the page</em></strong></p> <p>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 --</p> <pre><code>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)) </code></pre> <p>Obviously, this view is not fully working yet. How would I convert this into something that's functional? </p> <p>Also, I have three variables that will need to be reused in several of the <code>getting_started</code> functions:</p> <pre><code>user = User.objects.get(email=request.user.username) profile = UserProfile.objects.get(user=user) location = profile.location </code></pre> <p>Where would I put these variable definitions so I can reuse them in all the functions, and how would I call them? Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload