Ticket #10944: site_url.diff

File site_url.diff, 2.1 KB (added by laurentluce, 14 years ago)

get_url Site model method and site_url template tage

  • django/contrib/sites/tests.py

     
    33from django.core.exceptions import ObjectDoesNotExist
    44from django.http import HttpRequest
    55from django.test import TestCase
     6from django.template import Template, Context
    67
    78
    89class SitesFrameworkTests(TestCase):
     
    5455        site = get_current_site(request)
    5556        self.assert_(isinstance(site, RequestSite))
    5657        self.assertEqual(site.name, u"example.com")
     58   
     59    def test_get_url(self):
     60        # Test absolute URLs builder using relative path
     61        site = Site.objects.get_current()
     62        self.assertEqual(u"example.com/test", site.get_url("/test"))
     63
     64    def test_get_site_url_tag(self):
     65        out = Template(
     66                "{% load sites %}"
     67                "{% site_url '/test' %}"
     68            ).render(Context())
     69        self.assertEquals(out, "example.com/test")
     70
  • django/contrib/sites/models.py

     
    6161        except KeyError:
    6262            pass
    6363
     64    def get_url(self, path):
     65        """Returns absolute URLs based on a given relative path"""
     66        return self.domain + path
    6467
    6568class RequestSite(object):
    6669    """
  • django/contrib/sites/templatetags/sites.py

     
     1from django import template
     2from django.contrib.sites.models import Site
     3from django.conf import settings
     4
     5register = template.Library()
     6
     7def site_url(path):
     8    site = Site.objects.get_current()
     9    return site.get_url(path)
     10
     11register.simple_tag(site_url)
Back to Top