【发布时间】:2017-04-26 01:52:36
【问题描述】:
我正在尝试使用 Django 为我的用户创建用户配置文件。总体而言,它似乎大部分正常工作,我能够在管理页面中看到一切正确。在我的实际 HTML 页面上,我正确地看到了我需要的模型字段,但是它们没有填充任何数据,而且我输入的数据实际上不会保存,即使它说它确实保存了。
views.py
class DemoUserEditView(UpdateView):
form_class = DemoUserEditForm
template_name = "user/profile.html"
view_name = 'account_profile'
success_url = '/member/'
def get_object(self):
return self.request.user
def form_valid(self, form):
form.save()
messages.add_message(self.request, messages.INFO, 'User profile updated')
return super(DemoUserEditView, self).form_valid(form)
account_profile = login_required(DemoUserEditView.as_view())
models.py.
class UserProfile(models.Model):
user = models.OneToOneField(User, primary_key=True, on_delete=models.CASCADE)
avatar_url = models.CharField(max_length=256, blank=True, null=True)
skills = models.CharField(max_length=256, blank=True, null=True)
avatarpic = models.ImageField(_('avatar photo'),
blank=True, null=True,
upload_to=user_directory_path, validators=[validate_img_extension])
Bio = models.TextField(_('Bio'),
max_length=200, blank=True, null=True, unique=False)
EDUCATION_CHOICES = (
('0', "Didn't complete High School"),
('1', 'High School or GED'),
('2', 'Associate Degree'),
('3', 'Batchlors Degree'),
('4', 'Masters Degree'),
('5', 'PhD Degree'),
('6', 'Professional Degree'),
)
Education = models.CharField(_('Education'),
max_length=100, blank=True, null=False,
choices=EDUCATION_CHOICES, unique=False,
help_text="Level of Education")
urls.py
url(r'^accounts/', include('allauth.urls')),
url(r'^accounts/profile/$', 'base.views.account_profile', name='account_profile'),
我正在尝试使用通用视图,我认为 UpdateView 将是合适的标签。我已经阅读了Generic View docs 以及几个this stackoverflow 问题和this one。我的大问题是我不再收到错误消息,它说表单有效并且正在保存,但事实并非如此,我不确定还有什么可以尝试的。
那么如何使用现有的用户数据填充我的模型表单?
【问题讨论】:
标签: python django model modelform django-generic-views