Tôi đang cố gắng tạo một widget tùy chỉnh trong quản trị Django. Tôi tạo ra một lớp:Django. TemplateDoesNotExist trong trường hợp một widget tùy chỉnh
class FroalaWYSIWYGTextareaWidget(django.forms.widgets.Textarea):
template_name = 'froala_wysiwyg.html'
Sau đó, một hình thức mô hình đơn giản:
class ArticleForm(django.forms.ModelForm):
class Meta:
fields = '__all__'
model = Article
widgets = {
'content': FroalaWYSIWYGTextareaWidget(),
}
Dưới đây là các thiết lập của tôi:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_PATH, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.i18n',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Thông thường tất cả mọi thứ hoạt động tốt và Django có thể tìm thấy các mẫu trong tôi/mẫu/thư mục nhưng trong trường hợp của tiện ích này, tôi có Lỗi 500:
TemplateDoesNotExist at /admin/article/article/1/change/
froala_wysiwyg.html
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/article/article/1/change/
Django Version: 1.11.4
Exception Type: TemplateDoesNotExist
Exception Value: froala_wysiwyg.html
Exception Location: /home/username/.virtualenvs/sitename/lib/python3.5/site-packages/django/template/engine.py in find_template, line 148
Python Executable: /home/username/.virtualenvs/sitename/bin/python
Python Version: 3.5.2
tôi sửa lỗi django.filesystem.loader và phát hiện ra rằng thường Loader.engine.dirs là danh sách: ['/home/username/python/sitename/templates']
nên Loader.get_template_sources() hoạt động tuyệt vời
nhưng trong trường hợp của widget tùy chỉnh này loader.engine này .dirs chỉ chứa: ['/home/username/.virtualenvs/sitename/lib/python3.5/site-packages/django/forms/templates']
Tùy chọn này chỉ bỏ qua tùy chọn cài đặt và sử dụng biểu mẫu/mẫu. Nó là một lỗi của Django hay tôi phải thay đổi một cái gì đó trong cài đặt? Tôi không hiểu đường dẫn django/forms/templates
này đến từ đâu? Cảm ơn.
Vâng. Nhưng tôi nên làm gì nếu tôi muốn thay đổi textarea.html cho MỘT trường duy nhất? – Viach
@Viach Trong trường hợp đó bạn có thể kế thừa lớp này và tạo một lớp mới có một textarea.html tùy chỉnh, sau đó bạn có thể cung cấp nó trong tham số widget của bạn trong biểu mẫu –