Django/Bootstrap template rendering

2019-09-01 08:06发布

问题:

I'm currently coding a website using Django and Bootstrap. I created the templates and models first, and now, I'm implementing the controllers. All is not implemented yet, but I needed some help on this. I was wondering how to render a Django authentication form with the Boostrap grid system. I'm using Boostrap 4 and Django 2.0.4.

My older form was like this :

<div class="jumbotron">
    <form class="form-horizontal" method="post" action="{% url 'login' %}">
        {% csrf_token %}
        <div class="form-group">
            <div class="col-lg-4 offset-lg-4">
                <label class="control-label" for="usernameInput">{{ form.username.label_tag }}</label>
                <input id="usernameInput" type="text" name="{{ form.field.html_name }}" value="{{ form.username }}" class="form-control"
                       placeholder="Username">
            </div>
        </div>

        <div class="form-group">
            <div class="col-lg-4 offset-lg-4">
                <label class="control-label" for="passwordInput">{{ form.password.label_tag }}</label>
                <input id="passwordInput" type="password" name="{{ form.field.html_name }}" value="{{ form.field.value }}" class="form-control"
                       placeholder="Password">
            </div>
        </div>

        <div class="container" id="btn_login">
            <button type="submit" class="btn btn-primary btn-md" value="login" role="button">Log in</button>
            <input type="hidden" name="next" value="{{ next }}"/>
        </div>
    </form>

    <span class="container" id="forgotten_password">
        <a href="">Forgot your password ?</a>
    </span>
</div>


And here is the new one :

<div class="jumbotron">
    {% load widget_tweaks %}

    <form class="form-horizontal" method="post" action="{% url 'login' %}">
        {% csrf_token %}

        {% for hidden_field in form.hidden_fields %}
            {{ hidden_field }}
        {% endfor %}

        {% if form.non_field_errors %}
            <div class="alert alert-danger" role="alert">
                {% for error in form.non_field_errors %}
                    {{ error }}
                {% endfor %}
            </div>
        {% endif %}

        {% for field in form.visible_fields %}
            <div class="form-group">
                {{ field.label_tag }}

                {% if form.is_bound %}
                    {% if field.errors %}
                        {% render_field field class="form-control" %}
                        {% for error in field.errors %}
                            <div class="invalid-feedback">
                                {{ error }}
                            </div>
                        {% endfor %}
                    {% else %}
                        {% render_field field class="form-control" %}
                    {% endif %}
                {% else %}
                    {% render_field field class="form-control" %}
                {% endif %}

                {% if field.help_text %}
                    <small class="form-text text-muted">{{ field.help_text }}</small>
                {% endif %}
            </div>
        {% endfor %}

        <div class="container" id="btn_login">
            <button type="submit" class="btn btn-primary btn-md" role="button">Log in</button>
        </div>
    </form>

    <span class="container" id="forgotten_password">
        <a href="">Forgot your password ?</a>
    </span>
</div>

But as you can obviously tell, this is not rendering the same way. For example, I'd like to take back the width of the input.

For the rest, I use this line in my urls.py
re_path(r'^login/$', auth_views.login, {'template_name': 'main/login.html'}, name='login'),

And this one in my settings.py to get redirected to the right page :
LOGIN_REDIRECT_URL = 'my_pls'

I googled a lot and finally used this link (in case you case notice something I didn't understand) : https://simpleisbetterthancomplex.com/article/2017/08/19/how-to-render-django-form-manually.html#understanding-the-rendering-process

回答1:

You should use custom HTML attributes on your **forms.py**.

It's simple:

from django import forms

class your_form(forms.Form):
    attrs_for_the_field = {
        'class': 'form-control',
        'placeholder': 'Write here!',
    }

    field = forms.CharField(widget=forms.CharField(attrs=attrs_for_the_field))


With this code you will render the following HTML:

<input type="text" name="field" class="form-control"  placeholder="Write here!" id="id_field">


Take a look at https://docs.djangoproject.com/en/2.0/ref/forms/widgets/ in order to know how Django represents an HTML input element.

You also should read https://docs.djangoproject.com/en/2.0/ref/forms/fields/ so that you could understand how it works.



回答2:

You can add all the HTML configuration for the fields in the form code in forms.py . Than the form can be displayed with just {{ form.as_p}}. Width of input can be retrieved with jQuery or JavaScript .