Django 如何使用UpdateView?
我对UpdateView有两个问题,大概是相关的。首先,它不是更新用户,而是创建一个新的用户对象。其次,我不能限制表单中显示的字段 以下是我的视图.py:Django 如何使用UpdateView?,django,django-forms,django-views,django-class-based-views,Django,Django Forms,Django Views,Django Class Based Views,我对UpdateView有两个问题,大概是相关的。首先,它不是更新用户,而是创建一个新的用户对象。其次,我不能限制表单中显示的字段 以下是我的视图.py: class RegistrationView(FormView): form_class = RegistrationForm template_name = "register.html" success_url = "/accounts/profile/" def form_valid(self, f
class RegistrationView(FormView):
form_class = RegistrationForm
template_name = "register.html"
success_url = "/accounts/profile/"
def form_valid(self, form):
if form.is_valid:
user = form.save()
user = authenticate(username=user.username, password=form.cleaned_data['password1'])
login(self.request, user)
return super(RegistrationView, self).form_valid(form) #I still have no idea what this is
class UserUpdate(UpdateView):
model = User
form_class = RegistrationForm
fields = ['username', 'first_name']
template_name = "update.html"
success_url = "/accounts/profile/"
和url.py
url(r'^create/$', RegistrationView.as_view(), name="create-user"),
url(r'^profile/(?P<pk>\d+)/edit/$', UserUpdate.as_view(), name="user-update"),
url(r'^create/$,RegistrationView.as_view(),name=“create user”),
url(r“^profile/(?P\d+)/edit/$”,UserUpdate.as_view(),name=“user update”),
如何正确使用UpdateView?首先:user=form.save()
将表单保存在数据库中。因为表单中没有pk,所以它创建了一个新的pk。
你需要做的可能是检查是否存在使用该用户名的用户,如果不存在,则创建该用户(这部分请检查谷歌)
第二:要限制字段,您必须在表单的Meta
类(此处未显示)中指定它们。请选中此项。问题1。
用户未被更新,因为您使用的是同一表单
(RegistrationForm)进行更新并创建新用户
问题2。表单属于它们自己的文件Forms.py。
我建议的重构:
#forms.py
#place(forms.py) this in the same directory as views.py
class UpdateForm(forms.ModelForm):
#form for updating users
#the field you want to use should already be defined in the model
#so no need to add them here again DRY
class Meta:
model = User
fields = ('field1', 'field2', 'field3',)
#views.py
#import your forms
from .forms import UpdateForm
#also import your CBVs
from django.views.generic import UpdateView
class UserUpdate(UpdateView):
context_object_name = 'variable_used_in `update.html`'
form_class = UpdateForm
template_name = 'update.html'
success_url = 'success_url'
#get object
def get_object(self, queryset=None):
return self.request.user
#override form_valid method
def form_valid(self, form):
#save cleaned post data
clean = form.cleaned_data
context = {}
self.object = context.save(clean)
return super(UserUpdate, self).form_valid(form)
稍微优雅的URL.py
#urls.py
#i'm assuming login is required to perform edit function
#in that case, we don't need to pass the 'id' in the url.
#we can just get the user instance
url(
regex=r'^profile/edit$',
view= UserUpdate.as_view(),
name='user-update'
),
你遗漏了很多信息,所以不确定你的设置是什么。我的解决方案是基于假设你有Django 1.5。如果要在数据库中获取新对象而不是更新现有对象,则可能是复制并粘贴了新对象的模板,而忘记了更改表单的“操作”属性,因此可以使用CBV处理表单。这应该指向以硬编码路径或URL标记的形式进行更新的视图(
{%URL''object.id%
)。这并不是严格相关的,但请检查这一点:还有几点:一旦调用form\u valid()
方法,就不需要检查表单是否有效。这是在post()中执行的
method(请参阅:)。form\u valid()
方法的最后一行确保我们在RegistrationView的父级(UpdateView)中正确调用form\u valid()
。很抱歉,我意外删除了此答案。请告诉我它是否有用或有帮助。我认为这是错误的。使用相同的表单应该没有任何效果。请参见下面的我的答案。@Staccato您可以编写代码,通过重写post方法来更新对象吗?我已尝试通过重写post来实现此功能,但它会创建新的对象公元Thanks@MikeAono我不太确定我是否理解您的要求,但如果您创建一个新问题并发布代码,我将很乐意帮助您