Monday, December 1, 2014

Re: Using login_required decorator

The best way to require login to certain url and view hooks is the:

@login_required

decorator. Let me show you how I have it setup:

My "Profile" view is the profile a user sees once they are logged in to then have the ability to edit their information so obviously this needs to have the login required decorator because they shouldn't be able to get to this point unless they are logged in or are authenticated but here it is:


@login_required

def Profile(request):

    contextdata = {}

    if request.user.groups.filter(name='Athletes').exists():

        return render_to_response('profile.html', contextdata, context_instance = RequestContext(request))

    else:

        contextdata = {'Error': 'Error'}

        return render_to_response('profile.html', contextdata, context_instance = RequestContext(request))


And in the template "profile.html" I then link to /profile/edit/ where a user edits their information but this is the sort of "home" page for their profile after they are logged in. I used similar logic and a similar approach to what you were doing before but then I upgraded to this approach so I recommend you do the same. Here is the documentation as supplemental information:

https://docs.djangoproject.com/en/1.7/topics/auth/default/#the-login-required-decorator

Make sure to set the LOGIN_URL in your settings so django knows where to send people when they try to access a restricted page when they are not logged in:

https://docs.djangoproject.com/en/1.7/ref/settings/#std:setting-LOGIN_URL

Once you review all this you will have a much cleaner and more concise implementation across your site.

Cheers,

JJ


On Monday, December 1, 2014 6:59:00 PM UTC-5, Rootz wrote:
I have a small project but I am trying to restrict access on some of the django app urls to login users only. The problem is that when I hit a page that requires login users I expected that they(users) are redirected to the login page however that is not the case of what happens instead they are redirected to an example url link like this '/login?next=/detail/1/' with an error message as stated "TypeError at /login/ object() takes no parameters" 

The django project url

(r'^detail/(?P<pk>\d{1,10})/$',login_required(views.DetailViewMember.as_view)),

url(r'^login/$',views.members_login,name='login'),

The Login View Function

def members_login(request):

    if request.method == 'POST':
        password = request.POST['password']
        username = request.POST['username']
        user = authenticate(username=username,password=password)

        if user is not None:
            if user.is_active:
                login(request,user)
                return redirect('members:index')
            else:
                #inactive users required to re-register
                return redirect('members:index')#render(request,'members/login',dict(loginErr=True))
        else:
            #no account required to register to create one
            return redirect('members:index')
    
    else:
        #test if login is a regular get request then redirect
        return redirect('members:index')

Can you explain to me why is it the I am getting this error?

Thank you

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/90f90d6c-e1a6-4893-97db-e2e6d5f48a7d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment