Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter the form in django template

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 %}}
like image 301
Frank Tang Avatar asked Feb 03 '26 09:02

Frank Tang


1 Answers

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.

like image 110
Arun V Jose Avatar answered Feb 05 '26 22:02

Arun V Jose