不要将当前值放在表格中 [英] Don't put the current value in the form

查看:74
本文介绍了不要将当前值放在表格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

面对这样的问题,关于用户的当前值未传递到表单。我要做的是将用户模型和概要文件模型结合起来,然后以两种形式显示当前用户数据以进行编辑。似乎正确完成了该操作,但是由于某些原因,该操作不起作用。请告诉我谁知道这件事)

Faced such a problem that the current values ​​about the user are not passed to the form. What I want to do is to combine the user model and the profile model, then display the current user data in two forms for editing. It seems to be done correctly, but for some reason it does not work. Please tell me who understands this)

文件 views.py

class ProfilePage(UpdateView):
        model=Profile
        template_name="market/profile.html"
        form_class=ProfileUpdateForm
        second_model = User
        second_form_class= UserUpdateForm
    
    
        def get_context_data(self, **kwargs):
            context = super(ProfilePage,self).get_context_data(**kwargs)
            context['UserUpdateForm'] = UserUpdateForm(
                prefix='UserUpdateForm', 
                data = self.request.POST,
                instance=self.request.user,
            )
            context['ProfileUpdateForm'] = ProfileUpdateForm(
                prefix='ProfileUpdateForm', 
                data = self.request.POST,
                files = self.request.FILES,
                instance=self.request.user.profile,
            )
            return context
    
        def post(self, request, *args, **kwargs):
            super(ProfilePage,self).post(self, request, *args, **kwargs)
            context = self.get_context_data(**kwargs)
            if context['UserUpdateForm'].is_valid():
                context['UserUpdateForm'].save()
            elif context['ProfileUpdateForm'].is_valid():
                context['ProfileUpdateForm'].save()
            return self.render_to_response(context)

文件 forms.py

class UserUpdateForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ("username",)


class ProfileUpdateForm(forms.ModelForm):
    class Meta:
        model=Profile
        fields=('valuta','slug','avtor')

文件 models.py

class Profile(models.Model):
    user=models.OneToOneField(User,on_delete=models.CASCADE)
    avtor=models.ImageField(default='user_images/default.jpg', upload_to='user_images',blank=True)
    slug=models.SlugField(max_length=50, unique=True,blank=True)
    valuta=models.ForeignKey(Valuta,default="5", on_delete=models.CASCADE)

文件 profile.html

<form method="POST">
    {% csrf_token %}
    {{UserUpdateForm.as_p}}
    <button type="submit">send</button>
</form>
<form action="" enctype="multipart/form-data" method="POST">
    {% csrf_token %}
    {{ProfileUpdateForm.as_p}}
    <button type="submit">send</button>
</form>


推荐答案

在<$ c $中创建表单时c> get_context_data ,您还传递了 self.request.POST 。因此,它尝试使用POST数据呈现表单(起初它是空的)。

When you create your forms in your get_context_data, you also pass self.request.POST. So it tries to render the form with the POST data (whereas it is empty at first).

您应该为或无添加这行用于UserUpdateForm和ProfileUpdateForm:

You should add or None for this line for UserUpdateForm and ProfileUpdateForm :

data = self.request.POST or None,
            

并且对于使用文件的ProfileUpdateForm:

And also for the ProfileUpdateForm where it uses files :

files = self.request.FILES or None,

然后它将呈现表单使用实例数据,并在存在时使用POST数据。

Then it will render the form with the instance data and use POST data when it exists.

这篇关于不要将当前值放在表格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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