#can someone help
32 messages · Page 1 of 1 (latest)
@brazen spoke
this is the code im dealing with
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('login_user')
else:
return view_func(request, *args, **kwargs)
# Add a default HttpResponse at the end of the function
return HttpResponse("You are not authorized to access this page")
aka decorators.py file
You're not returning your wrapper function
I dunno why you have the http response as the return instead
Yeah, ChatGPT isn't a good way to write code that actually works
It only kinda understands code and often makes mistakes like this
like this right
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('login_user')
else:
return view_func(request, *args, **kwargs)
# Add a default HttpResponse at the end of the function
return wrapper_func
Try that yeah
it solved the problem but i might get a loop of redirects tho with was the main issue
yep
You'll only get a redirect loop if you use the unauthenticated user decorator on your login view.
Which would make no sense because if they're trying to log in they're clearly not authenticated
Your view_func is probably wrong
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('login_user')
else:
return view_func(request, *args, **kwargs)
# Add a default HttpResponse at the end of the function
return wrapper_func
The function that is view_func isn't returning an http response
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('login_user')
else:
return view_func(request, *args, **kwargs)
return HttpResponse("You are not authorized to access this page")
# Add a default HttpResponse at the end of the function
return wrapper_func
still same error