Skip to content

Commit 1066c38

Browse files
authored
Merge pull request #6 from ahmedaljazzar/enhancements/url-resolver
Supporting Django 2.0 reverse function
2 parents 2dfae12 + ea12b95 commit 1066c38

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

djangomako/backends.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010

1111
import tempfile
1212

13-
from django.core.urlresolvers import reverse
13+
from django import get_version
1414
from django.contrib.staticfiles.templatetags.staticfiles import static
1515
from django.template import TemplateDoesNotExist, TemplateSyntaxError
1616
from django.template.backends.base import BaseEngine
17-
from django.template.backends.utils import csrf_input_lazy, \
18-
csrf_token_lazy
19-
17+
from django.template.backends.utils import (
18+
csrf_input_lazy,
19+
csrf_token_lazy,
20+
)
2021
from django.utils.module_loading import import_string
22+
2123
from mako.template import Template as MakoTemplate
2224
from mako import exceptions as mako_exceptions
2325

@@ -151,6 +153,9 @@ def render(self, context=None, request=None):
151153
if context is None:
152154
context = {}
153155

156+
context['static'] = static
157+
context['url'] = self.get_reverse_url()
158+
154159
if request is not None:
155160
# As Django doesn't have a global request object,
156161
# it's useful to put it in the context.
@@ -159,7 +164,13 @@ def render(self, context=None, request=None):
159164
context['csrf_input'] = csrf_input_lazy(request)
160165
context['csrf_token'] = csrf_token_lazy(request)
161166

162-
context['static'] = static
163-
context['url'] = reverse
164-
165167
return self.template.render(**context)
168+
169+
@staticmethod
170+
def get_reverse_url():
171+
if get_version() < '2.0':
172+
from django.core.urlresolvers import reverse
173+
else:
174+
from django.urls import reverse
175+
176+
return reverse

niceapp/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def get_template_names(self):
3838
def get_context_data(self, **kwargs):
3939
context = super(ClassBasedView, self).get_context_data(**kwargs)
4040
engine = kwargs['engine']
41-
41+
print context
4242
context.update({
4343
'items': [
4444
'Wow',

templates/django.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
generated.
1616
</p>
1717
<p>
18-
The home url for the CBV is: <i><strong>{% url 'CBV' %}</strong></i>
18+
The home url for the CBV is: <a href="{% url 'CBV' %}">{% url 'CBV' %}</a>
1919
</p>
2020
<p>
2121
The request object is <i><strong>{{ request }}</strong></i>
2222
</p>
2323
</body>
2424

25-
</html>
25+
</html>

templates/mako.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
Your CSRF Token is: <i><strong>${ csrf_token }</strong></i>
1919
</p>
2020
<p>
21-
The home url for the CBV is: <i><strong>${ url('CBV') }</strong></i>
21+
The home url for the CBV is: <a href="${ url('CBV') }">${ url('CBV') }</a>
2222
</p>
2323
<p>
2424
The request body is <i><strong>${ request.body }</strong></i>
2525
</p>
2626

2727
</body>
2828

29-
</html>
29+
</html>

0 commit comments

Comments
 (0)