简体   繁体   中英

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:

Views.py for the update section:

  @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:

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"),

Thanks, please let me know what I can do.

Your error is in line request.project.save() , request doesn't have project attribute.

And actually you don't need to call save() method for project.

Because ProjectUpdateForm is the ModelForm and ModelForm.save() ( Django docs ) method will create a new instance of the specified model or update assigned instance.


@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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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