【问题标题】:Can't modify django rest framework base.html file无法修改 django rest 框架 base.html 文件
【发布时间】:2014-11-17 10:08:41
【问题描述】:

我正在使用 django rest 框架,如下所述:django rest framework doc 我在模板目录中添加了 /rest_framework/api.html。

现在的结构是:

|
|\
| apps
|  \
|   settings.py
\
 templates
  \
   rest_framework
    \
     api.html

api.html:

{% extends "rest_framework/base.html" %}

{% block footer %}
    Hello !
{% endblock %}

settings.py:

...

TEMPLATE_LOADERS = (
    ('django.template.loaders.cached.Loader', (
        'django.template.loaders.filesystem.Loader',
        'django.template.loaders.app_directories.Loader',
        'django.template.loaders.eggs.Loader',
    )),
)

...

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'django.contrib.admindocs',
    'django.contrib.markup',
    'django.contrib.webdesign',
     ...
    'rest_framework',
     ...


)

...

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',),
    'PAGINATE_BY': 10
}

我在 api.html 中所做的修改在可浏览的 api 中不显示。我做错了什么?

【问题讨论】:

    标签: python django rest django-rest-framework


    【解决方案1】:

    您是否缺少主要 settings.py 中的 DIRS(这告诉我们在哪里查找模板(覆盖模板):

     TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, 'templates')],
            ...
         }
    

    【讨论】:

    • 不知何故设置'APP_DIRS': True' 无法正确扫描您的应用程序模板文件夹中的rest_framework/api.html。我能做到的唯一方法是在我的项目根目录中明确创建一个templates/rest_framework/api.html,然后按照这个答案的建议明确包括它。确保检查您的 BASE_DIR 值,因为它取决于您的设置组织(我的 settings 在我的项目根目录的子目录中)。
    【解决方案2】:

    djangorestframework==3.5.x

    我有一个确切的问题,即在我的项目应用程序目录之一中存在模板的地方没有拾取模板,例如:

    Project Structure

    project/
        app1/
            templates/
                app1/
                    ...
                rest_framework/
                    app.html
    

    settings.py

    DEBUG = True
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    ...
                ],
                'debug': DEBUG
            },
        },
    ]
    

    我不得不关注joao figueiredo的评论,并在应用目录的外部添加一个特定的模板文件夹。

    Project Structure

    project/
        app1/
            templates/
                app1/
                    ...
        templates/  # Move your file to a specific template dir
            rest_framework/
                app.html
    

    settings.py

    DEBUG = True
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, 'templates')],  # look in this specific folder
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    ...
                ],
                'debug': DEBUG
            },
        },
    ]
    

    【讨论】:

      【解决方案3】:

      您使用的是哪个版本的 Django REST 框架? 我对 base.html 中的块页脚进行了更改,这是为 3.0 版本计划的。

      是你的“你好!”也没有显示在页面的源代码中(按CTRL+U可以得到)?

      如果是,那么最终可能是 CSS 将颜色变为白色的问题。你可以把“你好!”在这样的标签中:<p>Hello !</p>.

      编辑:

      附加信息。

      粘性页脚始终显示在页面底部下方 60 像素处存在问题,因此需要向下滚动才能看到它。如果您使用的是旧版本,这也可能导致问题。 最重要的问题是:是“你好!”根本不在发送到浏览器的源 HTML 中,或者它在那里,但您在页面上看不到它?

      请给我反馈,以便我们解决这个问题。

      【讨论】:

        【解决方案4】:

        在 Django >= 1.8 中,您还必须在 TEMPLATES 字典中将“APP_DIRS”添加为 true。

        TEMPLATES = [ {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'APP_DIRS': True,
            'DIRS': [BASE_DIR,os.path.join(BASE_DIR, 'templates')],
            ...  }
        

        【讨论】:

          【解决方案5】:

          有一个更简单的解决方案。只需将'rest_framework' 放在INSTALLED_APPS 列表的底部即可。或者至少在您覆盖 api 模板的应用程序之后。这样,django 会先去你应用的模板文件夹搜索api.html模板,然后再去rest_framework模板文件夹。

          让我们将此应用程序称为“myapi”。就我而言,我有:

          项目结构

          project/
              myapi/
                  templates/
                      api/
                          ...
                      rest_framework/
                          app.html
          

          settings.py

          INSTALLED_APPS = (
              ...
              'myapi',
              'rest_framework'
          )
          
          TEMPLATES = [
              {
                  'BACKEND': 'django.template.backends.django.DjangoTemplates',
                  'DIRS': [],
                  'APP_DIRS': True,
                  'OPTIONS': {
                      'context_processors': [
                          ...
                      ],
                      'debug': DEBUG
                  },
              },
          ]
          

          【讨论】:

            【解决方案6】:

            您可以将模板设置更改为

            TEMPLATES = [
            {
                'BACKEND': 'django.template.backends.django.DjangoTemplates',
                'DIRS': [BASE_DIR,os.path.join(BASE_DIR, 'templates')],
                ...
             }
            

            如果您使用 django1.8,因为它加载模板的方式不同,BASE_DIR 用于您的模板,os.path.join(BASE_DIR, 'templates') 用于 django-rest-framework。

            【讨论】:

            • 我遇到了这个问题,模板加载路径有些像 .../tempaltes/templates/api.html 所以我添加了 BASE_DIR 作为模板目录,这将使它正确找到模板,不知道为什么给我投反对票。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-02-24
            • 1970-01-01
            • 1970-01-01
            • 2015-10-04
            • 2013-12-26
            • 2014-10-16
            • 2016-10-25
            相关资源
            最近更新 更多