Answer the question
In order to leave comments, you need to log in
How to make custom form widgets in django?
Interested in 3 questions:
1. How to connect and use your .html template in the widget?
2. How to make a custom template inside the widget (the minimum set of tags)?
3. How to properly store your widget in the application:
-myapp:
--forms:
---widget.py
---templates:
----mywidget.html
from django import forms
from django.forms.utils import flatatt
from django.utils.html import format_html
class Tags(forms.TextInput):
input_type = 'text'
#переопределяем рендер
def render(self, name=None, value=None, attrs=None):
attrs = dict(self.attrs, **attrs) if attrs else self.attrs
final_attrs = dict(attrs, type=self.input_type, name=name)
return format_html('<input {} >', flatatt(final_attrs)) # flatatt позволяет пребразовать словарь в нормальные пары
# подключаем статику
class Media:
css = {
'all': ('style.css',)
}
js = 'script.js'
test = forms.CharField(widget=Tags)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question