如何将表单控件添加到 Django 表单? [英] How to add form control to a Django Form?
问题描述
我正在尝试将表单控件添加到我为 Django 制作的表单应用程序中.我已经创建了应用程序并想将表单添加到引导程序模板中,但是我不知道如何为我的电子邮件字段添加引导程序的更时尚的文本框.
我想最终得到类似于此引导程序模板角落中的登录字段(尽管没有密码字段):http://getbootstrap.com/examples/jumbotron/
我的 signup.html 文件中的代码如下所示:
{% 扩展 'base.html' %}{% 块内容 %}<div class="col-sm-12"><form method='POST' action=''>{% csrf_token %}{{ form.as_p }}<input type='submit' class='btn btn-success btn-lg'></表单>
{% 结束块 %}
上面引导程序站点的代码如下所示(为了清楚起见,我去掉了密码部分):我不知道如何将 django 应用程序集成到此代码中,以便该站点将注册发布到我的数据库中.
<button type="submit" class="btn btn-success">登录</button></表单>
我如何整合两者?提前致谢.
作为参考,例如,如果您正在使用 ModelForm
,您可以只覆盖 init
方法像这样的表格:
def __init__(self, *args, **kwargs):super(MemberForm, self).__init__(*args, **kwargs)对于 field_name,self.fields.items() 中的字段:field.widget.attrs['class'] = 'form-control'
I'm try to add form control to a form app I made for Django. I have created the app already and want to add the form into a bootstrap template, however I don't know how to add bootstrap's sleeker text-box for my email field.
I would like to end up with something like the sign-in field found in the corner of this bootstrap template (albeit without the password field): http://getbootstrap.com/examples/jumbotron/
My code in my signup.html file looks like this:
{% extends 'base.html' %}
{% block content %}
<div class="col-sm-12">
<form method='POST' action=''> {% csrf_token %}
{{ form.as_p }}
<input type='submit' class='btn btn-success btn-lg'>
</form>
</div>
{% endblock %}
The code for the bootstrap site above looks like this (I got rid of the password part for clarity): I don't know how to integrate the django app into this code so that the site will post signups to my database.
<form class="navbar-form navbar-right" role="form">
<div class="form-group">
<input type="text" placeholder="Email" class="form-control">
</div>
<button type="submit" class="btn btn-success">Sign in</button>
</form>
How do I integrate the two? Thanks in advance.
For reference, if you're for example using a ModelForm
you can just override the init
method of your form like this:
def __init__(self, *args, **kwargs):
super(MemberForm, self).__init__(*args, **kwargs)
for field_name, field in self.fields.items():
field.widget.attrs['class'] = 'form-control'
这篇关于如何将表单控件添加到 Django 表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!