UI Layout while using Django Form
I am trying to create a django form . Like as follows
class AForm(form.Form):
first_name = forms.CharField(max_length=1000)
last_name = forms.CharField(max_length=1000)
And I do have the html code like
<form action="zzzzzz" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
I want first name and last name next to each other .
Is it po开发者_Python百科ssible to do UI layout inside django form , itself, rather than at html page ?
Thanks J
You can customize form layout by overriding as_p
/ as_table
methods. For example:
class AForm(form.Form):
first_name = forms.CharField(max_length=1000)
last_name = forms.CharField(max_length=1000)
def as_p(self):
return render_to_string('forms/AForm.html', dict(form=self))
Where forms/AForm.html
is a valid Django Template
精彩评论