Django for Designers/CRUD: Difference between revisions

imported>Aldeka
imported>Aldeka
 
(3 intermediate revisions by the same user not shown)
Line 1:
== Part 4: CRUD ==
 
<div class="instructor">Time: 2 hours, 25 minutes</div>
 
<div class="instructor">Right now, we have a nice website that displays data from our database. Unfortunately, though, while we have a mock bookmark form in the app header, currently the only way to create new bookmarks is in the Python shell. That's not fun at all.</div>
Line 374 ⟶ 376:
Reload your development server and your web page, and try creating a bookmark. It should appear right away on the page now!
 
Try submitting the form with a tag but no URL. <div class="instructor">Nothing happens, right?Eek.</div> IfWe you checksee the serverbrowser logtry into yourrender terminalthe entire page inside the window,page--because youwe'llre seeredirecting back to the annormal errorindex message:view!
 
Eek. Our index view doesn't properly handle the case where the request is a POST, but the form doesn't validate, and Django noticed this and returned an error. Let's fix this by editing the ''index'' function in ''views.py'' again:
<source lang="bash">
ValueError: The view bookmarks.views.index didn't return an HttpResponse object.
</source>
 
Eek. Our index view doesn't handle the case where the request is a POST, but the form doesn't validate, and Django noticed this and returned an error. Let's fix this by editing the ''index'' function in ''views.py'' again:
 
<source lang="python">
Line 426 ⟶ 424:
 
<div class="instructor">In the real world, if you were doing lots of this sort of manipulation, instead of AHAH you might want to be using a Javascript framework such as Backbone to avoid getting confused, messy code. You'd also want to use templates for the elements that your Javascript hooks are adding and modifying. Ideally, you'd want those templates to be the same ones that your Django application used, to avoid repeating yourself! There's a lot of ways Django users deal with these problems. One way to do this is to use Django as an API data engine and do all the routing, application logic, and template rendering--at least, on the JS-heavy pages--via a Javascript-based template language. [http://django-tastypie.readthedocs.org/en/latest/ Tastypie] is a popular Django application for making APIs for this sort of thing. Another approach is to teach Django to speak the language of a JS-based templating language. Projects like [https://github.com/yavorskiy/django-handlebars django-handlebars] or [https://github.com/mjumbewu/djangobars djangobars] are examples of this approach!</div>
 
[[Django_for_Designers/Styling|Next page]]
Anonymous user