#can someone help

32 messages · Page 1 of 1 (latest)

brazen spoke
#

i have a problem with django decorators.py file that i made for user authentication

#

this is the error

crystal basinBOT
#

@brazen spoke

marvin the martian Uploaded Some Code
Uploaded these files to a Gist
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")
red grove
#

You're not returning your wrapper function

#

I dunno why you have the http response as the return instead

brazen spoke
#

uhm i asked chatgpt to help

#

he gave this as a solution

red grove
#

Yeah, ChatGPT isn't a good way to write code that actually works

#

It only kinda understands code and often makes mistakes like this

brazen spoke
#

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
red grove
#

Try that yeah

brazen spoke
#

it solved the problem but i might get a loop of redirects tho with was the main issue

#

yep

red grove
#

You'll only get a redirect loop if you use the unauthenticated user decorator on your login view.

brazen spoke
#

i am

#

uh o let me check

red grove
#

Which would make no sense because if they're trying to log in they're clearly not authenticated

brazen spoke
#

i mean i could try and log out

#

im gettign a error when i want to log out

red grove
#

Your view_func is probably wrong

brazen spoke
#

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
red grove
#

The function that is view_func isn't returning an http response

brazen spoke
#

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

red grove
#

I'm gonna suggest you brush up on your python. This isn't how returns work and it's pretty clear you don't understand decorators and what view_func is.

#

I'm not gonna spend my day being your human ChatGPT.