jazzband/django-widget-tweaks — dự án mã nguồn mở — đang có 2.2k sao GitHub. Tweak the form field rendering in templates, not in python-level form definitions. CSS classes and HTML attributes can be altered.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Tweak the form field rendering in templates, not in python-level
form definitions. Altering CSS classes and HTML attributes is supported.
That should be enough for designers to customize field presentation (using
CSS and unobtrusive javascript) without touching python code.
License is MIT.
Installation
You can get Django Widget Tweaks by using pip::
$ pip install django-widget-tweaks
To enable widget_tweaks in your project you need to add it to INSTALLED_APPS in your projects
settings.py file:
.. code-block:: python
INSTALLED_APPS += [
'widget_tweaks',
]
Usage
This app provides two sets of tools that may be used together or standalone:
a render_field template tag for customizing form fields by using an
HTML-like syntax.
several template filters for customizing form field HTML attributes and CSS
classes
The render_field tag should be easier to use and should make form field
customizations much easier for designers and front-end developers.
The template filters are more powerful than the render_field tag, but they
use a more complex and less HTML-like syntax.
render_field
This is a template tag that can be used as an alternative to aforementioned
filters. This template tag renders a field using a syntax similar to plain
HTML attributes.
Example:
.. code-block:: html+django
{% 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 %}
<!-- double colon -->
{% render_field form.search_query v-bind::class="{active:isActive}" %}
For fields rendered with {% render_field %} tag it is possible
to set error class and required fields class by using
WIDGET_ERROR_CLASS and WIDGET_REQUIRED_CLASS template variables:
You can be creative with these variables: e.g. a context processor could
set a default CSS error class on all fields rendered by
{% render_field %}.
attr
Adds or replaces any single html attribute for the form field.
Examples:
.. code-block:: html+django
{% load widget_tweaks %}
<!-- change input type (e.g. to HTML5) -->
{{ form.search_query|attr:"type:search" }}
<!-- add/change several attributes -->
{{ form.text|attr:"rows:20"|attr:"cols:20"|attr:"title:Hello, world!" }}
<!-- attributes without parameters -->
{{ form.search_query|attr:"autofocus" }}
<!-- attributes with double colon Vuejs output: v-bind:class="{active:ValueEnabled}" -->
{{ form.search_query|attr:"v-bind::class:{active:ValueEnabled}" }}
add_class
Adds CSS class to field element. Split classes by whitespace in order to add
several classes at once.
Example:
.. code-block:: html+django
{% load widget_tweaks %}
<!-- add 2 extra css classes to field element -->
{{ form.title|add_class:"css_class_1 css_class_2" }}
set_data
Sets HTML5 data attribute ( https://johnresig.com/blog/html-5-data-attributes/ ).
Useful for unobtrusive javascript. It is just a shortcut for 'attr' filter
that prepends attribute names with 'data-' string.
Example:
.. code-block:: html+django
{% load widget_tweaks %}
<!-- data-filters:"OverText" will be added to input field -->
{{ form.title|set_data:"filters:OverText" }}
append_attr
Appends attribute value with extra data.
Example:
.. code-block:: html+django
{% load widget_tweaks %}
<!-- add 2 extra css classes to field element -->
{{ form.title|append_attr:"class:css_class_1 css_class_2" }}
'add_class' filter is just a shortcut for 'append_attr' filter that
adds values to the 'class' attribute.
remove_attr
Removes any single html attribute for the form field.
Example:
.. code-block:: html+django
{% load widget_tweaks %}
<!-- removes autofocus attribute from field element -->
{{ form.title|remove_attr:"autofocus" }}
add_label_class
The same as add_class but adds css class to form labels.
Example:
.. code-block:: html+django
{% load widget_tweaks %}
<!-- add 2 extra css classes to field label element -->
{{ form.title|add_label_class:"label_class_1 label_class_2" }}
add_error_class
The same as 'add_class' but adds css class only if validation failed for
the field (field.errors is not empty).
Example:
.. code-block:: html+django
{% load widget_tweaks %}
<!-- add 'error-border' css class on field error -->
{{ form.title|add_error_class:"error-border" }}
add_error_attr
The same as 'attr' but sets an attribute only if validation failed for
the field (field.errors is not empty). This can be useful when dealing
with accessibility:
.. code-block:: html+django
{% load widget_tweaks %}
<!-- add aria-invalid="true" attribute on field error -->
{{ form.title|add_error_attr:"aria-invalid:true" }}
add_required_class
The same as 'add_error_class' adds css class only for required field.
Example:
.. code-block:: html+django
{% load widget_tweaks %}
<!-- add 'is-required' css class on field required -->
{{ form.title|add_required_class:"is-required" }}
field_type and widget_type
'field_type' and 'widget_type' are template filters that return
field class name and field widget class name (in lower case).
Some fields may render as a MultiWidget, composed of multiple subwidgets
(for example, a ChoiceField using RadioSelect). You can use the same tags
and filters, but your template code will need to include a for loop for fields
like this:
.. code-block:: html+django
{% load widget_tweaks %}
{% for widget in form.choice %}
{{ widget|add_class:"css_class_1 css_class_2" }}
{% endfor %}
Mixing render_field and filters
The render_field tag and filters mentioned above can be mixed.
{# my_template.html #}
{% load widget_tweaks %}
<form method='POST' action=''> {% csrf_token %}
{% include "inc/field.html" with field=form.title %}
{% include "inc/field.html" with field=form.description|attr:"foo:non_default_foo" %}
</form>
With 'rightmost filter wins' rule it wouldn't be possible to override
|attr:"foo:default_foo" in main template.
Rendering form error messages
This app can render the following form error messages:
Field related errors
Non-field related errors
All form errors - Displays all field and Non-field related errors. If related to a specific field the name is displayed above the error, if the error is a general form error, displays all
Field related errors
To render field related errors in your form:
Example:
.. code-block:: html+django
{% load widget_tweaks %}
{% for error in field.errors %}
<span class="text-danger">{{ error }}</span>
{% endfor %}
Example usage:
.. code-block:: html+django
{% for field in form.visible_fields %}
{{ field }}
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
{% for error in field.errors %}
<span class="text-danger">{{ error }}</span>
{% endfor %}
{% endfor %}
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/jazzband/django-widget-tweaks là nguồn chính thức.
jazzband/django-widget-tweaks có bao nhiêu sao?
jazzband/django-widget-tweaks có 2.2k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/jazzband/django-widget-tweaks. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
jazzband/django-widget-tweaks có tag gì không?
Bản đồng bộ chưa ghi nhận topic GitHub nào cho jazzband/django-widget-tweaks. GitHub topics hiển thị ở thanh bên phải trang repo — đó là nơi đáng kiểm tra nhất.
jazzband/django-widget-tweaks có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho jazzband/django-widget-tweaks. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
jazzband/django-widget-tweaks còn đang phát triển không?
Commit gần nhất trên jazzband/django-widget-tweaks là 30 ngày trước (theo timestamp GitHub). Repo có 140 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
jazzband/django-widget-tweaks dùng license gì?
jazzband/django-widget-tweaks phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
jazzband/django-widget-tweaks viết bằng ngôn ngữ gì?
jazzband/django-widget-tweaks chủ yếu viết bằng Python. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Đọc đầy đủ README ở tab phía trên.
Muốn nghe thêm một ý kiến về django-widget-tweaks?
Hỏi một AI đọc được trang này — một cú bấm là có ngay nhận định về django-widget-tweaks.