Devang Padhiyar
Devang Padhiyar

Reputation: 3697

How to serve `Docs` folder in Django which contains static html files?

I have following code structure in Django.

├── docs
│   └── _build
│       ├── doctrees
│       └── html
│           └── index.html
│    
├── localcoinswap
├── scripts
├── _static
├── _templates
├── tests
└── manage.py

Here the docs folder contains static html files. i.e index.html

Here are some questions regarding to problem:

  1. The docs folder's html should be served as url <domain>/docs/index.html in Django project. How to achieve this

  2. It should be restricted to User's who have is_staff attribute True.

What urlpattern should I use and Which View is useful to serve those static files (Admin restricted)?

Thank you in advance!

Upvotes: 2

Views: 86

Answers (1)

Aman Garg
Aman Garg

Reputation: 2547

Add the base directory for your docs in TEMPLATES setting in settings.py.

TEMPLATES = [
    {
        ...
        'DIRS': [os.path.join(BASE_DIR, '_templates'), "add your base directory here"],
        ...
    }
]

In your urls.py, serve the files using TemplateView. To further restrict the url to only the staff users, you can wrap the view in staff_member_required decorator.

from django.contrib.admin.views.decorators import staff_member_required
...

urlpatterns += [url(r'^docs/index\.html$', staff_member_required(TemplateView.as_view(template_name='index.html')), name="index"),]

Make sure the file names for your templates and doc templates don't clash, else the first one evaluated according to the DIR list will always be considered.

Upvotes: 1

Related Questions