【问题标题】:How do I use UpdateView?如何使用更新视图?
【发布时间】:2013-08-01 20:50:13
【问题描述】:

我有两个可能相关的 UpdateView 问题。首先,它不是更新用户,而是创建一个新的用户对象。其次,我不能限制表单中显示的字段。

这是我的views.py:

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/" 

和 urls.py

url(r'^create/$', RegistrationView.as_view(), name="create-user"), 
url(r'^profile/(?P<pk>\d+)/edit/$', UserUpdate.as_view(), name="user-update"), 

如何正确使用 UpdateView?

【问题讨论】:

标签: django django-forms django-views django-class-based-views


【解决方案1】:

firstuser = form.save() 保存在 db 中的表格。因为形式中没有 pk,所以它会创建一个新的。 您要做的可能是检查具有该用户名的用户是否存在,如果不创建它(这部分检查谷歌)。

second:要限制字段,您必须在表单的 Meta 类中指定它们(您没有在此处显示)检查此 https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#modelform

【讨论】:

    【解决方案2】:

    问题 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)    
    
    

    略显优雅的 urls.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 的假设。您可以learn more 了解如何使用 CBV 处理表单

    【讨论】:

    • 对不起,我不小心删除了这个答案。让我知道它是否有用或有帮助。
    • 我不认为这是正确的。使用相同的表格应该没有效果。请参阅下面的答案。
    • @Staccato 你能否编写代码来通过覆盖 post 方法来更新对象?我已经尝试通过覆盖 post 来实现这一点,但它会创建新对象。谢谢
    • @MikeAono 我不太确定我明白你在问什么,但如果你创建一个新问题并发布你的代码,我很乐意为你提供帮助
    【解决方案3】:

    如果您在数据库中获取新对象而不是更新现有对象,则很可能您复制并粘贴了新对象的模板而忘记更改表单的操作属性。这应该指向以硬编码路径或 URL 标记 ({% url '&lt;name of URLconf&gt;' object.id %) 的形式进行更新。

    【讨论】:

      猜你喜欢
      • 2021-04-27
      • 1970-01-01
      • 2017-02-27
      • 2015-05-13
      • 1970-01-01
      • 1970-01-01
      • 2019-12-07
      • 2023-03-22
      • 2019-11-04
      相关资源
      最近更新 更多