Snippet List
I have a ModelForm which includes m2m field to images. User can upload images and crop them with my cool jquery cropper, then areas are saved as images and their IDs and thumbnail URLs are passed back to page and included as thumbnails with hidden inputs. I have no problem while form have no errors, and when it does, i can not just simply display thumbnails — all I have is IDs, and form has no objects to iterate cause instance was not saved, and as it was not save it has no id and as it has no id it can not have m2m relations. So i wrote templatetag which returns queryset based on ids. It works like that:
<ul id="lot-images" class="thumb-uploaders">
{% if form.errors %}
{% load load_form_objects %}
{% load_form_objects lot_form.images as images %}
{% for image in images %}
{% if image %}
<li>
<input type="hidden" name="images" value="{{image.id}}"/>
<div class="image">
<div class="mask"></div>
<img src="{{ image.get_thumbnail_url }}" alt=""/>
<a href="#" class="delete">Удалить</a>
</div>
</li>
{% else %}
<li><a class="upload-medium"></a></li>
{% endif %}
{% endfor %}
{% else %}
<li><a class="upload-medium"></a></li>
<li><a class="upload-medium"></a></li>
<li><a class="upload-medium"></a></li>
<li><a class="upload-medium"></a></li>
<li><a class="upload-medium"></a></li>
{% endif %}
</ul>
A decorator that can be applied to your views to turn ObjectDoesNotExist exceptions into Http404 exceptions. This means people will see a "Page not found" error rather than an "Internal Server Error" when they are request something that does not exist in the database.
Example:
@raise_404
def show_event(request, id):
event = Event.objects.get(id)
return render_to_response('events/show_event.html', { 'event' : event })
- error
- exception
- 404
- errors
- exceptions
- objectdoesnotexist
- doesnotexist
This is simple validation weather a string is close enough to what we want. First param is keyword we are comparing to Second is user's input and third is tolerance level. Its very rudimentary. I have my mind fixed upon some imperfections. I am trying to make it good for human like(crazy keyboard) error.
- comparison
- errors
- typo
- validate
I recently got a form working via jQuery and Django. This was not easy for me to do and I thought I'd record my finding here.
The form submits via jQuery and the "form" plugin. Please visit jQuery's home page to find all those links.
This code handles:
* urls.py -- passing both normal and 'Ajax' urls to a view.
* views.py -- Handling both kinds of requests so that both normal and ajax submits will work.
* The HTML template with the script for submitting and some bling.
Error handling
===
I like to stay DRY so the idea of checking the form for errors in javascript *and* checking it in Django irks me. I decided to leave that up to Django, so the form submits and gets validated on the server. The error messages are sent back to the browser and then displayed.
- ajax
- urls
- fields
- views
- jquery
- form
- errors
8 snippets posted so far.