Can't modify django rest framework base.html f

2019-06-16 02:55发布

问题:

I'm using django rest framework, and as discribed here : django rest framework doc I've added /rest_framework/api.html in my templates dir.

The structure now is :

|
|\
| 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
}

The modification I do in api.html are not displayed in the browsable api. What am I doing wrong ?

回答1:

Which version of Django REST Framework are you using? I made changes to the block footer in the base.html and this was planed for the 3.0 release.

Is your 'Hello !' also not showing in the source code of the page (you can get it by pressing CTRL+U)?

If yes, than it could eventually be an issue with CSS making the colour white. You can put 'Hello !' in a tag like this: <p>Hello !</p>.

EDIT:

Additional info.

There was an issue with the sticky footer displaying always 60px below the page bottom, thus scrolling down was needed to see it. If you are using an older version this can be also causing the problem. The most important question is: is 'Hello !' not at all in the source HTML sent to the browser or is it there, but you can't see it on the page?

Please give me a feedback, so we can solve this.



回答2:

Are you missing the DIRS from the main settings.py (this tells us where to look for templates (override templates):

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


回答3:

djangorestframework==3.5.x

I had the exact problem where the template was not picked up where the template existed in one of my project app directories, as such:

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
        },
    },
]

I had to follow joao figueiredo's comment, and add a specific template folder outside of the app directory.

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
        },
    },
]


回答4:

It is crucial in Django >= 1.8 that you also add "APP_DIRS" to true in the TEMPLATES dictionary.

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


回答5:

you may change your tempaltes settings to

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

if you use django1.8,that because it load tempalte diffrently,the BASE_DIR is for your templates,os.path.join(BASE_DIR, 'templates') is for django-rest-framework.



回答6:

There is an easier solution. Simply put 'rest_framework' at the bottom of your INSTALLED_APPS list. Or at least after the app where you are overriding the api template. This way, django will go to your app's templates folder searching for api.html template before going to rest_framework tempalte folder.

Let's call this app 'myapi'. In my case I have:

Project Structure

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
        },
    },
]