Created
January 11, 2012 13:55
-
-
Save jkal/1594760 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Usage | |
===== | |
1. Add 'urljs' in INSTALLED_APPS. | |
2. Hook the view in the URLconf: | |
url(r'^urljs/$', 'urljs.views.urljs', name='urljs'), | |
3. Somewhere in your main template, include the necessary JS files: | |
<script type="text/javascript" src="{{ STATIC_URL }}js/urljs.js"></script> | |
<script type="text/javascript" src="{% url urljs %}?callback=urljs.init"></script> | |
4. Everytime you want to reverse a URL by its name: | |
var public_profile_url = urljs.reverse('profile_public', args={username: 'bob'}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var urljs = (function () { | |
var patterns = {}; | |
var exports = {}; | |
var interpolate = function(s, args) { | |
var i = 0; | |
return s.replace(/%(?:\(([^)]+)\))?([%diouxXeEfFgGcrs])/g, function (match, v, t) { | |
if (t == "%") return "%"; | |
return args[v || i++]; | |
}); | |
} | |
exports.init = function(data) { | |
patterns = data; | |
} | |
exports.reverse = function(name, args) { | |
if (!patterns.hasOwnProperty(name)) { | |
throw('URL pattern does not exist'); | |
} | |
pattern = patterns[name]; | |
return interpolate(pattern, args); | |
} | |
return exports; | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
def jsonp(view_func): | |
""" | |
JSONP view decorator. | |
Usage: | |
@jsonp | |
def my_view(request): | |
d = {'key': 'value'} | |
return HttpResponse(json.dumps(d), content_type='application/json') | |
""" | |
@wraps(view_func) | |
def _view(request, *args, **kwargs): | |
resp = view_func(request, *args, **kwargs) | |
if resp.status_code != 200: | |
return resp | |
if 'callback' in request.GET: | |
callback= request.GET['callback'] | |
resp['Content-Type']='text/javascript; charset=utf-8' | |
resp.content = '%s(%s)' % (callback, resp.content) | |
return resp | |
elif 'jsonp' in request.GET: | |
callback= request.GET['jsonp'] | |
resp['Content-Type']='text/javascript; charset=utf-8' | |
resp.content = '%s(%s)' % (callback, resp.content) | |
return resp | |
else: | |
return resp | |
return _view |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
from django.http import HttpResponse | |
from django.core.urlresolvers import get_resolver | |
from django.utils import simplejson | |
from urljs.utils import jsonp | |
def _get_named_patterns(): | |
"""Returns a dictionary with all the named URL patterns.""" | |
resolver = get_resolver(None) | |
patterns = {} | |
for key, value in resolver.reverse_dict.iteritems(): | |
if isinstance(key, basestring): | |
patterns[key] = '/%s' % value[0][0][0] | |
return patterns | |
@jsonp | |
def urljs(request): | |
"""Hook this into your URLconf to get a JS object of all | |
the named URL patterns used in the project. | |
Example: | |
url(r'^urljs/$', 'urljs.views.urljs', name='urljs'), | |
""" | |
patterns = _get_named_patterns() | |
return HttpResponse(simplejson.dumps(patterns), mimetype='text/javascript') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment