我在项目中使用 Django 1.11 。在我的编辑表单中,我有 ImageField 。 Django默认渲染html,你可以在下面看到。如何正确更改ImageField的html格式?
我尝试了下一个代码,但Django引发了错误 TemplateDoesNotExist 。 Django在我的自定义小部件中看不到 template_file ?如何解决这个问题?顺便说一下, initial_text 和 input_text 效果很好。我也试过 template_with_initial ,但不幸的是它没有帮助我。我将不胜感激任何帮助!
Django默认渲染 html :
Currently:
<a href="/media/images/2017/08/23/picture.jpg">images/2017/08/23/picture.jpg</a>
Change:
<input name="image" id="id_image" type="file">
widgets.py:
from django.forms.widgets import ClearableFileInput
class CustomClearableFileInput(ClearableFileInput):
initial_text = 'Current image'
input_text = 'Change'
clear_checkbox_label = 'Clear Image'
template_name = 'custom_clearable_file_input.html' <-- DONT WORK
custom_clearable_file_input.html:
{% if widget.is_initial %}
<span>{{ widget.initial_text }}</span>: <a href="{{ widget.value.url }}">{{ widget.value }}</a>
{% if not widget.required %}
<input type="checkbox" name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}"/>
<label for="{{ widget.checkbox_id }}">{{ widget.clear_checkbox_label }}</label>
{% endif %}
<br/>
<span>{{ widget.input_text }}</span>:
{% endif %}
<input type="{{ widget.type }}" name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}/>
错误:
Traceback (most recent call last):
File "/srv/envs/Project/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/srv/envs/Project/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/srv/envs/Project/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/srv/envs/Project/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/srv/envs/Project/lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/nurzhan/CA/article/views.py", line 106, in get
request=request
File "/srv/envs/Project/lib/python3.6/site-packages/django/template/loader.py", line 68, in render_to_string
return template.render(context, request)
File "/srv/envs/Project/lib/python3.6/site-packages/django/template/backends/django.py", line 68, in render
reraise(exc, self.backend)
File "/srv/envs/Project/lib/python3.6/site-packages/django/template/backends/django.py", line 89, in reraise
six.reraise(exc.__class__, new, sys.exc_info()[2])
File "/srv/envs/Project/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/srv/envs/Project/lib/python3.6/site-packages/django/template/backends/django.py", line 66, in render
return self.template.render(context)
File "/srv/envs/Project/lib/python3.6/site-packages/django/template/base.py", line 207, in render
return self._render(context)
File "/srv/envs/Project/lib/python3.6/site-packages/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/srv/envs/Project/lib/python3.6/site-packages/django/template/base.py", line 993, in render
bits.append(force_text(bit))
File "/srv/envs/Project/lib/python3.6/site-packages/django/utils/encoding.py", line 76, in force_text
s = six.text_type(s)
File "/srv/envs/Project/lib/python3.6/site-packages/django/utils/html.py", line 385, in <lambda>
klass.__str__ = lambda self: mark_safe(klass_str(self))
File "/srv/envs/Project/lib/python3.6/site-packages/django/forms/boundfield.py", line 41, in __str__
return self.as_widget()
File "/srv/envs/Project/lib/python3.6/site-packages/django/forms/boundfield.py", line 120, in as_widget
**kwargs
File "/srv/envs/Project/lib/python3.6/site-packages/django/forms/widgets.py", line 221, in render
return self._render(self.template_name, context, renderer)
File "/srv/envs/Project/lib/python3.6/site-packages/django/forms/widgets.py", line 226, in _render
return mark_safe(renderer.render(template_name, context))
File "/srv/envs/Project/lib/python3.6/site-packages/django/forms/renderers.py", line 31, in render
template = self.get_template(template_name)
File "/srv/envs/Project/lib/python3.6/site-packages/django/forms/renderers.py", line 37, in get_template
return self.engine.get_template(template_name)
File "/srv/envs/Project/lib/python3.6/site-packages/django/template/backends/django.py", line 41, in get_template
reraise(exc, self)
File "/srv/envs/Project/lib/python3.6/site-packages/django/template/backends/django.py", line 89, in reraise
six.reraise(exc.__class__, new, sys.exc_info()[2])
File "/srv/envs/kaseAdmPy362/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/srv/envs/kaseAdmPy362/lib/python3.6/site-packages/django/template/backends/django.py", line 39, in get_template
return Template(self.engine.get_template(template_name), self)
File "/srv/envs/kaseAdmPy362/lib/python3.6/site-packages/django/template/engine.py", line 162, in get_template
template, origin = self.find_template(template_name)
File "/srv/envs/kaseAdmPy362/lib/python3.6/site-packages/django/template/engine.py", line 148, in find_template
raise TemplateDoesNotExist(name, tried=tried)
django.template.exceptions.TemplateDoesNotExist: custom_clearable_file_input.html
[24/Aug/2017 11:02:30] "GET /administration/article/47/edit/ HTTP/1.1" 500
答案 0 :(得分:1)
这取决于存储自定义模板文件的路径。
如form rendering documentations:
中所述Django的表单小部件使用Django的模板引擎系统进行渲染。
表单模板的呈现由... DjangoTemplates控制 [渲染器类]。
[DjangoTemplates]渲染器使用独立的DjangoTemplates引擎 (与您在TEMPLATES中配置的内容无关 设置)。它首先从内置表单模板加载模板 django / forms / templates中的目录,然后来自已安装的应用程序 使用app_directories加载器模板目录。
如果您想使用您的自定义项呈现模板 TEMPLATES设置,例如上下文处理器,使用 TemplatesSetting渲染器。
您有两种选择:
在(app_name)文件夹中创建一个templates
子目录,并将temmplate文件放在此文件夹中。 (/app_name/templates/custom_clearable_file_input.html
)
您可以将模板文件保留在templates
文件TEMPLATES['DIRS']
参数中指定的全局settings.py
文件夹中(/templates/custom_clearable_file_input.html
),但必须使用模板设置TemplatesSettings documentation中指定的渲染器(我添加了settings.py
文件的自定义示例)
import django
...
INSTALLED_APPS = [
...
'django.forms'
...
]
...
TEMPLATES = [
{
...
'DIRS': [
'templates',
django.__path__[0] + '/forms/templates',
],
'APP_DIRS': True,
...
},
]
...
FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'