Reputation: 31
I am trying a form POST using django with django-bootstrap3. The form has those fields: name, email, phone, company, subject, and message. The templates code is as follows.
Question: If I do not want to use all fields of form to generate tempate, such as only use name, email and phone, how to implement? I guess I can filter the form but I do not know how to do.
{% load bootstrap3 %}
{% block contact-page %}
<div class="container">
<div class="center">
<h2>Drop Your Message</h2>
<p class="lead">We are looking forward to hearing from you.</p>
</div>
<div class="row contact-wrap">
<div class="status alert alert-success" style="display: none"></div>
<form action="" method="post" class="form">
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">
{% bootstrap_icon "star" %} Submit
</button>
{% endbuttons %}
</form>
</div><!--/.row-->
</div><!--/.container-->
{% endblock %}}
Upvotes: 3
Views: 3520
Reputation: 3389
I can understand what you need. You have to add name, email and phone
.
Just imagine, If you use name, email, phone, company, subject, and message
django-bootstrap3 provide an option to add fields separately using bootstrap_field
.
#models.py
class CustomUser(models.Model):
name = models.CharField(max_length=100)
email = models.CharField(max_length=100)
phone = models.CharField(max_length=100)
company = models.EmailField(max_length=150)
subject = models.CharField(max_length=100)
message = models.CharField(max_length=100)
# template.html
{% load bootstrap3 %}
<form action="" role="form" method="post">{% csrf_token %}
{% bootstrap_field form.name placeholder='Name' %}
{% bootstrap_field form.email placeholder='Email' %}
{% bootstrap_field form.phone placeholder='Phone' %}
{% buttons submit='OK' reset="Cancel" %}{% endbuttons %}
</form>
I think you got the point.
Upvotes: 2
Reputation: 31
I got the answer. Thank you Tim.
{% load bootstrap3 %}
{% block contact-page %}
<div class="container">
<div class="center">
<h2>Drop Your Message</h2>
<p class="lead">We are looking forward to hearing from you.</p>
</div>
<div class="row contact-wrap">
<div class="status alert alert-success" style="display: none"></div>
<form action="" method="post" class="form">
<!--{% bootstrap_form form %}-->
<div class="col-sm-5 col-sm-offset-1">
{% for field in form %}
{% if field.name != 'subject' and field.name != 'message' %}
{% bootstrap_field field %}
{% endif %}
{% endfor %}
</div>
<div class="col-sm-5">
{% for field in form %}
{% if field.name == 'subject' or field.name == 'message' %}
{% bootstrap_field field %}
{% endif %}
{% endfor %}
{% buttons %}
<button type="submit" class="btn btn-primary">
{% bootstrap_icon "star" %} Submit
</button>
{% endbuttons %}
</div>
</form>
</div><!--/.row-->
</div><!--/.container-->
{% endblock %}}
Upvotes: 0
Reputation: 1357
With django-bootstrap3 you can use the {% bootstrap_field form.fieldname %}
tag to render specific fields only. This could, however, lead to an error, if some of the omitted fields are required, as the form receives omitted fields as empty but still checks them to be valid.
The cleaner method would be to omit these fields in your form. Either by using the Meta.fields
parameter in a ModelForm
or by removing the fields from a regular Form
.
Upvotes: 0