jeffreyb
jeffreyb

Reputation: 163

Django restrict access to TemplateView

I'm using TemplateView to display swagger pages (local files). However, now I need to restrict access. Using a normal view, I could use @login_required mixin on the view. Is there a way to do that with TemplateViews? Or should I be using some other way of displaying these swagger pages?

url(r'^swagger/', TemplateView.as_view(template_name='swagger.html'), name='swagger'),

Upvotes: 1

Views: 734

Answers (1)

Vitor Freitas
Vitor Freitas

Reputation: 3610

The most clean way would be to create a view extending the TemplateView, so it would help leaving your urls.py clean.

views.py

from django.contrib.auth.mixins import LoginRequiredMixin

class SwaggerView(LoginRequiredMixin, TemplateView):
    template_name = 'swagger.html'

urls.py

from . import views

url(r'^swagger/', views.SwaggerView.as_view(), name='swagger'),

Upvotes: 2

Related Questions