开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜