Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

django login required

from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    # handle view
-------------------------------------------------------------------

# add in settings.py

LOGIN_URL = your_login-page_url

### now if the user attemped to request the view without login  ###
### user will be redirected to the login-page using the provided url in settings.py ###
Comment

Django login_required decorator

from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    ...
    


# login_required() does the following:

# If the user isn’t logged in, redirect to settings.LOGIN_URL,
# passing the current absolute path in the query string.
# Example: /accounts/login/?next=/polls/3/. If the user is logged in,
# execute the view normally. The view code is free to assume the user is logged in.
# By default, the path that the user should be redirected to upon successful authentication is stored in a query string parameter called "next".
# If you would prefer to use a different name for this parameter, 
# login_required() takes an optional redirect_field_name parameter:




from django.contrib.auth.decorators import login_required

@login_required(redirect_field_name='my_redirect_field')
def my_view(request):
    ...




# Note that if you provide a value to redirect_field_name,
# you will most likely need to customize your login template as well,
# since the template context variable which stores the redirect path will use the value of redirect_field_name as its key rather than "next" (the default).




login_required() also takes an optional login_url parameter. Example:

from django.contrib.auth.decorators import login_required

@login_required(login_url='/accounts/login/')
def my_view(request):
    ...




# Note that if you don’t specify the login_url parameter,
# you’ll need to ensure that the settings.LOGIN_URL and your login view are properly associated.
# For example, using the defaults,
# add the following lines to your URLconf:




from django.contrib.auth import views as auth_views

path('accounts/login/', auth_views.LoginView.as_view()),




# The settings.LOGIN_URL also accepts view function names and named URL patterns.
# This allows you to freely remap your login view within your URLconf without having to update the setting.
Comment

login_required on class django

You can add the decorator in the urls.py

from django.contrib.auth.decorators import login_required
 url(r'^workers/$', login_required(views.RootWorkerView.as_view()))
Comment

login_required on class django

now you can use Django builtin LoginRequiredMixin

from django.contrib.auth.mixins import LoginRequiredMixin

class MyView(LoginRequiredMixin, View):
    login_url = '/login/'
    redirect_field_name = 'redirect_to'
Comment

django login required

def login_view(request):
    if request.method == 'GET':
        cache.set('next', request.GET.get('next', None))

    if request.method == 'POST':
        # do your checks here

        login(request, user)

        next_url = cache.get('next')
        if next_url:
            cache.delete('next')
            return HttpResponseRedirect(next_url)

    return render(request, 'account/login.html')
Comment

PREVIOUS NEXT
Code Example
Python :: time py 
Python :: multiprocessing a for loop python 
Python :: yahoo finance api python 
Python :: remove extra spaces and empty lines from string python 
Python :: element wise subtraction python list 
Python :: how to remove a string inside another string python 
Python :: run linux command using python 
Python :: make a script run itself again python 
Python :: ban command in discord.py 
Python :: Delete file in python Using the os module 
Python :: pandas max columns 
Python :: how to check an element in a list in python 
Python :: E: Unable to locate package python-gobject 
Python :: timedelta 
Python :: how to take input in python3 separated by space 
Python :: pandas iterate rows 
Python :: python check if number is integer or float 
Python :: lower upper in pytho 
Python :: How to colour a specific cell in pandas dataframe 
Python :: isolate row based on index pandas 
Python :: python remove everything after character 
Python :: python infinity 
Python :: when button is clicked tkinter python 
Python :: loop over twodimensional array python 
Python :: clone website 
Python :: docker django development and production 
Python :: train_test_split sklearn 
Python :: copy list python 
Python :: django login code 
Python :: OneHotEncoder() 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =