Login

Django Uri Param Generator

Author:
ioan1k
Posted:
September 18, 2009
Language:
Python
Version:
1.1
Tags:
pagination url uri djangourl
Score:
0 (after 2 ratings)

This is a simple URI Querystring generator that can be used with Django for generating URI Querystring and preserving the current

Currently working to port into a template tag to allow {% urlgen page|5 %} {% urlgen page 5 %} {% urlgen page,5 %} OR {% urlgen sort name display all %} etc..

  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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
class urlGen:
    
    """
        REVISION: 1.0
        AUTHOR: NICKOLAS WHITING
        
        Build a URL QuerString based on given uri, param
        to add current querystring for a URL
        
        param: Parameter to add to querystring
        
        uri: Dictionary of current querystring Params | Accepts django's request.GET
        
        Usage Example
        
        Add a current URI param and remove if it exists
        
        uri = urlGen()
        
        Current URI: ?page=1&search=nick
        uri.urlgen('page', request.GET)
        Outputs: ?search=nick&page=
        
        Add a uri Param and exclude a current
        
        Current URI: ?search=nick&page=2
        urlgen('order', request.GET, ['page',])
        Outputs: ?search=nick&order=
        
        
    """
    
    def generate(self, param, uri = {}, exclude = []):
        self.param = param
        self.uri = uri
        self.exclude = exclude
        
        self.querystring = False
        
        """
        BUG FIX:
        
        Append param to exclude to ensure the param
        Doesnt get added twice to URI
    
        """
        exclude.append(param)
        
        # Add the URI Param if it is the only one given
        
        if len(self.uri) == 0:
            try:
                self.appendQuerystring(self.param)
            except ExceptionError:
                raise ExceptionError (
                    'urlgen recieved an unexpected error adding %s param failed' % (params)
                    )
        else:
            for k,v in self.uri.iteritems():
                if self.param is not str(k) and k not in self.exclude:
                    self.appendQuerystring(k, v)
            
            # Append the param to end of URL
            
            self.appendQuerystring(self.param)
            
        return self.querystring
                
    def appendQuerystring(self, param, value = False):
        
        """
        Appends a param to the current querystring
        """
        if self.querystring is False:
            if value is False:
                self.querystring = '?%s=' % (str(param))
            else:
                self.querystring = '?%s=%s' % (str(param), str(value))
        else:
            if value is False:
                self.querystring  = '%s&%s=' % (self.querystring, str(param))
            else:
                self.querystring  = '%s&%s=%s' % (self.querystring, str(param), str(value))

""" USAGE EXAMPLE """

    [ urlGen Location ]
        |-- __init__.py
        |-- urlgen.py
    [ Sample File views.py ]
    
    from urlgen.urlgen import urlGen
    uri = urlGen()
    orderURI = uri.generate('order', request.GET)
    return render_to_response('templates.html', {'orderURI': orderURI,}, context_instance=RequestContext(request))

    [ template.html ]
    Order By : <a href="{{ pageURI }}name">Name</a> | <a href="{{ orderURI }}latest">Latest</a> 
   
    [ HTML OUTPUT ]
    <a href="?order=name">Name</a> | <a href="?order=latest">Latest</a>          

    uri = urlGen()

    order = uri.generate('order', request.GET)
    # OUTPUT
    # ?order=
    page = uri.generate('page', {'search': 'unus', 'order': 'name', 'page': 15})
    # OUTPUT
    # ?search=unus&order=name&page=

More like this

  1. get_querystring template tag by skam 2 months, 2 weeks ago
  2. Append paramaters to a GET querystring (template tag) by gregb 5 years, 9 months ago
  3. Page numbers with ... like in Digg by Ciantic 6 years ago
  4. Paginator Tag by insin 8 years, 1 month ago
  5. django paginator by mhangman 5 years, 5 months ago

Comments

Please login first before commenting.