Snippet List
1) Install django-extensions (requires werkzeug)
2) Paste snippet into settings.py
3) manage.py runserver_plus
Now you should be able to open files in textmate by clicking the file links in the werkzeug error pages. It will also take you to the correct line number and highlight files that are in your project directory in a different color.
- debug
- error
- debugging
- textmate
The default traceback sent by email when an error occurs, usually gives too little information comparing it to the error page in the DEBUG mode. This snippet guerilla-patches error handling and sends by email the same information as you would see in DEBUG mode.
To set it up, add the snippet to any `models.py` of an installed app.
(I wonder why this hasn't been implemented in the core)
Prevents error flooding on high traffic production sites. Particularly useful to prevent clogging email servers etc.
See [discussion](http://groups.google.com/group/django-developers/browse_thread/thread/1dabc9139dd750ca/1d86fdca23189a7d?lnk=gst&q=error#1d86fdca23189a7d) and [closed ticket](http://code.djangoproject.com/ticket/11565).
Uses memcache if it can, or falls back to local, in-process memory where unavailable (down, etc).
Tweak to your needs using setting ERROR_RATE_LIMIT (seconds).
Requires Django 1.3+ or trunk r13981+
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
Often its useful to get error information for ajax/javascript errors happening on various clients. This can go to something like this:
# error_sink
def error_sink(request):
# post request, with event name in "event", and event data in "data"
context = request.REQUEST.get("context", "")
context = cgi.parse_qs(context)
context["data"] = cgi.parse_qs(context.get("data", [""])[0])
context["user"] = request.vuser
context["referrer"] = request.META.get('HTTP_REFERER', "referrer not set")
context = pformat(context)
send_mail(
"ajax error", context, "[email protected]",
["[email protected]",], fail_silently=True
)
return JSONResponse({"status": "ok" })
# }}}
- ajax
- jquery
- error
- reporting
You can place this code above your form and it will list out all errors in your form if there are errors. Sometimes I like listing the errors at the top of the form because I think it looks cleaner. Make sure you add error_messages={'required': 'My detailed error here'} in your view.py for your form.
This is a light-weight flash implementation. Instead of hitting the database it uses cookies. The messages are shown to the user only once, after that the cookies are deleted.
I tested it on Google App Engine, but it should work on vanilla Django as well, there's no GAE specific code.
To set up, add `"path.to.flash.Middleware"` to the `MIDDLEWARE_CLASSES` list.
Also add `'path.to.flash.context_processor'` to the `TEMPLATE_CONTEXT_PROCESSORS` list.
In your views, import and call `flash_error(msg)` and `flash_notice(msg)` passing the message that you want to show.
In your base template use this mark up:
{% if flash.notice %}
<div id="flash_notice">
<p>{{ flash.notice }}</p>
</div>
{% endif %}
{% if flash.error %}
<div id="flash_error">
<p>{{ flash.error }}</p>
</div>
{% endif %}
And finally, add this to your CSS file changing colours as necessary:
#flash_error {
margin-top: 30px;
padding: 20px;
background-color: #FFCCCC;
border: solid 1px #CC0000;
}
#flash_notice {
margin-top: 30px;
padding: 20px;
background-color: #CCFFCC;
border: solid 1px #00CC00;
}
#flash_error p, #flash_notice p {
margin: 0px;
}
Please comment if you notice any FUs. I'm new to Django and will appreciate any feedback.
- error
- flash
- rails
- notification
- notice
Handles exceptions from AJAX code, giving more useful errors and tracebacks.
(By the way, all these new snippets are extracts from [django-webapp](http://code.google.com/p/django-webapp/).)
This exact code is not well tested, but it's refactored from some code we use in production.
- middleware
- ajax
- error
- exception
<code>
is_loaded = False
if not is_loaded:
is_loaded = True
#... ExceptionHandlingMiddleware as EHM
import views
EHM.append(views.AuthFailError, views.auth_fail) # when AuthFailError thrown it redirects to `auth_fail` view function.
</code>
- middleware
- error
- exception
- custom
- handling
This middleware makes the admin error emails a lot more informative: you get the same HTML response that you get with `DEBUG=True`.
It uses the base class defined in [#638](http://www.djangosnippets.org/snippets/638/).
You will probably want to apply the patch for [#6748](http://code.djangoproject.com/ticket/6748) to help avoid slowdowns caused by unintentional database queries. As the ticket (and django-developers thread) notes, it isn't foolproof; you may still find this executing database queries.
- admin
- debug
- error
- mail
- 500
I wanted to mark rows with an erroneous input with 'class="error"' and move the errorlist below the input tag so I wrote a NormalRowFormatter which behaves like a format string by overwriting \_\_mod\_\_.
Examples:
class MyForm(PrettyBaseForm, forms.Form):
# add your fields here
SomethingForm = form_for_model(Something, form=PrettyBaseForm)
- newforms
- error
- baseform
- as_table
- pretty
- prettybaseform
- _html_output
- style
14 snippets posted so far.