2017-08-23 24 views
9

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.

Trả lời

8

của nó chắc chắn không phải là một lỗi

I don't understand where does this django/forms/templates path come from?

Bạn có thể xem source code nơi bạn sẽ nhìn thấy dòng

[docs]class Textarea(Widget): 
    template_name = 'django/forms/widgets/textarea.html' 

này là nguồn gốc của câu hỏi đầu tiên của bạn. Bây giờ, thứ hai,

This renderer uses a standalone DjangoTemplates engine (unconnected to what you might have configured in the TEMPLATES setting). It loads templates first from the built-in form templates directory in django/forms/templates and then from the installed apps’ templates directories using the app_directories loader.

Điều này cũng đúng cho các lớp tiện ích biểu mẫu của bạn. Để làm cho mọi thứ hoạt động cho bạn, bạn phải chỉ định đường dẫn có cùng thuật ngữ như app_name/forms/widget/textarea.html

+0

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

+0

@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 –

3

Nếu bạn muốn sử dụng mẫu widget tùy chỉnh được lưu ở đâu đó trong thư mục "TEMPLATES" của dự án của bạn, hãy làm theo các bước sau:

  1. Sử dụng các thiết lập mẫu mà bạn đã cung cấp trong câu hỏi của bạn (lặp đi lặp lại dưới đây):

    MẪU = [ { 'backend': 'django.template.backends.django.DjangoTemplates', ' DIRS ': [os.path .join (BASE_PATH, 'mẫu')], 'APP_DIRS': Đúng, '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', ], }, }, ]

  2. Đặt FORM_RENDERER như sau trong settings.py

    FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'

  3. Thêm ứng dụng "django.forms" vào danh sách 'INSTALLED_APPS' trong settings.py

Ngoài ra, hãy đảm bảo chỉ định đường dẫn chính xác của mẫu tiện ích tùy chỉnh tương ứng với trực tiếp "TEMPLATES" của bạn ory đến thuộc tính "template_name" của tiện ích tùy chỉnh của bạn.

Các vấn đề liên quan