Ticket #10944: site_url.2.diff
File site_url.2.diff, 4.8 KB (added by , 14 years ago) |
---|
-
django/contrib/sites/tests.py
3 3 from django.core.exceptions import ObjectDoesNotExist 4 4 from django.http import HttpRequest 5 5 from django.test import TestCase 6 from django.template import Template, Context 6 7 7 8 8 9 class SitesFrameworkTests(TestCase): … … 54 55 site = get_current_site(request) 55 56 self.assert_(isinstance(site, RequestSite)) 56 57 self.assertEqual(site.name, u"example.com") 58 59 def test_site_get_url(self): 60 # Test absolute URLs builder using relative path for Site 61 site = Site.objects.get_current() 62 self.assertEqual(u"http://example.com/test", site.get_url("/test")) 63 self.assertEqual(u"https://example.com/test", site.get_url("/test", protocol="https")) 64 65 def test_request_site_get_url(self): 66 # Test absolute URLs builder using relative path for RequestSite 67 Site._meta.installed = False 68 site = Site.objects.get_current() 69 self.assertEqual(u"http://example.com/test", site.get_url("/test")) 70 self.assertEqual(u"https://example.com/test", site.get_url("/test", protocol="https")) 71 72 def test_get_site_url_tag(self): 73 out = Template( 74 "{% load sites %}" 75 "{% site_url '/test' %}" 76 ).render(Context()) 77 self.assertEquals(out, "http://example.com/test") 78 out = Template( 79 "{% load sites %}" 80 "{% site_url '/test' 'https' %}" 81 ).render(Context()) 82 self.assertEquals(out, "https://example.com/test") 83 84 def test_get_request_site_url_tag(self): 85 Site._meta.installed = False 86 out = Template( 87 "{% load sites %}" 88 "{% site_url '/test' %}" 89 ).render(Context()) 90 self.assertEquals(out, "http://example.com/test") 91 out = Template( 92 "{% load sites %}" 93 "{% site_url '/test' 'https' %}" 94 ).render(Context()) 95 self.assertEquals(out, "https://example.com/test") 96 -
django/contrib/sites/models.py
61 61 except KeyError: 62 62 pass 63 63 64 def get_url(self, path, protocol='http'): 65 """Returns absolute URLs based on a protocol and given relative path""" 66 return protocol + '://' + self.domain + path 64 67 65 68 class RequestSite(object): 66 69 """ … … 81 84 82 85 def delete(self): 83 86 raise NotImplementedError('RequestSite cannot be deleted.') 87 88 def get_url(self, path, protocol='http'): 89 """Returns absolute URLs based on a protocol and given relative path""" 90 return protocol + '://' + self.domain + path 84 91 85 86 92 def get_current_site(request): 87 93 """ 88 94 Checks if contrib.sites is installed and returns either the current -
django/contrib/sites/templatetags/sites.py
1 from django import template 2 from django.contrib.sites.models import Site 3 from django.conf import settings 4 5 register = template.Library() 6 7 def site_url(path, scheme='http'): 8 site = Site.objects.get_current() 9 return site.get_url(path, scheme) 10 11 register.simple_tag(site_url) -
docs/ref/contrib/sites.txt
237 237 >>> 'http://%s%s' % (Site.objects.get_current().domain, obj.get_absolute_url()) 238 238 'http://example.com/mymodel/objects/3/' 239 239 240 .. versionadded:: 1.3 241 242 A :class:`~django.contrib.sites.models.Site` object has the method 243 :meth:`~django.contrib.sites.Site.get_url()` to help you build absolute 244 URLs based on a protocol, a domain and a relative path. 245 246 >>> Site.objects.get_current().get_url(obj.get_absolute_url()) 247 'http://example.com/mymodel/objects/3/' 248 >>> Site.objects.get_current().get_url(obj.get_absolute_url(), protocol='https') 249 'https://example.com/mymodel/objects/3/' 250 251 The template tag ``site_url`` allows you to build absolute URLs inside a 252 template by specifying a relative path and a protocol. 253 254 Sample usages:: 255 256 {% site_url '/test' %} 257 {% site_url '/test' 'https' %} 258 240 259 Caching the current ``Site`` object 241 260 =================================== 242 261