根据请求更改表单域 [英] Change form fields based on request

查看:185
本文介绍了根据请求更改表单域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序具有可以在会话中设置的类别字段,可能不会。如果是,我不想看到窗体上的字段只是将其作为隐藏字段,其值等于请求中的值。如果没有设置,那么我想显示一个下拉列表。



我已经设置了包含下拉列表的窗体,这是该字段的默认值,而我的问题是哪里是将窗口小部件更改为隐藏的最佳位置,请记住,我需要请求,所以我不能在 init 中显示这个明显的位置。



尝试这种方法,但该字段保持可见:

  class DocForm(forms.ModelForm) :


class Meta:
model = Document
fields = __all__
widgets = {creator:forms.HiddenInput(),}

def __init __(self,* args,** kwargs):
#cant在这里,因为没有请求

class DocAddView(CreateView):


form_class = DocForm


def get_form_class(s​​elf):
form_class = super(DocAddView,self).get_form_class()
form_class.Meta.widgets ['category'] = forms.Hid denInput()
return form_class


解决方案

表单的 __ init __ 方法来获取请求对象。

  class DocForm 

def __init __(self,request,* args,** kwargs):
super(DocForm,self).__ init __(* args,** kwargs)
use_hidden_​​input = do_something_with_request(request)
如果use_hidden_​​input:
self.fields ['category']。widget = forms.HiddenInput()
pre>

然后覆盖 get_form_kwargs ,以便视图将请求传递给表单。

  class DocAddView(CreateView):
...
def get_form_kwargs(self):
#抓住当前的一套表单#kwargs
kwargs = super(DocAddView,self).get_form_kwargs()
#用user_id
kwargs ['request'] = self.request
返回kwargs

这种方法在此博文


The application has a category field that may be set in the session and may not. If it is, I don't want to see the field on the form just have it as a hidden field with the value equal to that in the request. If it's not set then I want to display a dropdown.

I've setup the form to include the dropdown, which is the default for this field and my question is, where is the best place to change the widget to hidden, bearing in mind I need the request so I can't do it in the forms init, which is the obvious place.

Tried this approach, but the field remained visible:

class DocForm(forms.ModelForm):


    class Meta:
        model = Document
        fields = __all__
        widgets = {"creator": forms.HiddenInput(),}

    def __init__(self, *args, **kwargs):
        #cant do it here because don't have request

class DocAddView(CreateView):


    form_class = DocForm


    def get_form_class(self):
        form_class = super(DocAddView, self).get_form_class()
        form_class.Meta.widgets['category'] = forms.HiddenInput()
        return form_class

解决方案

Change your form's __init__ method to take the request object.

class DocForm(forms.ModelForm):
    ...
    def __init__(self, request, *args, **kwargs):
        super(DocForm, self).__init__(*args, **kwargs)
        use_hidden_input = do_something_with_request(request)
        if use_hidden_input:
            self.fields['category'].widget = forms.HiddenInput()

Then override get_form_kwargs, so that the view passes the request to the form.

class DocAddView(CreateView):
    ...
    def get_form_kwargs(self):
        # grab the current set of form #kwargs
        kwargs = super(DocAddView, self).get_form_kwargs()
        # Update the kwargs with the user_id
        kwargs['request'] = self.request
        return kwargs

This approach is explained in this blog post.

这篇关于根据请求更改表单域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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