Inserts the output of a view, using fully qualified view name (and then some
args), a or local Django URL.
{% view view_or_url arg[ arg2] k=v [k2=v2...] %}
This might be helpful if you are trying to do 'on-server' AJAX of page
panels. Most browsers can call back to the server to get panels of content
asynchonously, whilst others (such as mobiles that don't support AJAX very
well) can have a template that embeds the output of the URL synchronously
into the main page. Yay! Go the mobile web!
Follow standard templatetag instructions for installing.
**IMPORTANT**: the calling template must receive a context variable called
'request' containing the original HttpRequest. This means you should be OK
with permissions and other session state.
**ALSO NOTE**: that middleware is not invoked on this 'inner' view.
Example usage...
Using a view name (or something that evaluates to a view name):
{% view "mymodule.views.inner" "value" %}
{% view "mymodule.views.inner" keyword="value" %}
{% view "mymodule.views.inner" arg_expr %}
{% view "mymodule.views.inner" keyword=arg_expr %}
{% view view_expr "value" %}
{% view view_expr keyword="value" %}
{% view view_expr arg_expr %}
{% view view_expr keyword=arg_expr %}
Using a URL (or something that evaluates to a URL):
{% view "/inner" %}
{% view url_expr %}
(Note that every argument will be evaluated against context except for the
names of any keyword arguments. If you're warped enough to need evaluated
keyword names, then you're probably smart enough to add this yourself!)
I work with multiple projects, many of which have multiple custom management commands defined. It can be hard to remember them, and slow to pick them out of the "manage.py help" list.
This quickie command lists all of a project's custom commands (along with their help text). Writing it was easy after looking at the source of django.core.management.
Open questions include: how do you decide which app to put this command in? Should this command list itself?
This is a conditional templatetag decorator that makes it *very* easy to write template tags that can be used as conditions. This can help avoid template boilerplate code (e.g. setting a variable in your template to be used in a condition).
All you have to do is define a function with expected parameters that returns True or False. Examples are in the code.
Generate QR Code image from a string with the Google charts API
http://code.google.com/intl/fr-FR/apis/chart/types.html#qrcodes
Exemple usage in a template
{{ my_string|qrcode:"my alt" }}
will return the image tag with
* src: http://chart.apis.google.com/chart?chs=150x150&cht=qr&chl=my_string&choe=UTF-8
* alt: my alt"
As there is no straight way to re-produce the real tabular inline formsets you get in django-admin, here is how this template has to look like if you do it form your own formsets generated from formset factories.
By enabling this backend:
AUTHENTICATION_BACKENDS = (
'path.to.my.backends.CaseInsensitiveModelBackend',
)
Your users will now be able to log in with their username, no matter whether the letters are upper- or lower-case.
Truncates a string after a given length, keeping the last word complete.
This filter is more precise than the default `truncatewords` filter.
Words length vary too much, 10 words may result in 40 or 70 characters, so cutting by character count makes more sense.
There is a [blog post](http://ricobl.wordpress.com/2008/12/23/templates-django-filtro-truncatewords-melhorado/) about this snippet (in Portuguese).
As I was unable to find good examples to render a Form with two or more inlineformsets.
Therefor I have posted this to Django snippets.
This code is little different from another snippet with a Form with one InlineFormSet (the prefixes are necessary in this situation).
The example shows a person's data together with two inline formsets (phonenumbers and addresses) for a person.
You can add, update and delete from this form.
As I was unable to find good examples to render an inlineformset together, I have posted this to Django snippets.
The example shows a person's data together with the phonenumbers for that person.
You can add, update and delete from this form.
Often I want fields in my models to be unique - Django's `unique` works too late in model form validation and will throw an exception unless you check for it by hand. This is a bit of code that cleans up the boiler plate of checking if different fields are unique.
Set `exclude_initial` to `False` if you want to raise an error if the unique field cannot be set to whatever value the instance had before.
**Update**
Thanks fnl for rightly pointing out that Django's `unique=True` does check for this - just make sure to pass `instance=obj` when you're initializing your forms. _HOWEVER_ a problem you'll typically run into with this is though you want a field to unique, you also want multiple entries to be able to be `blank`. This can help you!
The default server_error view uses Context instead of RequestContext. If you were depending on a context processor to make MEDIA_URL available in your templates, your 500.html template will not render with the correct image paths. This handler adds MEDIA_URL (and nothing else) back to the context that is sent to the template.
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" })
# }}}
This provides some basic cryptographic fields using pyCrypto. All encryption/decription is done transparently and defaults to use AES. Example usage:
class DefferredJunk(models.Model):
semi_secret = EncryptedCharField(max_length=255)
Generic Relations with django.contrib.contenttypes.generic.GenericForeignKey work well for models but the admin interface just shows the object id as an integer with no easy way to lookup the id of a new object. This jquery javascript adds a "Lookup <ContentType Name>" link next to the object id field in the admin interface.
This is done by reusing the showRelatedObjectLookupPopup() function provided with django which is used when the field is included in raw_id_fields (a little magnifying glass linked to popup object selector shows up). This essentially works the same way but changes which model's objects are shown and selected in the popup based on the ContentType selected for object_type
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.