简体   繁体   English

保存按钮不适用于属性错误:“WSGIRequest”object 没有属性“项目”

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

I checked the other posts on here that have the attribute error that I have, but they seem to be for different reasons.我检查了这里的其他帖子,这些帖子有我所拥有的属性错误,但它们似乎是出于不同的原因。 I am currently requesting the information from a form for users to update a project page.我目前正在从表单中请求用户更新项目页面的信息。 Then, if the form is valid, I am saving the form, saving the project, then trying to return redirect to the project page;然后,如果表单有效,我将保存表单,保存项目,然后尝试将重定向返回到项目页面; however, when I click the button, the computer renders the error page.但是,当我单击按钮时,计算机会呈现错误页面。 I will attach my forms.py, views.py, models.py, and urls.py:我将附上我的 forms.py、views.py、models.py 和 urls.py:

Views.py for the update section:更新部分的 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 for ProjectUpdateForm: forms.py 用于 ProjectUpdateForm:

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

urls.py网址.py

from projects import views as p

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

Thanks, please let me know what I can do.谢谢,请让我知道我能做什么。

Your error is in line request.project.save() , request doesn't have project attribute.您的错误在request.project.save()行中,请求没有项目属性。

And actually you don't need to call save() method for project.实际上你不需要为项目调用save()方法。

Because ProjectUpdateForm is the ModelForm and ModelForm.save() ( Django docs ) method will create a new instance of the specified model or update assigned instance.因为ProjectUpdateFormModelFormModelForm.save() ( Django docs ) 方法将创建指定 model 的新实例或更新分配的实例。


@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)
    ...

Also your redirect must contain argument name , because your project url required name attribute:此外,您的重定向必须包含参数name ,因为您的项目 url 需要 name 属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM