# Django Views
# Typical Imports
The type of imports you need
from django.contrib.auth.decorators import login_required
from django.contrib.auth.decorators import user_passes_test
from django.shortcuts import render, redirect
from django.conf import settings as app_settings
from django.views import View
from .forms import *
from .models import *
from .functions import *
from django.contrib import messages
from django.contrib.auth.models import User
# User Passes test decorator
from django.contrib.auth.decorators import login_required
from django.contrib.auth.decorators import user_passes_test
def anonymous_required(function=None, redirect_url=None):
if not redirect_url:
redirect_url = 'dashboard'
actual_decorator = user_passes_test(
lambda u: u.is_anonymous,
login_url=redirect_url
)
if function:
return actual_decorator(function)
return actual_decorator
#using the anonymous required decorator
@anonymous_required
def login(request):
## REst of the code
#using the long-required decorator
@login_required
def logout(request):
auth.logout(request)
return redirect('login')
# Typical Route with form
@login_required
@pass_another_test
@pass_test3
def someView(request):
modelss = Model.objects.filter(variable=variable)
context = {}
context['models'] = models
if request.method == 'GET':
form = ModelForm()
context['form'] = form
return render(request, 'app/models-view.html', context)
if request.method == 'POST':
form = ModelForm(request.POST)
if form.is_valid():
obj = form.save(commit=False)
obj.extraVariable = extraVariable
obj.save()
messages.success(request, "Model Updated Succesfully")
return redirect('models-view')
else:
context['form'] = form
messages.error(request,"Problem processing your request")
return render(request, 'app/models-view.html', context)
return render(request, 'app/models-view.html', context)