【发布时间】:2021-05-03 02:43:10
【问题描述】:
我正在尝试在 Django 3.1 中开发个人资料编辑页面。我对此很陌生,所以如果有些东西很时髦,那就这么说吧。我从Django edit user profile复制了一些内容
Views.py
from myapp.forms import UserForm, UserProfileInfoForm
from django.views.generic import (View, TemplateView, UpdateView)
from myapp.models import UserProfileInfo
class UpdateProfile(UpdateView):
model = UserProfileInfoForm
fields = ['first_name', 'last_name', 'email', 'phone', 'title', 'password']
template_name = 'profile.html'
slug_field = 'username'
slug_url_kwarg = 'slug'
models.py
class UserProfileInfo(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
phone = PhoneField(blank=True, help_text='Contact phone number')
title = models.CharField(max_length=255)
system_no = models.CharField(max_length=9)
def __str__(self):
return self.user.username
forms.py
#...
from myapp.models import UserProfileInfo
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
class Meta():
model = User
fields = ('username', 'email', 'password', 'first_name', 'last_name')
class UserProfileInfoForm(forms.ModelForm):
class Meta():
model = UserProfileInfo
fields = ('phone', 'title', 'system_no')
myapp/urls.py
from django.urls import path
from . import views
app_name = 'myapp'
urlpatterns = [
path('', views.index,name='index'),
path('about/',views.AboutView.as_view(),name='about'),
path('register/', views.register,name='register'),
path('profile/', views.UpdateProfile.as_view(), name='profile'),
]
我有一个仅在登录后显示的链接: base.html(仅限href)
<a class="nav-link active" href="{% url 'myapp:profile' %}">Profile</a>
你能告诉我要解决什么问题吗?单击上面的链接后,我在浏览器中收到一条错误消息
/profile/ 处的属性错误
类型对象“UserProfileInfoForm”没有属性“_default_manager”
【问题讨论】: