Django - 分别打印每个 model/form
Django - Print each model/form out separately
我使用 {{form}} 打印出整个表格。我想知道有什么办法可以分别打印每个元素吗?我尝试了 {{survey.location}} 或 {{survey.get_location_display}} 但它不起作用。下面可能是 models.py、forms.py、views.py 和 home.html。
models.py
from django.db import models
class Survey(models.Model):
location = models.CharField(max_length=120, blank=False, null=True)
education = models.CharField(max_length=120, blank=False, null=True)
forms.py
from django import forms
from .models import Survey
class Meta:
model = Survey
fields = ["location", "education"]
views.py
from django.shortcuts import render
from .forms import SurveyForm
def homepage(request):
if request.method == 'POST':
form = SurveyForm(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
instance.save()
else:
form = SurveyForm()
return render(request, "home.html", {"form": form})
home.html
<form method='POST' action= "" >{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Submit"/>
</form>
您可以使用{{form.field_name}}
单独打印表格的每个字段。
上的 Django 文档
Each field is available as an attribute of the form using {{
form.name_of_field }}
, and in a Django template, will be rendered
appropriately.
{{form.location}} # renders 'location' field of a form
{{form.education}} # renders 'education' field of a form
模板中的 {{form.location}}
将呈现如下内容:
<input type="text" name="location" maxlength="120" ../>
是的,这是 docs 的一种方式:
<form action="/contact/" method="post">
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.subject.errors }}
<label for="id_subject">Email subject:</label>
{{ form.subject }}
</div>
<div class="fieldWrapper">
{{ form.message.errors }}
<label for="id_message">Your message:</label>
{{ form.message }}
</div>
<div class="fieldWrapper">
{{ form.sender.errors }}
<label for="id_sender">Your email address:</label>
{{ form.sender }}
</div>
<div class="fieldWrapper">
{{ form.cc_myself.errors }}
<label for="id_cc_myself">CC yourself?</label>
{{ form.cc_myself }}
</div>
<p><input type="submit" value="Send message" /></p>
</form>
我使用 {{form}} 打印出整个表格。我想知道有什么办法可以分别打印每个元素吗?我尝试了 {{survey.location}} 或 {{survey.get_location_display}} 但它不起作用。下面可能是 models.py、forms.py、views.py 和 home.html。
models.py
from django.db import models
class Survey(models.Model):
location = models.CharField(max_length=120, blank=False, null=True)
education = models.CharField(max_length=120, blank=False, null=True)
forms.py
from django import forms
from .models import Survey
class Meta:
model = Survey
fields = ["location", "education"]
views.py
from django.shortcuts import render
from .forms import SurveyForm
def homepage(request):
if request.method == 'POST':
form = SurveyForm(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
instance.save()
else:
form = SurveyForm()
return render(request, "home.html", {"form": form})
home.html
<form method='POST' action= "" >{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Submit"/>
</form>
您可以使用{{form.field_name}}
单独打印表格的每个字段。
Each field is available as an attribute of the form using
{{ form.name_of_field }}
, and in a Django template, will be rendered appropriately.
{{form.location}} # renders 'location' field of a form
{{form.education}} # renders 'education' field of a form
模板中的 {{form.location}}
将呈现如下内容:
<input type="text" name="location" maxlength="120" ../>
是的,这是 docs 的一种方式:
<form action="/contact/" method="post">
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.subject.errors }}
<label for="id_subject">Email subject:</label>
{{ form.subject }}
</div>
<div class="fieldWrapper">
{{ form.message.errors }}
<label for="id_message">Your message:</label>
{{ form.message }}
</div>
<div class="fieldWrapper">
{{ form.sender.errors }}
<label for="id_sender">Your email address:</label>
{{ form.sender }}
</div>
<div class="fieldWrapper">
{{ form.cc_myself.errors }}
<label for="id_cc_myself">CC yourself?</label>
{{ form.cc_myself }}
</div>
<p><input type="submit" value="Send message" /></p>
</form>