如何使用更新视图? [英] How do I use UpdateView?

查看:34
本文介绍了如何使用更新视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two, presumably related, problems with UpdateView. First, it is not updating the user but creating a new user object. Second, I cannot restrict the fields displayed in the form.

这是我的views.py:

Here is my 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?

How do I properly use UpdateView?

推荐答案

问题 1.用户未更新,因为您使用的是相同的表单(RegistrationForm) 进行更新并创建新用户.

Problem 1. The user is not being updated because you are using the same form (RegistrationForm) to do your updates and to create new users.

问题 2. 表单属于它们自己的名为 forms.py 的文件.我建议的重构:

Problem 2. Forms belong in a file of their own called forms.py. My suggested refactoring:



    #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

slightly elegant 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 的假设.您可以 详细了解如何使用 CBV 处理表单

You left out a lot of info so not really sure what your setup is.My solution is based on the assumption that you have Django 1.5. You can learn more about handling forms with CBVs

这篇关于如何使用更新视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆