Home » Django & REST Api » How to customise API Root in Django REST Framework

How to customise API Root in Django REST Framework

Modify your projects settings.py ( helloproject/settings.py ) 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 settings.py 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”

TEMPLATES = [
    {
        '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="https://stackpath.bootstrapcdn.com/bootswatch/4.5.2/darkly/bootstrap.min.css" type="text/css">
{% endblock %}

{% block bootstrap_navbar_variant %}{% endblock %}

Reference : https://www.django-rest-framework.org/topics/browsable-api/ https://docs.djangoproject.com/en/3.1/howto/overriding-templates/


Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment