Save Button Not Working with Attribute Error: 'WSGIRequest' object has no attribute 'project'

Save Button Not Working with Attribute Error: 'WSGIRequest' object has no attribute 'project'

我检查了这里的其他帖子,它们有我的属性错误,但它们似乎是出于不同的原因。我目前正在请求表单中的信息,供用户更新项目页面。然后,如果表单有效,我将保存表单,保存项目,然后尝试 return 重定向到项目页面;但是,当我单击该按钮时,计算机会呈现错误页面。我将附上我的 forms.py、views.py、models.py 和 urls.py:

Views.py 更新部分:

  @wraps(function)
  def wrap(request, *args, **kwargs):
        user = request.user
        name = kwargs.get('name')  
        if uProjects.objects.filter(project=Project.objects.get(name=name), user=user, ifAdmin=True).exists():
             return function(request, *args, **kwargs)
        else:
            return HttpResponseRedirect('/')
  return wrap

@admin_check
def update(request, name):
    project = Project.objects.get(name = name)
    if request.method == "POST":
        pr_form = ProjectUpdateForm(request.POST,
                                    request.FILES,
                                    instance=project)
    #if is_admin in Member == True: #need to authenticate user, access user permissions, if user has permission:
        if pr_form.is_valid():
            pr_form.save()
            messages.success(request, f'This project has been updated.')
           
            request.project.save()
            return redirect('project')
        
    else:
        pr_form = ProjectUpdateForm(instance=project)
    context = {
        'pr_form': pr_form
    }
    return render(request, 'projects/updateproject.html', context)

forms.py 对于 ProjectUpdateForm:

class ProjectUpdateForm(forms.ModelForm):
    class Meta:
        model = Project
        fields=['name', 'department', 'department','bPic', 'logo',
        'department', 'purpose', 'projectTag', 'lookingFor', 'recruiting']

urls.py

from projects import views as p

path('project/<str:name>/', p.project, name='project'),
path('editproject/<str:name>/', p.update, name="editproject"),

谢谢,请让我知道我能做什么。

您的错误在第 request.project.save() 行,请求没有项目属性。

实际上您不需要为项目调用 save() 方法。

因为ProjectUpdateFormModelFormModelForm.save() (Django docs)方法将创建指定模型的新实例或更新分配的实例。


@admin_check
def update(request, name):
    project = Project.objects.get(name = name)
    if request.method == "POST":
        pr_form = ProjectUpdateForm(request.POST,
                                    request.FILES,
                                    instance=project)
    #if is_admin in Member == True: #need to authenticate user, access user permissions, if user has permission:
        if pr_form.is_valid():
            # save() returns an instance object, you can use it to manipulate your object.
            instance = pr_form.save() 
            messages.success(request, f'This project has been updated.')
            # YOUR ERROR IS ⬇️ HERE request doesn't have project attribute
            # request.project.save()
            # redirect with arguments
            return redirect('project', name=instance.name)
    ...

此外,您的重定向必须包含参数 name,因为您的项目 url 需要名称属性:

redirect('project', name=instance.name)