How to customise API Root in Django REST Framework

Modify your projects ( helloproject/ ) to know the BASE directory of the project

BASE_DIR = Path(__file__).resolve().parent.parent

You can print the BASE diretctory in python to know what is your directory, so you can adjust the theme directory for DRF to customise theme.

print (BASE_DIR)

Now, override the templates directory path by modifying TEMPLATES => DIRS in your projects to use your templates directory. In our project we had kept the templates directory in per application , hence our templates directory is “helloproject/helloapp/templates” which we used in “DIR”

        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'helloproject/helloapp/templates'],

Now, inside templates directory create a api.html to override your default theme as,

$ mkdir -p helloproject/helloapp/templates/rest_framework/
$ vim helloproject/helloapp/templates/rest_framework/api.html
{% extends "rest_framework/base.html" %}

{% block bootstrap_theme %}
    <link rel="stylesheet" href="" type="text/css">
{% endblock %}

{% block bootstrap_navbar_variant %}{% endblock %}

Reference :

Leave a Comment