如何使用UpdateView? [英] How do I use UpdateView?

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

问题描述

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



这是我的views.py:

  class RegistrationView(FormView )
form_class = RegistrationForm
template_name =register.html
success_url =/ accounts / profile /

def form_valid(self,form):
如果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)#我仍然不知道这是

class UserUpdate(UpdateView):
model =用户
form_class = RegistrationForm
fields = ['username','first_name']
template_name =update.html
success_url =/ accounts / profile /

和urls.py

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

如何正确使用UpdateView?先谢谢了。

解决方案

问题1.
用户没有被更新,因为您使用相同的形式
RegistrationForm)来进行更新并创建新的用户。



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

  

#forms.py
#place (forms.py)这在与目录相同的目录中,如下所示:


class UpdateForm(forms.ModelForm):
#form更新用户
#要使用的字段应该已经在模型中定义
#so不需要再次添加它们DRY
class Meta:
model = User
fields =('field1','field2' 'field3',)

#views.py
#导入您的表单
from .forms import UpdateForm
#also从django导入您的CBV
。 view.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对象
def get_object(self,queryset = None):
return self.request.user

#override form_valid方法
def form_valid(self,form):
#save clean post data
clean = form.cleaned_data
context = {}
self.object = context.save(clean)
return super(UserUpdate,self).form_valid(form)

略优雅的urls.py

  

#urls.py
#我假设登录是执行编辑功能
#在这种情况下,我们不需要在URL中传递'id'。
#we可以得到用户实例
url(
regex = r'^ profile / edit $',
view = UserUpdate.as_view(),
name ='user-update'
),

你省了很多的信息,所以不太确定你的设置是什么。我的解决方案是基于你有Django 1.5的假设。您可以了解更多关于使用CBV处理表单的信息


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.

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

and urls.py

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

How do I properly use UpdateView? Thanks in advanced.

解决方案

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.

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)    

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'
    ),

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

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

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