After I created a decorator to forbid a logged user to access the login page this error started to appear when I run the server:
File "/home/user1/Dev/sites/pai/lib/python3.8/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/user1/Dev/sites/pai/planeja_tere/publications/urls.py", line 7, in <module>
path('login/', views.loginPage, name='login'),
File "/home/user1/Dev/sites/pai/lib/python3.8/site-packages/django/urls/conf.py", line 73, in _path
raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().
my urls.py:
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('publications/', views.Publications, name='publications'),
path('login/', views.loginPage, name='login'),
path('logout/', views.logoutUser, name='logout'),
path('register/', views.registerPage, name='register'),
]
views.py:
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm
from django.forms import inlineformset_factory
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from .forms import CreateUserForm
from .decorators import unauthenticated_user
# Create your views here.
def home(request):
context = {}
return render(request, 'publications/home.html', context)
@login_required(login_url='login')
def Publications(request):
return render(request, 'publications/publications.html')
def registerPage(request):
form = CreateUserForm()
if request.method == 'POST':
form = CreateUserForm(request.POST)
if form.is_valid():
form.save()
user = form.cleaned_data.get('username')
messages.success(request, user ', sua conta foi criada com sucesso!')
return redirect('login')
context = {'form': form}
return render(request, 'publications/register.html', context)
@unauthenticated_user
def loginPage(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('home')
else:
messages.info(request, 'Usuário ou senha incorretos')
context = {}
return render(request, 'publications/login.html', context)
def logoutUser(request):
logout(request)
return redirect('login')
decorators.py:
from django.http import HttpResponse
from django.shortcuts import redirect
def unauthenticated_user(view_func):
def wrapper_func(request, *args, **kwargs):
if request.user.is_authenticated:
return redirect('home')
else:
return view_func(request, *args, **kwargs)
I'm usin this crash course as source of help on creating login and register views: https://www.youtube.com/watch?v=eBsc65jTKvw&list=PL-51WBLyFTg2vW-_6XBoUpE7vpmoR3ztO&index=15
CodePudding user response:
The decorator needs return a function:
def unauthenticated_user(view_func):
def wrapper_func(request, *args, **kwargs):
if request.user.is_authenticated:
return redirect('home')
else:
return view_func(request, *args, **kwargs)
return wrapper_func # <-- add