Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to add decorators with class in django

from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required

#AS example
class ProfileView(View):
    @method_decorator(login_required(login_url='login'))
    def get(self, request):
      .......
Comment

where django do you get decorators

from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect

from users.models import generate_avatar
from users.forms import (PersonalForm, ProfessionalForm, 
                         SubscriptionsForm)

User = get_user_model()


@login_required
def personal(request):
    profile = request.user.profile
    if request.method == 'POST':
        form = PersonalForm(request.POST, instance=profile)
        if form.is_valid():
            form.save()
            profile.avatar = generate_avatar(profile)
            profile.save()
            return redirect('register-professional')
    else:
        form = PersonalForm(instance=profile)
    return render(request, 'registration/personal.html', {
        'form': form
    })


@login_required
def professional(request):
    profile = request.user.profile
    if request.method == 'POST':
        form = ProfessionalForm(request.POST, instance=profile)
        if form.is_valid():
            form.save()
            return redirect('register-subscriptions')
    else:
        form = ProfessionalForm(instance=profile)
    return render(request, 'registration/professional.html', {
        'form': form
    })


@login_required
def subscriptions(request):
    subscriptions = request.user.subscriptions
    if request.method == 'POST':
        form = SubscriptionsForm(request.POST, instance=subscriptions)
        if form.is_valid():
            form.save()
            request.user.has_finished_registration = True
            request.user.save()
            return redirect('home')
    else:
        form = SubscriptionsForm(instance=subscriptions)
    return render(request, 'registration/subscriptions.html', {
        'form': form
    })
Comment

PREVIOUS NEXT
Code Example
Python :: modbusfc03 python 
Python :: python send commands in one line but write in multiple 
Python :: import baseestimator 
Python :: python average function program 
Python :: list exaple in python 
Python :: Higher-order functions and operations on callable objects in python 
Python :: print the list item dtype 
Python :: how to add a list to a list python 
Python :: python continue outer loop 
Python :: if python 
Python :: python plot confidence interval 
Python :: rename a column 
Python :: how to randomise a string in python 
Python :: python string index 
Python :: django messages framework 
Python :: how to create a window in pygame 
Python :: python check if key exist in dict 
Python :: pygame buttons 
Python :: remove dict python 
Python :: str in python 
Python :: flask app.route 
Python :: python max with custom function 
Python :: np.vstack python 
Python :: run python from c# 
Python :: np minimum of array 
Python :: bitwise operation in python 
Python :: count python 
Python :: numpy difference between two arrays 
Python :: how to set background color for a button in tkinter 
Python :: python string: immutable string 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =