Skip to content

Commit ed0f7b8

Browse files
author
Emmanouil Konstantinidis
committed
Get a list with all the views
1 parent 4802d92 commit ed0f7b8

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

drfdocs/templates/drfdocs/base.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
77
<body>
88
<h1>Django Rest Frameworks Docs</h1>
99

10+
{% block content %}{% endblock %}
11+
1012
</body>
1113
</html>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
11
{% extends "drfdocs/base.html" %}
2+
3+
{% block content %}
4+
<ul>
5+
{% for view in views %}
6+
<li>{{ view }}</li>
7+
{% endfor %}
8+
</ul>
9+
{% endblock %}

drfdocs/views.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1+
from django.conf import settings
12
from django.views.generic.base import TemplateView
3+
from django.core.urlresolvers import RegexURLResolver, RegexURLPattern
24

35

46
class DRFDocsView(TemplateView):
57

8+
root_urlconf = __import__(settings.ROOT_URLCONF)
69
template_name = "drfdocs/home.html"
10+
VIEW_NAMES = []
11+
12+
def get_all_view_names(self, urlpatterns):
13+
for pattern in urlpatterns:
14+
if isinstance(pattern, RegexURLResolver):
15+
self.get_all_view_names(pattern.url_patterns)
16+
elif isinstance(pattern, RegexURLPattern):
17+
view_name = pattern.callback.__name__
18+
self.VIEW_NAMES.append(view_name)
19+
return self.VIEW_NAMES
720

821
def get_context_data(self, **kwargs):
22+
self.VIEW_NAMES = []
923
context = super(DRFDocsView, self).get_context_data(**kwargs)
10-
context['example'] = True
24+
all_urlpatterns = self.root_urlconf.urls.urlpatterns
25+
context['views'] = self.get_all_view_names(all_urlpatterns)
1126
return context

0 commit comments

Comments
 (0)