@@ -33,7 +33,7 @@ To upgrade to `webstack-django-sorting` v1.0.0+, you must remove the old middlew
3333The provide is available on PyPI:
3434
3535``` shell
36- pip install webstack_django_sorting
36+ uv add webstack_django_sorting
3737```
3838
3939The project provides examples of integration with Django and Jinja2 templates.
@@ -42,44 +42,44 @@ The project provides examples of integration with Django and Jinja2 templates.
4242
43431 . Add the application to the ` INSTALLED_APPS ` list:
4444
45- ``` python
46- INSTALLED_APPS = [
47- # ...
48- ' webstack_django_sorting' ,
49- ]
50- ```
45+ ``` python
46+ INSTALLED_APPS = [
47+ # ...
48+ ' webstack_django_sorting' ,
49+ ]
50+ ```
5151
52522 . Check the request context processor is loaded in ` TEMPLATES ` options:
5353
54- ```python
55- TEMPLATES = [
56- {
57- ' BACKEND' : ' django.template.backends.django.DjangoTemplates' ,
58- ' DIRS' : [],
59- ' APP_DIRS' : True ,
60- ' OPTIONS' : {
61- ' context_processors' : [
62- # ...
63- ' django.template.context_processors.request' ,
64- # ...
65- ],
66- },
67- },
68- ]
69- ```
54+ ``` python
55+ TEMPLATES = [
56+ {
57+ ' BACKEND' : ' django.template.backends.django.DjangoTemplates' ,
58+ ' DIRS' : [],
59+ ' APP_DIRS' : True ,
60+ ' OPTIONS' : {
61+ ' context_processors' : [
62+ # ...
63+ ' django.template.context_processors.request' ,
64+ # ...
65+ ],
66+ },
67+ },
68+ ]
69+ ```
7070
71713 . Add this line at the top of your template to load the sorting tags:
7272
73- ```html
74- {% load sorting_tags % }
75- ```
73+ ``` html
74+ {% load sorting_tags %}
75+ ```
7676
77774 . Decide on a variable that you would like to sort, and use the
7878 autosort tag on that variable before iterating over it:
7979
80- ```html
81- {% autosort object_list % }
82- ```
80+ ``` html
81+ {% autosort object_list %}
82+ ```
8383
8484 You can pass the option ` nulls=first ` (or ` nulls=last ` ) to explicitly define
8585 the ordering of NULL (not supported by all databases,
@@ -88,23 +88,27 @@ The project provides examples of integration with Django and Jinja2 templates.
88885 . Now, you want to display different headers with links to sort
8989 your objects_list:
9090
91- ```html
92- < tr>
93- < th> {% anchor first_name _(" Name" ) % }< / th>
94- < th> {% anchor creation_date _(" Creation" ) % }< / th>
95- < / tr>
96- ```
91+ ``` html
92+ <tr >
93+ <th >{% anchor first_name _("Name") %}</th >
94+ <th >{% anchor creation_date _("Creation") %}</th >
95+ </tr >
96+ ```
9797
9898 The first argument is a field or an attribute of the objects list, and the
9999 second one (optional) is a title that would be displayed. The previous
100100 snippet will be rendered like this in French:
101101
102- ```html
103- < tr>
104- < th>< a href= " /path/to/your/view/?sort=first_name" title= " Nom" > Nom< / a>< / th>
105- < th>< a href= " /path/to/your/view/?sort=creation_date" title= " Création" > Création< / a>< / th>
106- < / tr>
107- ```
102+ ``` html
103+ <tr >
104+ <th ><a href =" /path/to/your/view/?sort=first_name" title =" Nom" >Nom</a ></th >
105+ <th >
106+ <a href =" /path/to/your/view/?sort=creation_date" title =" Création"
107+ >Création</a
108+ >
109+ </th >
110+ </tr >
111+ ```
108112
109113 An optional 3rd argument allows you to sort first by descending
110114 (e.g. show most recent dates first) ` {% anchor some_date _("Date") desc %} `
@@ -116,48 +120,50 @@ The project provides examples of integration with Django and Jinja2 templates.
116120
1171211 . Define the environment in the ` TEMPLATES ` options:
118122
119- ```python
120- TEMPLATES = {
121- {
122- " BACKEND" : " django.template.backends.jinja2.Jinja2" ,
123- " DIRS" : [],
124- " APP_DIRS" : True ,
125- " OPTIONS" : {
126- " environment" : " testproj.testapp.jinja2.env.JinjaEnvironment" ,
127- },
128- },
129- ]
130- ` ```
123+ ``` python
124+ TEMPLATES = {
125+ {
126+ " BACKEND" : " django.template.backends.jinja2.Jinja2" ,
127+ " DIRS" : [],
128+ " APP_DIRS" : True ,
129+ " OPTIONS" : {
130+ " environment" : " testproj.testapp.jinja2.env.JinjaEnvironment" ,
131+ },
132+ },
133+ ]
134+ ```
131135
1321362 . Your environment file should add `sorting_anchor` and `sort_queryset` to globals :
133137
134- ```python
135- from jinja2.environment import Environment
136- from webstack_django_sorting.jinja2_globals import sorting_anchor, sort_queryset
138+ ```python
139+ from jinja2.environment import Environment
140+ from webstack_django_sorting.jinja2_globals import sorting_anchor, sort_queryset
137141
138- class JinjaEnvironment(Environment):
139- def __init__ (self , ** kwargs):
140- super ().__init__ (** kwargs)
141- self .globals[" sorting_anchor" ] = sorting_anchor
142- self .globals[" sort_queryset" ] = sort_queryset
143- ```
142+ class JinjaEnvironment(Environment):
143+ def __init__ (self , ** kwargs):
144+ super ().__init__ (** kwargs)
145+ self .globals[" sorting_anchor" ] = sorting_anchor
146+ self .globals[" sort_queryset" ] = sort_queryset
147+ ```
144148
1451493 . Now, you can generate header links to sort your queryset.
146150
147- ```html
148- < tr>
149- < th> {{ sorting_anchor(request, " created_on" , " Date" ) }}< / th>
150- < !-- ... -- >
151- < tr>
152- ```
151+ ```html
152+ < tr>
153+ < th> {{ sorting_anchor(request, " created_on" , " Date" ) }}< / th>
154+ < !-- ... -- >
155+ < / tr>
156+
157+ < tr>< / tr>
158+ ```
153159
1541604 . The queryset should be wrapped with `sort_queryset` to use the GET request arguments for sorting:
155161
156- ```html
157- {% for secret_file in sort_queryset(request, secret_files) % }
158- < !-- ... -- >
159- {% endfor % }
160- ```
162+ ```html
163+ {% for secret_file in sort_queryset(request, secret_files) % }
164+ < !-- ... -- >
165+ {% endfor % }
166+ ```
161167
162168That' s it!
163169
0 commit comments