一、Django ModelForm: accessing a field's value in the view template
template中,读取modelForm的数据,并显示(而不是录入)
Since you're trying to get this data from a filled out form, I think the easiest way would be to pass the forms .cleaned_data
to the template and use that. Let's say you have a form with fields name
and specialty
, it would look something like this:
def my_view(request): form = my_form(request.GET) if form.is_valid(): ... do search stuff ... search_query = form.cleaned_data return render_to_response('my_template.html', {'search_query': search_query, 'search_results': ...,}, ...)
template
<tr> <th>Name</th> <th>Specialty</td> </tr> <tr> <td>{{ search_query.name }}</td> <td>{{ search_query.specialty }}</th> </tr>
Form has attribute cleaned_data
only if is it valid. If you want to access to value for one of Form's fields you can just write in template:
{{ form.data.%field_name% }}
try the following: {{ form.a.value }}
二、django: taking input and showing output in the same page
(录入和现实在一个页面
三、https://django-extra-views.readthedocs.io/en/latest/pages/formset-views.html#modelformsetview
有空看看extra-views, 有formset的灵活用法。
四、https://micropyramid.com/blog/understanding-djangos-model-formsets-in-detail-and-their-advanced-usage/
了解formset的高级用法
五、formset的参数
https://www.geeksforgeeks.org/django-modelformsets/
https://www.webforefront.com/django/modelformsets.html
Model formset factory
The modelformset_factory() method is the centerpiece to working with model formsets. The modelformset_factory() method can accept up to nineteen arguments, nine of which are identical to standard formsets and the remaining ones specific to model formsets. The following snippet illustrates all the names and default values for each argument in the modelormset_factory() method, with bolded text representing model formset specific options.
modelformset_factory(model, queryset=model.objects.all(),
form=ModelForm,fields=None, exclude=None,
formset=BaseModelFormSet, extra=1, can_order=False, can_delete=False,
max_num=None, min_num=None, validate_max=False, validate_min=False,
widgets=None, localized_fields=None,labels=None,
help_texts=None, error_messages=None,
field_classes=None,formfield_callback=None)
As you can confirm in this snippet, the only required argument (i.e. that doesn't have a default value) for the modelformset_factory() method is model. The meaning for each argument is the following:
model.- Defines the model class on which to create a formset.
queryset.- Defines the queryset to create a formset. By default, all model records are used to create the formset (i.e. the model.objects.all() queryset if used).
fields.- Defines the model form fields to include as part of the model to create the model formset -- just like the fields meta model form option.
exclude.- Defines the model form fields to omit as part of the model to create the model formset -- just like the exclude meta model form option.
widgets.- Defines overriding widgets for the model form to create the model formset -- just like the widgets meta model form option.
localize_fields.- Defines model form fields to localize (i.e. support multiple languages) to create the formset -- just like the localize_fields meta model form option.
labels.- Defines overriding label for the model form to create the model formset -- just like the labels meta model form option.
help_text.- Defines overriding help text for the model form to create the model formset -- just like the help_texts meta model form option.
error_messages.- Defines overriding error messages for the model form to create the model formset -- just like the help_texts meta model form option.
field_classes.- Defines overriding field classes for the model form to create the model formset -- just like the field_classes meta model form option.
formsetfield_callback.- Defines a method to execute prior to creating a form field from a model field. Generally used to customize a model form field -- as described in the earlier model form section -- in the context of a formset.
六、动态增加url
How to add url parameters to Django template url tag?
七、form的render
How to have a link in label of a form field
八、inline nest list in template
Django foreign key relation in template
How to access foreign key in a django template? (ListView)
query foreign key table for list view in django
How to display ForeignKey object in ListView - Django
Display Multiple Queryset in List View
九:template中设置max, min