Inherit the standard url tag to include domain name

 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 <atodorov@nospam.dif.io>
#
#   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. extends_default by daniellindsley 5 years, 5 months ago
  2. isoutc template filter by japerk 5 years ago
  3. Server Side Cursors for Django's psycopg2 Backend by ryanbutterfield 3 years, 1 month ago
  4. "Partial Templates" - an alternative to "include" by vigrid 5 years, 2 months ago
  5. Automating URLs by jorjun 4 years, 9 months ago

Comments

(Forgotten your password?)