forked from yceruto/django-ajax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
298 lines (210 loc) · 18.7 KB
/
index.html
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="description" content="django-ajax : Fast and easy AJAX libraries for django projects. Contains ajax decorator, ajax middleware, shortcuts, jquery plugins and more.">
<link rel="stylesheet" type="text/css" media="screen" href="stylesheets/stylesheet.css">
<title>django-ajax</title>
</head>
<body>
<!-- HEADER -->
<div id="header_wrap" class="outer">
<header class="inner">
<a id="forkme_banner" href="https://github.com/yceruto/django-ajax">View on GitHub</a>
<h1 id="project_title">django-ajax</h1>
<h2 id="project_tagline">Fast and easy AJAX libraries for django projects. Contains ajax decorator, ajax middleware, shortcuts, jquery plugins and more.</h2>
<section id="downloads">
<a class="zip_download_link" href="https://github.com/yceruto/django-ajax/zipball/master">Download this project as a .zip file</a>
<a class="tar_download_link" href="https://github.com/yceruto/django-ajax/tarball/master">Download this project as a tar.gz file</a>
</section>
</header>
</div>
<!-- MAIN CONTENT -->
<div id="main_content_wrap" class="outer">
<section id="main_content" class="inner">
<h1>
<a id="django-ajax" class="anchor" href="#django-ajax" aria-hidden="true"><span class="octicon octicon-link"></span></a>django-ajax</h1>
<p>Fast and easy AJAX libraries for django projects.</p>
<h2>
<a id="requirements" class="anchor" href="#requirements" aria-hidden="true"><span class="octicon octicon-link"></span></a>Requirements</h2>
<ul>
<li>
<a href="http://www.python.org">python</a> >= 2.6</li>
<li>
<a href="https://djangoproject.com">django</a> >= 1.5</li>
<li>
<a href="http://jquery.com">jQuery</a> >= 1.5</li>
</ul>
<h2>
<a id="installation" class="anchor" href="#installation" aria-hidden="true"><span class="octicon octicon-link"></span></a>Installation</h2>
<p>Install django-ajax in your python environment</p>
<p>1- Download and install package:</p>
<div class="highlight highlight-sh"><pre>$ pip install djangoajax</pre></div>
<p>Through Github:</p>
<div class="highlight highlight-sh"><pre>pip install -e git://github.com/yceruto/django-ajax#egg=djangoajax</pre></div>
<p>or simply with:</p>
<div class="highlight highlight-sh"><pre>$ python setup.py install</pre></div>
<p>2- Add <code>'django_ajax'</code> into the <code>INSTALLED_APPS</code> list.</p>
<p>3- Read usage section and enjoy its advantages!</p>
<h2>
<a id="usage" class="anchor" href="#usage" aria-hidden="true"><span class="octicon octicon-link"></span></a>Usage</h2>
<h3>
<a id="ajax-decorator" class="anchor" href="#ajax-decorator" aria-hidden="true"><span class="octicon octicon-link"></span></a><a href="https://github.com/ajax" class="user-mention">@ajax</a> Decorator</h3>
<div class="highlight highlight-python"><pre> <span class="k">from</span> django_ajax.decorators <span class="k">import</span> ajax
<span class="nf">@ajax</span>
<span class="kt">def</span> <span class="nf">my_view</span>(<span class="nv">request</span>)
do_something()</pre></div>
<p>When nothing is returned as result of view then returns (JSON format):</p>
<div class="highlight highlight-javascript"><pre> {<span class="s2">"status"</span><span class="o">:</span> <span class="m">200</span>, <span class="s2">"statusText"</span><span class="o">:</span> <span class="s2">"OK"</span>, <span class="s2">"content "</span><span class="o">:</span> <span class="kc">null</span>}</pre></div>
<h4>
<a id="sending-custom-data-in-the-response" class="anchor" href="#sending-custom-data-in-the-response" aria-hidden="true"><span class="octicon octicon-link"></span></a>Sending custom data in the response</h4>
<div class="highlight highlight-python"><pre> <span class="nf">@ajax</span>
<span class="kt">def</span> <span class="nf">my_view</span>(<span class="nv">request</span>):
c <span class="o">=</span> <span class="m">2</span> <span class="o">+</span> <span class="m">3</span>
<span class="k">return</span> {<span class="s1">'result'</span>: c}</pre></div>
<p>The result is send to the browser in the following way (JSON format)</p>
<div class="highlight highlight-javascript"><pre> {<span class="s2">"status"</span><span class="o">:</span> <span class="m">200</span>, <span class="s2">"statusText"</span><span class="o">:</span> <span class="s2">"OK"</span>, <span class="s2">"content"</span><span class="o">:</span> {<span class="s2">"result"</span><span class="o">:</span> <span class="m">5</span>}}</pre></div>
<h4>
<a id="combining-with-others-decorators" class="anchor" href="#combining-with-others-decorators" aria-hidden="true"><span class="octicon octicon-link"></span></a>Combining with others decorators</h4>
<div class="highlight highlight-python"><pre> <span class="k">from</span> django.contrib.auth.decorators <span class="k">import</span> login_required
<span class="k">from</span> django_ajax.decorators <span class="k">import</span> ajax
<span class="nf">@ajax</span>
<span class="nf">@login_required</span>
<span class="kt">def</span> <span class="nf">my_view</span>(<span class="nv">request</span>):
<span class="c1"># if the request.user is anonymous then this view not proceed </span>
<span class="k">return</span> {<span class="s1">'user_id'</span>: request.user.id}</pre></div>
<p>The JSON response:</p>
<div class="highlight highlight-javascript"><pre> {<span class="s2">"status"</span><span class="o">:</span> <span class="m">302</span>, <span class="s2">"statusText"</span><span class="o">:</span> <span class="s2">"FOUND"</span>, <span class="s2">"content"</span><span class="o">:</span> <span class="s2">"/login"</span>}</pre></div>
<h4>
<a id="template-response" class="anchor" href="#template-response" aria-hidden="true"><span class="octicon octicon-link"></span></a>Template response</h4>
<div class="highlight highlight-python"><pre> <span class="k">from</span> django.shortcuts <span class="k">import</span> render
<span class="k">from</span> django_ajax.decorators <span class="k">import</span> ajax
<span class="nf">@ajax</span>
<span class="kt">def</span> <span class="nf">my_view</span>(<span class="nv">request</span>):
<span class="k">return</span> render(request, <span class="s1">'home.html'</span>)</pre></div>
<p>The JSON response:</p>
<div class="highlight highlight-javascript"><pre> {<span class="s2">"status"</span><span class="o">:</span> <span class="m">200</span>, <span class="s2">"statusText"</span><span class="o">:</span> <span class="s2">"OK"</span>, <span class="s2">"content"</span><span class="o">:</span> <span class="s2">"<html>...</html>"</span>}</pre></div>
<h4>
<a id="catch-exceptions" class="anchor" href="#catch-exceptions" aria-hidden="true"><span class="octicon octicon-link"></span></a>Catch exceptions</h4>
<div class="highlight highlight-python"><pre> <span class="nf">@ajax</span>
<span class="kt">def</span> <span class="nf">my_view</span>(<span class="nv">request</span>):
a <span class="o">=</span> <span class="m">23</span> <span class="o">/</span> <span class="m">0</span> <span class="c1"># this line throws an exception</span>
<span class="k">return</span> a</pre></div>
<p>The JSON response:</p>
<div class="highlight highlight-javascript"><pre> {<span class="s2">"status"</span><span class="o">:</span> <span class="m">500</span>, <span class="s2">"statusText"</span><span class="o">:</span> <span class="s2">"INTERNAL SERVER ERROR"</span>, <span class="s2">"content"</span><span class="o">:</span> <span class="s2">"integer division or modulo by zero"</span>}</pre></div>
<h3>
<a id="ajaxmiddleware" class="anchor" href="#ajaxmiddleware" aria-hidden="true"><span class="octicon octicon-link"></span></a>AJAXMiddleware</h3>
<p>If you use AJAX quite frequently in your project, we suggest using the AJAXMiddleware described below.</p>
<p>Add <code>django_ajax.middleware.AJAXMiddleware</code> into the <code>MIDDLEWARE_CLASSES</code> list in <code>settings.py</code>.</p>
<p>All your responses will be converted to JSON if the request was made by AJAX, otherwise is return a HttpResponse.</p>
<blockquote>
<p><strong>Caution</strong>
If you use this middleware cannot use <code>@ajax</code> decorator.</p>
</blockquote>
<h3>
<a id="ajaxmixin-for-class-based-views" class="anchor" href="#ajaxmixin-for-class-based-views" aria-hidden="true"><span class="octicon octicon-link"></span></a>AJAXMixin for class-based views</h3>
<p><code>AJAXMixin</code> is an object that calls the AJAX decorator.</p>
<div class="highlight highlight-python"><pre> <span class="k">from</span> django.views.generic <span class="k">import</span> TemplateView
<span class="k">from</span> django_ajax.mixin <span class="k">import</span> AJAXMixin
<span class="kt">class</span> <span class="nc">SimpleView</span>(<span class="no">AJAXMixin</span>, <span class="no">TemplateView</span>):
template_name <span class="o">=</span> <span class="s1">'home.html'</span></pre></div>
<p>The JSON response:</p>
<div class="highlight highlight-javascript"><pre> {<span class="s2">"status"</span><span class="o">:</span> <span class="m">200</span>, <span class="s2">"statusText"</span><span class="o">:</span> <span class="s2">"OK"</span>, <span class="s2">"content"</span><span class="o">:</span> <span class="s2">"<html>...</html>"</span>}</pre></div>
<h3>
<a id="ajax-on-client-side" class="anchor" href="#ajax-on-client-side" aria-hidden="true"><span class="octicon octicon-link"></span></a>AJAX on client side</h3>
<p>Include <code>jquery.ajax.min.js</code> into <code>base.html</code> template:</p>
<div class="highlight highlight-html"><pre><span class="h"> <<span class="nt">script</span> <span class="na">type</span>=<span class="s2">"text/javascript"</span> <span class="na">src</span>=<span class="s2">"{% static 'django_ajax/js/jquery.ajax.min.js' %}"</span>></<span class="nt">script</span>></span></pre></div>
<p>Call to AJAX request using the <code>ajaxPost</code> or <code>ajaxGet</code> functions:</p>
<div class="highlight highlight-html"><pre><span class="h"> <<span class="nt">script</span> <span class="na">type</span>=<span class="s2">"text/javascript"</span>></span>
<span class="h"> ajaxPost(<span class="s1">'/save'</span>, {<span class="s1">'foo'</span><span class="o">:</span> <span class="s1">'bar'</span>}, <span class="kt">function</span>(<span class="nv">content</span>){</span>
<span class="h"> <span class="c1">//onSuccess</span></span>
<span class="h"> <span class="nf">alert</span>(content);</span>
<span class="h"> })</span>
<span class="h"> </<span class="nt">script</span>></span></pre></div>
<p>or</p>
<div class="highlight highlight-html"><pre><span class="h"> <<span class="nt">script</span> <span class="na">type</span>=<span class="s2">"text/javascript"</span>></span>
<span class="h"> ajaxGet(<span class="s1">'/'</span>, <span class="kt">function</span>(<span class="nv">content</span>){</span>
<span class="h"> <span class="c1">//onSuccess</span></span>
<span class="h"> <span class="nf">alert</span>(content);</span>
<span class="h"> })</span>
<span class="h"> </<span class="nt">script</span>></span></pre></div>
<p>If the response is not successful, it´s shown an alert with the message appropriated.</p>
<h3>
<a id="ajax-plugin-based-on-eldarion-ajax" class="anchor" href="#ajax-plugin-based-on-eldarion-ajax" aria-hidden="true"><span class="octicon octicon-link"></span></a>AJAX plugin Based on <a href="https://github.com/eldarion/eldarion-ajax">eldarion-ajax</a>
</h3>
<p>Include <code>jquery.ajax-plugin.min.js</code> into <code>base.html</code> template:</p>
<div class="highlight highlight-html"><pre><span class="h"> <<span class="nt">script</span> <span class="na">type</span>=<span class="s2">"text/javascript"</span> <span class="na">src</span>=<span class="s2">"{% static 'django_ajax/js/jquery.ajax-plugin.min.js' %}"</span>></<span class="nt">script</span>></span></pre></div>
<p>In this moment any tag with the attribute <code>data-ajax</code> will be controlled by ajax plugin. Each request is sent using AJAX and the response will be handle on JSON format.</p>
<p>The value of the attribute <code>data-success</code> will be used as callback function if the request is successful. This function is called with an argument that represent the content response:</p>
<div class="highlight highlight-html"><pre> <<span class="nt">a</span> <span class="na">href</span>=<span class="s2">"/hello-world/"</span> <span class="na">class</span>=<span class="s2">"btn btn-primary"</span> <span class="na">data-ajax</span>=<span class="s2">"true"</span> <span class="na">data-success</span>=<span class="s2">"processResponse"</span>>Show Alert</<span class="nt">a</span>></pre></div>
<p>Where "processResponse" in this case is a callback function:</p>
<div class="highlight highlight-html"><pre><span class="h"> <<span class="nt">script</span> <span class="na">type</span>=<span class="s2">"text/javascript"</span>></span>
<span class="h"> <span class="kt">function</span> <span class="nf">processResponse</span>(<span class="nv">content</span>) {</span>
<span class="h"> do_something(content);</span>
<span class="h"> }</span>
<span class="h"> </<span class="nt">script</span>></span></pre></div>
<h4>
<a id="process-fragments" class="anchor" href="#process-fragments" aria-hidden="true"><span class="octicon octicon-link"></span></a>Process fragments</h4>
<p>Inspired on <a href="https://github.com/eldarion/eldarion-ajax">eldarion-ajax</a> the data
received by the names <code>'fragments'</code>, <code>'inner-fragments'</code>, <code>'append-fragments'</code>
or <code>'prepend-fragments'</code> will be processed by default, unless you pass in the
request the option "process-fragments" equal false. Here's an example:</p>
<div class="highlight highlight-python"><pre> <span class="nf">@ajax</span>
<span class="kt">def</span> <span class="nf">fragments_view</span>(<span class="nv">request</span>):
data <span class="o">=</span> {
<span class="s1">'fragments'</span>: {
<span class="s1">'#id1'</span>: <span class="s1">'replace element with this content1'</span>
},
<span class="s1">'inner-fragments'</span>: {
<span class="s1">'#id2'</span>: <span class="s1">'replace inner content'</span>
},
<span class="s1">'append-fragments'</span>: {
<span class="s1">'.class1'</span>: <span class="s1">'append this content'</span>
},
<span class="s1">'prepend-fragments'</span>: {
<span class="s1">'.class2'</span>: <span class="s1">'prepend this content'</span>
}
}
<span class="k">return</span> data</pre></div>
<p>These data are sent in response:</p>
<div class="highlight highlight-javascript"><pre> {<span class="s2">"status"</span><span class="o">:</span> <span class="m">200</span>, <span class="s2">"statusText"</span><span class="o">:</span> <span class="s2">"OK"</span>, <span class="s2">"content"</span><span class="o">:</span> {
<span class="s2">"fragments"</span><span class="o">:</span> {<span class="s2">"#id1"</span><span class="o">:</span> <span class="s2">"replace element with this content1"</span>},
<span class="s2">"inner-fragments"</span><span class="o">:</span> {<span class="s2">"#id2"</span><span class="o">:</span> <span class="s2">"replace inner content"</span>},
<span class="s2">"append-fragments"</span><span class="o">:</span> {<span class="s2">".class1"</span><span class="o">:</span> <span class="s2">"append this content"</span>},
<span class="s2">"prepend-fragments"</span><span class="o">:</span> {<span class="s2">".class2"</span><span class="o">:</span> <span class="s2">"prepend this content"</span>}
}}</pre></div>
<p>Then, using AJAX (<code>ajax</code>, <code>ajaxPost</code> or <code>ajaxGet</code>) functions these fragments to be processed automatically before calling to success function.</p>
<div class="highlight highlight-html"><pre><span class="h"> <<span class="nt">script</span> <span class="na">type</span>=<span class="s2">"text/javascript"</span>></span>
<span class="h"> <span class="kt">function</span> <span class="nf">fragments</span>() {</span>
<span class="h"> ajaxGet(<span class="s1">'/fragments-view-url'</span>, <span class="kt">function</span>(<span class="nv">content</span>){</span>
<span class="h"> <span class="nf">alert</span>(<span class="s1">'The fragments was processed successfully!'</span>);</span>
<span class="h"> });</span>
<span class="h"> }</span>
<span class="h"> </<span class="nt">script</span>></span></pre></div>
<p>If you do not want to process the fragments never, modify the AJAX configuration
that comes by default:</p>
<div class="highlight highlight-html"><pre><span class="h"> <<span class="nt">script</span> <span class="na">type</span>=<span class="s2">"text/javascript"</span>></span>
<span class="h"> ajax.DEFAULTS[<span class="s2">"process-fragments"</span>] <span class="o">=</span> <span class="kc">false</span>; <span class="c1">//true by default</span></span>
<span class="h"> </<span class="nt">script</span>></span></pre></div>
<p>or as option on the request:</p>
<div class="highlight highlight-html"><pre><span class="h"> <<span class="nt">script</span> <span class="na">type</span>=<span class="s2">"text/javascript"</span>></span>
<span class="h"> <span class="kt">function</span> <span class="nf">fragments</span>() {</span>
<span class="h"> ajaxGet(<span class="s1">'/fragments-view-url'</span>, <span class="kt">function</span>(<span class="nv">content</span>){</span>
<span class="h"> do_something_with(content.fragments);</span>
<span class="h"> }, {<span class="s2">"process-fragments"</span><span class="o">:</span> <span class="kc">false</span>});</span>
<span class="h"> }</span>
<span class="h"> </<span class="nt">script</span>></span></pre></div>
<h3>
<a id="enjoy" class="anchor" href="#enjoy" aria-hidden="true"><span class="octicon octicon-link"></span></a>Enjoy!</h3>
</section>
</div>
<!-- FOOTER -->
<div id="footer_wrap" class="outer">
<footer class="inner">
<p class="copyright">django-ajax maintained by <a href="https://github.com/yceruto">yceruto</a></p>
<p>Published with <a href="http://pages.github.com">GitHub Pages</a></p>
</footer>
</div>
</body>
</html>