如何设置表单字段值 - django

时间:2021-08-05 19:19:46

I want to show empty form field if there is nothing to show, otherwise a form field with the value inside:

如果没有要显示的内容,我想显示空表单字段,否则显示值为的表单字段:

{% if somevalue %}
  {{form.fieldname}} #<---- how do i set the `somevalue` as value of fieldname here?
{% else %}
  {{form.fieldname}}
{% endif %}

2 个解决方案

#1


13  

In your view do like that, if its CBV

在你看来,如果它的CBV

class YourView(FormView):
    form_class = YourForm

    def get_initial(self):
        # call super if needed
        return {'fieldname': somevalue}

if its generic view, or not FormView you can do like that

如果它的通用视图,或不是FormView,你可以这样做

form = YourForm(initial={'fieldname': somevalue})

#2


8  

There are multiple ways to provide initial data in django form.
At least some of them are:

1) Provide initial data as field argument.

有多种方法可以以django形式提供初始数据。至少其中一些是:1)提供初始数据作为字段参数。

class CityForm(forms.Form):
    location = ModelChoiceField(queryset=City.objects.all(), initial='Munchen')

2) Set it in the init method of the form:

2)在表单的init方法中设置它:

class CityForm(forms.Form):
    location = ModelChoiceField(queryset=City.objects.all())

    def __init__(self, *args, **kwargs):
        super(JobIndexSearchForm, self).__init__(*args, **kwargs)
        self.fields['location'].initial = 'Munchen'

3) Pass a dictionary with initial values when instantiating the form:

3)在实例化表单时传递具有初始值的字典:

#views.py
form = CityForm(initial={'location': 'Munchen'})

In your case, I guess something like this will work..

在你的情况下,我想这样的事情会起作用..

class CityForm(forms.Form):
    location = ModelChoiceField(queryset=City.objects.all())

    def __init__(self, *args, **kwargs):
        super(JobIndexSearchForm, self).__init__(*args, **kwargs)
        if City.objects.all().exists():
            self.fields['location'].initial = ''
        else:
            self.field['location'].initial = City.objects.all()[:1]

That all is just for demonstration, you have to adapt it to your case.

这一切只是为了演示,你必须适应你的情况。

#1


13  

In your view do like that, if its CBV

在你看来,如果它的CBV

class YourView(FormView):
    form_class = YourForm

    def get_initial(self):
        # call super if needed
        return {'fieldname': somevalue}

if its generic view, or not FormView you can do like that

如果它的通用视图,或不是FormView,你可以这样做

form = YourForm(initial={'fieldname': somevalue})

#2


8  

There are multiple ways to provide initial data in django form.
At least some of them are:

1) Provide initial data as field argument.

有多种方法可以以django形式提供初始数据。至少其中一些是:1)提供初始数据作为字段参数。

class CityForm(forms.Form):
    location = ModelChoiceField(queryset=City.objects.all(), initial='Munchen')

2) Set it in the init method of the form:

2)在表单的init方法中设置它:

class CityForm(forms.Form):
    location = ModelChoiceField(queryset=City.objects.all())

    def __init__(self, *args, **kwargs):
        super(JobIndexSearchForm, self).__init__(*args, **kwargs)
        self.fields['location'].initial = 'Munchen'

3) Pass a dictionary with initial values when instantiating the form:

3)在实例化表单时传递具有初始值的字典:

#views.py
form = CityForm(initial={'location': 'Munchen'})

In your case, I guess something like this will work..

在你的情况下,我想这样的事情会起作用..

class CityForm(forms.Form):
    location = ModelChoiceField(queryset=City.objects.all())

    def __init__(self, *args, **kwargs):
        super(JobIndexSearchForm, self).__init__(*args, **kwargs)
        if City.objects.all().exists():
            self.fields['location'].initial = ''
        else:
            self.field['location'].initial = City.objects.all()[:1]

That all is just for demonstration, you have to adapt it to your case.

这一切只是为了演示,你必须适应你的情况。