Django报错:'UserView'对象无'object'属性,表单提交失败
Hey there, let's dig into that 'UserView' object has no attribute 'object' error you're hitting when trying to submit your Django form to the database. Based on the code you shared (and the missing bits), here's a breakdown of what's going wrong and how to fix it:
This error almost always pops up with Django's class-based views (like CreateView or UpdateView) when the view tries to access self.object before it's been initialized. For CreateView, self.object starts as None and only gets set to a new model instance once the form is valid and saved. If your UserView is trying to access this attribute too early (or isn't set up to initialize it at all), you'll get this error.
forms.py First, your forms.py is cut off at from dj—you need a proper ModelForm tied to your UserInfo model to handle form submission. Here's a complete version you can use:
from django import forms from .models import UserInfo class UserInfoForm(forms.ModelForm): class Meta: model = UserInfo # Include all fields from your model (or list specific ones if you want to exclude some) fields = '__all__' # Optional: Add custom widgets or validation here if needed
Since your model has FileField fields (adherence_agreement and photo_scan), we'll need to remember to add enctype="multipart/form-data" to your form template later (more on that below).
UserView Implementation You didn't share your views.py code, but this is almost certainly where the issue lies. Let's assume you're using CreateView (the right choice for submitting new data to the database)—here's a proper implementation that avoids the object attribute error:
from django.views.generic.edit import CreateView from django.urls import reverse_lazy from .models import UserInfo from .forms import UserInfoForm class UserView(CreateView): model = UserInfo form_class = UserInfoForm # Redirect to a success page after form submission success_url = reverse_lazy('success-page') # Replace with your actual success URL name def get_context_data(self, **kwargs): # Always call the parent method FIRST to initialize self.object context = super().get_context_data(**kwargs) # Now you can safely access self.object (it'll be None for CreateView initially) context['page_title'] = "Submit Your User Profile" return context
Common Mistakes to Avoid:
- Accessing
self.objectbefore callingsuper(): If you customize methods likeget_context_dataorget, never accessself.objectuntil after you've called the parent class's method. - Forgetting to set
modelorform_class: These are required for generic views likeCreateViewto know which model and form to work with. - Skipping
super().form_valid(form): If you overrideform_valid, make sure to call the parent method—this is what setsself.objectto the newly saved instance.
Since you're dealing with file uploads, your form template must include enctype="multipart/form-data" to handle the file fields correctly. Here's a basic working template example:
<form method="post" enctype="multipart/form-data"> {% csrf_token %} <!-- Render form fields neatly --> {{ form.as_p }} <button type="submit">Submit Profile</button> </form>
If your template tries to access {{ form.instance.some_field }} before the view initializes self.object, that can also trigger the same error—stick to standard form rendering unless you're sure the instance is ready.
- Did you finish writing
forms.pywith a validModelForm? - Does
UserViewinherit from the correct generic view (likeCreateView)? - Are you calling
super()in custom view methods before accessingself.object? - Does your form template include
enctype="multipart/form-data"?
Once you fix these pieces, the 'UserView' object has no attribute 'object' error should disappear, and your form should submit data to the database as expected.
内容的提问来源于stack exchange,提问作者John Dawson

