无法使用自定义related_widget_wrapper.html

时间:2018-03-02 13:24:20

标签: python django django-widget

如果我使用自定义related_widget_wrapper.html,我会收到TemplateDoesNotExist错误。我做的是,我在templates / dashboard目录中创建了一个名为widgets的文件夹,并添加了一个related_widget_wrapper.html文件。这样我得到了上面提到的错误,我发现了什么奇怪的事情

django.template.loaders.filesystem.Loader: /Users/.virtualenvs/furniture/lib/python3.6/site-packages/django/forms/templates/dashboard/widgets/related_widget_wrapper.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/.virtualenvs/furniture/lib/python3.6/site-packages/django/contrib/admin/templates/dashboard/widgets/related_widget_wrapper.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/.virtualenvs/furniture/lib/python3.6/site-packages/django/contrib/auth/templates/dashboard/widgets/related_widget_wrapper.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/.virtualenvs/furniture/lib/python3.6/site-packages/django_tables2/templates/dashboard/widgets/related_widget_wrapper.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/.virtualenvs/furniture/lib/python3.6/site-packages/haystack/templates/dashboard/widgets/related_widget_wrapper.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/.virtualenvs/furniture/lib/python3.6/site-packages/treebeard/templates/dashboard/widgets/related_widget_wrapper.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/.virtualenvs/furniture/lib/python3.6/site-packages/versatileimagefield/templates/dashboard/widgets/related_widget_wrapper.html (Source does not exist)

它不是在我的应用内搜索。我的意思是它应该在我的项目模板中搜索。我错过了什么或者我做错了什么?

class RelatedFieldWidgetWrapper(Widget):
    """
    This class is a wrapper to a given widget to add the add icon for the dashboard.
    """
    template_name = 'dashboard/widgets/related_widget_wrapper.html' # this throws an error 
    # template_name = 'admin/widgets//related_widget_wrapper.html' this works though

    IS_POPUP_VALUE = '1'
    IS_POPUP_VAR = '_popup'
    TO_FIELD_VAR = '_to_field'

    def __init__(self, widget, rel):
        self.needs_multipart_form = widget.needs_multipart_form
        self.attrs = widget.attrs
        self.choices = widget.choices
        self.widget = widget
        self.rel = rel

    def __deepcopy__(self, memo):
        obj = copy.copy(self)
        obj.widget = copy.deepcopy(self.widget, memo)
        obj.attrs = self.widget.attrs
        memo[id(self)] = obj
        return obj

    @property
    def is_hidden(self):
        return self.widget.is_hidden

    @property
    def media(self):
        return self.widget.media

    def get_related_url(self, info, action, *args):
        app_label = info[0]
        model_object_name = info[1]
        # Convert the model's object name into lowercase, with dashes between
        # the camel-cased words
        model_object_name = '-'.join(re.sub('([a-z])([A-Z])', r'\1 \2', model_object_name).lower().split())
        # Does not specify current app
        return reverse("dashboard:%s-%s-%s" % (app_label, model_object_name, action), args=args)

    def get_context(self, name, value, attrs):
        rel_opts = self.rel.model._meta
        info = (rel_opts.app_label, rel_opts.object_name)
        self.widget.choices = self.choices
        url_params = '&'.join("%s=%s" % param for param in [
            (RelatedFieldWidgetWrapper.TO_FIELD_VAR, self.rel.get_related_field().name),
            (RelatedFieldWidgetWrapper.IS_POPUP_VAR, RelatedFieldWidgetWrapper.IS_POPUP_VALUE),
        ])
        context = {
            'rendered_widget': self.widget.render(name, value, attrs),
            'name': name,
            'url_params': url_params,
            'model': rel_opts.verbose_name,
        }
        change_related_template_url = self.get_related_url(info, 'update', '__fk__')
        context.update(
            change_related_template_url=change_related_template_url,
        )
        add_related_url = self.get_related_url(info, 'create')
        context.update(
            add_related_url=add_related_url,
        )
        delete_related_template_url = self.get_related_url(info, 'delete', '__fk__')
        context.update(
            delete_related_template_url=delete_related_template_url,
        )
        return context

    def value_from_datadict(self, data, files, name):
        return self.widget.value_from_datadict(data, files, name)

    def value_omitted_from_data(self, data, files, name):
        return self.widget.value_omitted_from_data(data, files, name)

    def id_for_label(self, id_):
        return self.widget.id_for_label(id_)

模板设置

location = lambda x: os.path.join(BASE_DIR, x)

FURNITURE_MAIN_TEMPLATE_DIR = os.path.join(
    os.path.dirname(os.path.abspath(__file__)), 'templates')
'DIRS': [location('templates'), furniture.FURNITURE_MAIN_TEMPLATE_DIR],

模板树

项目

应用

  • 目录

    仪表板

模板

  • 目录

    仪表板

    base.html文件

1 个答案:

答案 0 :(得分:2)

dashboard应用程序目录

开始

然后您需要创建templates并将related_widget_wrapper.html文件放到那里

因此,您的RelatedFieldWidgetWrapper课程属性将如下所示

class RelatedFieldWidgetWrapper(Widget):
    template_name = 'related_widget_wrapper.html'