Login

Inherit the standard url tag to include domain name

Author:
atodorov-otb
Posted:
December 22, 2013
Language:
Python
Version:
1.4
Score:
0 (after 0 ratings)

This module extends the standard `url' template tag in Django and adds support for fully qualified domain name URLs. It also can be extended with simple URL load balancing techniques if desired.

See my blog for the background story: http://atodorov.org/blog/2013/12/22/django-template-tag-inheritance-howto/

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
################################################################################
#
#   Copyright (c) 2013, Alexander Todorov <[email protected]>
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
################################################################################
#
# This module extends the standard `url' template tag in Django and adds support
# for fully qualified domain name URLs.
# It also can be extended with URL load balancing techniques if desired.
#
################################################################################

try:
    from settings import FQDN
except:
    FQDN = ""

# Note: we could use the sites framework but it stores the domain in the DB
# not in local_settings.py which I don't like. It's also missing support for
# web clusters which can serve the same content over different URLs

from django.template import Library
from django.template import defaulttags
from django.templatetags import future


register = Library()


class FQDN_URLNode(defaulttags.URLNode):
    def render(self, context):
        retval = super(FQDN_URLNode, self).render(context)
        retval = FQDN + retval

        if self.asvar:
            context[self.asvar] = retval
            return ''
        else:
            return retval

@register.tag
def fqdn_url(parser, token):
    # NB change with defaulttags.url for Django 1.5 and later
    retval = future.url(parser, token)
    retval.__class__ = FQDN_URLNode

    return retval

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.