开发者

UI Layout while using Django Form

开发者 https://www.devze.com 2023-03-20 03:42 出处:网络
I am trying to create a django form . Like as follows class AForm(form.Form): first_name = forms.CharField(max_length=1000)

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

精彩评论

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