Answer the question
In order to leave comments, you need to log in
How to make login to Django site only after authorization?
Hello. At work, I had to write a small intracorporate portal site. Since I am not strong in programming languages, the choice fell on a bunch of Python + Django. (This is my personal choice with a look into the future, please do not breed questions about the advantages / disadvantages of languages, CMS, etc.) The essence will be this: there are two groups of users - let's call them administrators and users. Actually the question is: how to make it so that when entering the site it was necessary to pass authorization, and only after it get access to the site functions that are defined in the groups / users section. Google leads only to how to organize authorization after you have entered the site.
Thanks in advance.
Answer the question
In order to leave comments, you need to log in
Force authorization:
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
class ValidLogin(object):
def process_request(request):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('signin'))
return None
MIDDLEWARE_CLASSES = (
'project.middleware.ValidLogin',
)
from django.conf.urls import patterns, url
from django.contrib.auth.decorators import user_passes_test
urlpatterns = patterns('crm.views',
url(r'^$',
user_passes_test(lambda u: u.is_superuser or u.is_moderator)(HomeCRM.as_view()),
name='home'),
)
from django.contrib.auth.decorators import login_required
urlpatterns = patterns('crm.views',
url(r'^$',
login_required(HomeCRM.as_view()),
name='home'),
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question