gpt4 book ai didi

Django - View accounts.decorators.wrapper_function 没有返回 HttpResponse 对象。它返回 None 而不是

转载 作者:行者123 更新时间:2023-12-05 08:18:33 25 4
gpt4 key购买 nike

我正在构建一个客户管理应用程序并且构建了几个装饰器。但是当我运行应用程序时出现此错误

The view accounts.decorators.wrapper_function didn't return an HttpResponse object. It returned None instead.

装饰器.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')
elif request.user.is_authenticated == None:
return redirect('login')
else:
return view_func(request, *args, **kwargs)

return wrapper_func


def allowed_users(allowed_roles=[]):
def decorator(view_func):
def wrapper_func(request, *args, **kwargs):

group = None
if request.user.groups.exists():
group = request.user.groups.all()[0].name

if group in allowed_roles:
return view_func(request, *args, **kwargs)
else:
return HttpResponse('You are not authorized to view this page')
return wrapper_func
return decorator


def admin_only(view_func):
def wrapper_function(request, *args, **kwargs):
group = None
if request.user.groups.exists():
group = request.user.groups.all()[0].name

if group == 'customer':
return redirect('user-page')

if group == 'admin':
return view_func(request, *args, **kwargs)

return wrapper_function

和我的views.py

from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.forms import inlineformset_factory
from django.contrib.auth.forms import UserCreationForm

from django.contrib.auth import authenticate, login, logout

from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import Group

from .models import *
from .forms import OrderForm, CreateUserForm, CustomerForm
from .filters import OrderFilter
from .decorators import unauthenticated_user, allowed_users, admin_only

# Create your views here.
@unauthenticated_user
def registerPage(request):
form = CreateUserForm()
if request.method == 'POST':
form = CreateUserForm(request.POST)
if form.is_valid():
user =form.save()
username = form.cleaned_data.get('username')


messages.success(request,'Account was created for '+ username)
return redirect('login')

context = {'form':form}
return render(request,'accounts/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, 'Username or password or incorrect')
context = {}
return render(request,'accounts/login.html',context)


def logoutUser(request):
logout(request)
return redirect('login')

@admin_only
@login_required(login_url='login')
def home(request):
orders = Order.objects.all()
customers = Customer.objects.all()

total_customer = customers.count()
total_orders = orders.count()
delivered = orders.filter(status='Delivered').count()
pending = orders.filter(status='Pending').count()


context = {'orders':orders,'customers':customers,'total_orders':total_orders,'delivered':delivered,'pending':pending}
return render(request, 'accounts/dashboard.html',context)

@login_required(login_url='login')
@allowed_users(allowed_roles=['customer'])
def userPage(request):
orders = request.user.customer.order_set.all()

total_orders = orders.count()
delivered = orders.filter(status='Delivered').count()
pending = orders.filter(status='Pending').count()

context = {'orders':orders,'total_orders':total_orders,'delivered':delivered,'pending':pending}
return render(request, 'accounts/user.html',context)

@login_required(login_url='login')
@allowed_users(allowed_roles=['customer'])
def accountSettings(request):
customer = request.user.customer
form = CustomerForm(instance=customer)

if request.method == 'POST':
form = CustomerForm(request.POST, request.FILES,instance=customer)
if form.is_valid():
form.save()


context = {'form':form}
return render(request, 'accounts/account_settings.html', context)

@login_required(login_url='login')
@allowed_users(allowed_roles=['admin'])
def product(request):
products = Product.objects.all()
return render(request, 'accounts/products.html', {'products': products})

@login_required(login_url='login')
@allowed_users(allowed_roles=['admin'])
def customer(request,pk_test):
customer = Customer.objects.get(id=pk_test)

orders = customer.order_set.all()
order_count = orders.count()
myFilter = OrderFilter(request.GET, queryset=orders)
orders = myFilter.qs
context = {'customer':customer,'orders':orders,'order_count':order_count,'myFilter':myFilter}

return render(request, 'accounts/customer.html',context)

@login_required(login_url='login')
@allowed_users(allowed_roles=['admin'])
def createOrder(request,pk):
OrderFormSet = inlineformset_factory(Customer,Order, fields=('product', 'status'),extra=10)
customer = Customer.objects.get(id=pk)
#form = OrderForm(initial={'customer':customer})
formset = OrderFormSet(queryset=Order.objects.none(),instance=customer)
if request.method == 'POST':
#print('Printing POST',request.POST)
#form = OrderForm(request.POST)
formset = OrderFormSet(request.POST,instance=customer)
if formset.is_valid():
formset.save()
return redirect('/')



context = {'formset':formset}
return render(request,'accounts/order_form.html',context)


@login_required(login_url='login')
@allowed_users(allowed_roles=['admin'])
def updateOrder(request,pk):
order = Order.objects.get(id=pk)
form = OrderForm(instance=order)
context = {'form':form}
if request.method == 'POST':
#print('Printing POST',request.POST)
form = OrderForm(request.POST,instance=order)
if form.is_valid():
form.save()
return redirect('/')
return render(request,'accounts/order_form.html',context)


@login_required(login_url='login')
@allowed_users(allowed_roles=['admin'])
def deleteOrder(request,pk):
order = Order.objects.get(id=pk)
if request.method == 'POST':
order.delete()
return redirect('/')
context={'item':order}
return render(request, 'accounts/delete.html',context)

Output when i run my local server

这是我的第一个问题,如果我没有提供任何对回答此问题有用的信息,请告诉我

最佳答案

我有同样的错误,我是如何解决的?实际上这是一个愚蠢的错误。首先。转到管理员,转到用户而不是组部分,您会看到您没有为该客户分配任何组。根据您的选择分配客户组或管理组。

这是我的错误,我没有分配任何组。我希望你在那里犯了同样的错误。

关于Django - View accounts.decorators.wrapper_function 没有返回 HttpResponse 对象。它返回 None 而不是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61074171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com