| | 200 | {{{ |
| | 201 | #!html |
| | 202 | <a name="P2"><h1>Handling the form when JavaScript is desactivated.</h1></a> |
| | 203 | }}} |
| | 204 | If a user has desactivated his browser javascript support, or is using a text mode browser, we need a way of making the previous marking button submit the mark to the server which should this time return an html template instead of data to the Ajax call. |
| | 205 | |
| | 206 | == Updating the form HTML (details.html template)== |
| | 207 | This time we put a submit type input inside the form instead of the button type in part 1. |
| | 208 | type="submit" as indicates its name, submits the form to the server,we will need a way of stopping this behavior using javaScript. |
| | 209 | |
| | 210 | {{{ |
| | 211 | <form enctype="multipart/form-data" id="myForm" method="post"> |
| | 212 | <span id="mark_message">{{mark_message}}</span> |
| | 213 | <select name="select_mark"> |
| | 214 | [...] |
| | 215 | </select> |
| | 216 | '''<input id="sendFormButton" type="submit" value="Notez" />''' |
| | 217 | </form> |
| | 218 | }}} |
| | 219 | |
| | 220 | Now, how can we tell our details method in view.py to know if it comes from a normal submit request or an Ajax request ? |
| | 221 | Two solutions, |
| | 222 | |
| | 223 | The '''first''' uses a hidden variable in form.html and an added content element in the JS part. |
| | 224 | |
| | 225 | {{{ |
| | 226 | function sendForm() |
| | 227 | { |
| | 228 | dojo.byId("mark_status").innerHTML = "Loading ..."; |
| | 229 | dojo.io.bind({ |
| | 230 | url: '.', |
| | 231 | handler: sendFormCallback, |
| | 232 | content: {"js", "true"}, |
| | 233 | formNode: dojo.byId('myForm') |
| | 234 | }); |
| | 235 | } |
| | 236 | |
| | 237 | |
| | 238 | [...] |
| | 239 | <form enctype="multipart/form-data" id="myForm" method="post"> |
| | 240 | [...] |
| | 241 | <input type="hidden" name="js" value="false"> |
| | 242 | [...] |
| | 243 | }}} |
| | 244 | |
| | 245 | With this, in our django method in view.py we can test for request["js"]=="true" it would means that Js is activatd and we return the appropriate answer to the Ajax request. |
| | 246 | |
| | 247 | The '''second''' uses the url to pass a new variable ajax_or_not to the detail method. |
| | 248 | |
| | 249 | {{{ |
| | 250 | #!python |
| | 251 | def details(request, r_slug, r_cat, ajax_or_not=None): |
| | 252 | [...] |
| | 253 | }}} |
| | 254 | |
| | 255 | We modify the url.py to accept this new parameter. |
| | 256 | {{{ |
| | 257 | #!python |
| | 258 | (r'^recettes/(?P<r_cat>[-\w]+)/(?P<r_slug>[-\w]+)/(?P<ajax_or_not>.*)$', 'cefinban.recettes.views.details'), |
| | 259 | }}} |
| | 260 | |
| | 261 | The dojo binding need to append a variable to the original document url, to make ajax_or_not not None. |
| | 262 | |
| | 263 | {{{ |
| | 264 | function sendForm() |
| | 265 | { |
| | 266 | dojo.byId("mark_status").innerHTML = "Loading ..."; |
| | 267 | dojo.io.bind({ |
| | 268 | url: './ajax/', |
| | 269 | handler: sendFormCallback, |
| | 270 | formNode: dojo.byId('myForm') |
| | 271 | }); |
| | 272 | } |
| | 273 | }}} |
| | 274 | |
| | 275 | == New details method in view.py== |
| | 276 | |
| | 277 | We just need to test for the existence of ajax_or_not |
| | 278 | |
| | 279 | {{{ |
| | 280 | #!python |
| | 281 | def details(request, r_slug, r_cat, ajax_or_not=None): |
| | 282 | [...] |
| | 283 | if request.POST: |
| | 284 | [...] same as part 1 |
| | 285 | # except here we check ajax_or_not |
| | 286 | if ajax_or_not: |
| | 287 | # use json for python js exchange |
| | 288 | # it was a french string, if we dont't use unicode |
| | 289 | # the result at the output of json in Dojo is wrong. |
| | 290 | message = unicode( message, "utf-8" ) |
| | 291 | jsonList = simple_json.dumps((my_mark, total, form_message ,message)) |
| | 292 | return HttpResponse(jsonList) |
| | 293 | |
| | 294 | return render_to_response('recettes/details.html', {'r': r, 'section':'index', |
| | 295 | 'mark_status':message , 'mark_message':form_message, 'my_mark': my_mark}, |
| | 296 | context_instance=RequestContext(request),) |
| | 297 | }}} |
| | 298 | |
| | 299 | |
| | 300 | |