Render Template in Django

unsplash

在网页设计中,要为元素制定class,id等元素,方便css做呈现。使用django forms去生成表单时,如何为form表单的每个field设置相应的元素呢?

在form里添加attrs

参考

1
2
3
4
class CommentForm(forms.Form):
name = forms.CharField(widget=forms.TextInput(attrs={'class': 'special'}))
url = forms.URLField()
comment = forms.CharField(widget=forms.TextInput(attrs={'size': '40'}))

使用django-widget-tweaks

Django-widget-tweaks

安装

安装命令:

1
pip install django-widget-tweaks

并添加到settings文件里:

1
2
3
4
5
INSTALLED_APPS = [
...
'widget_tweaks',
...
]

用法

1
2
3
4
5
6
7
8
9
10
11
12
13
{% load widget_tweaks %}

<!-- change input type (e.g. to HTML5) -->
{% render_field form.search_query type="search" %}

<!-- add/change several attributes -->
{% render_field form.text rows="20" cols="20" title="Hello, world!" %}

<!-- append to an attribute -->
{% render_field form.title class+="css_class_1 css_class_2" %}

<!-- template variables can be used as attribute values -->
{% render_field form.text placeholder=form.text.label %}

实例

参照

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{% extends 'base.html' %}

{% load widget_tweaks %}

{% block title %}Start a New Topic{% endblock %}

{% block breadcrumb %}
<li class="breadcrumb-item"><a href="{% url 'home' %}">Boards</a></li>
<li class="breadcrumb-item"><a href="{% url 'board_topics' board.pk %}">{{ board.name }}</a></li>
<li class="breadcrumb-item active">New topic</li>
{% endblock %}

{% block content %}
<form method="post" novalidate>
{% csrf_token %}

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

{% render_field field class="form-control" %}

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

<button type="submit" class="btn btn-success">Post</button>
</form>
{% endblock %}

集成Bootstrap4的validation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<form method="post" novalidate>
{% csrf_token %}

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

{% if form.is_bound %}
{% if field.errors %}

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

{% else %}
{% render_field field class="form-control is-valid" %}
{% 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 %}

<button type="submit" class="btn btn-success">Post</button>
</form>

可复用的form表单

  • 基于bootstrap样式的可复用表单
  • 将下列代表保存在templateincludes/form.html
  • 使用时使用{% include 'includes/form.html' %}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
{% load widget_tweaks %}

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

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

{% if form.is_bound %}
{% if field.errors %}
{% render_field field class="form-control is-invalid" %}
{% for error in field.errors %}
<div class="invalid-feedback">
{{ error }}
</div>
{% endfor %}
{% else %}
{% render_field field class="form-control is-valid" %}
{% 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 %}
0%