This view acts as an extension to the object_detail generic view in django.views.generic.object_list. The standard generic view can only filter the queryset by object_id or slug; this view allows you to filter by any parameter you like, as well as multiple parameters.
The usage is the same as the standard object_detail view except that you must also give the parameter 'filters', which should be an array of what you would like to filter the url values as, and instead of 'slug' or 'object_id' as the regex parameter in the URL, use 'value1', 'value2', etc.
Example: If you have a list of companies, each with multiple branches, you may want a branch details page. A URL for this may look as follows: http://www.mysite.com/company/company-slug/branch-slug/. To implement this simply use the urlpattern example give,
**Please use the updated version http://djangosnippets.org/snippets/2596/**
Unfortunately the built in Django JSON serialzer encodes GeoDjango GeometyrField as simple text. This snippet extends django's serializer and adds support for GeoJson format.
Built in JSON serializer output:
[{"pk": 1, ... "geopoint": "POINT (-76.5060419999999937 44.2337040000000030)" ... }]
GeoJSON serializer ouput:
[{"pk": 1, ... "geopoint": {"type": "Point", "coordinates": [-76.503296000000006, 44.230956999999997]}}]
The above snippet makes all of the settings.py variables accessible in templates. Add this to the context_processors.py file inside your app directory. After that add the below line to the TEMPLATE_CONTEXT_PROCESSORS in your settings.py.
'*app_name*.context_processors.access_to_settings'
In templates sometimes you need to display some menu by checking whether the user is logged in or not. So use the above filter as shown below
{% with request|check_login as logout %}
{% if logout%}
display something....
{% endif %}
{% endwith %}
The above snippet can be added as a custom filter for rendering code snippets in django templates. A simple '|render' next to any source code will do. To add css, use the following command to generate css file.
pygmentize -S emacs -f html > pygments-colorful.css
And link this css file to the template.
Simple piece of middleware that redirects all requests to **settings.FIRSTRUN_APP_PATH**, until a lockfile is created.
Tested on 1.3 only, but I do not believe it requires any special functionality beyond that provided in 1.1
This is useful if you need to force a user to run an installer, or do some configuration before your project can function fully.
At first glance, such a thing would generate a lot of hits on the disk. However, once the lockfile has been created, the middleware unloads itself, so when a project is in a production environment, the lockfile is only checked once per process invocation (with passenger or mod_wsgi, that's not very often at all).
Once your user has completed your FirstRun app, simply create the lockfile and the project will function as normal.
For it to function, the following settings must be configured:
* **settings.PROJECT_PATH** - absolute path to project on disk (e.g. */var/www/project/*)
* **settings.FIRSTRUN_LOCKFILE** - relative path of the lockfile (e.g. */.lockfile*)
* **settings.FIRSTRUN_APP_ROOT** - relative URL of the App you want to FirstRun (eg.*/firstrun/*)
I often need to filter my change_list by author but my User table is usually pretty crowded and adding the User FK to list_filter would end up with hundreds of user links in the sidebar. This snippets replace those hundreds of links by a simple HTML <input>.
The tag generates a parameter string in form '?param1=val1¶m2=val2'.
The parameter list is generated by taking all parameters from current
request.GET and optionally overriding them by providing parameters to the tag.
This is a cleaned up version of http://djangosnippets.org/snippets/2105/. It
solves a couple of issues, namely:
* parameters are optional
* parameters can have values from request, e.g. request.GET.foo
* native parsing methods are used for better compatibility and readability
* shorter tag name
Usage: place this code in your appdir/templatetags/add_get_parameter.py
In template:
{% load add_get_parameter %}
<a href="{% add_get param1='const' param2=variable_in_context %}">
Link with modified params
</a>
It's required that you have 'django.core.context_processors.request' in
TEMPLATE_CONTEXT_PROCESSORS
This is auth_data_required decorator analogous to login_required where authentication data must come in POST of request.
It's useful for API. I took the idea from [Tumblr API](http://www.tumblr.com/docs/en/api).
These generic views extend default views so that they also do permission checking on per-object basis.
* detail, update and delete - check access for user
* create - create permissions for user on object
* list - narrow object list with permissions
Classes prefixed with Owned are example implementation where user has access to object if designed object attribute references him.
Example:
`create_article = OwnedCreateView.as_view(owner='creator', model=Article, form_class=ArticleForm, success_url='/articles/article/%(id)d')`
Tag for iterating through the fields of an object from a template.
The tag is passed two string arguments, the name of the model to use (which is then looked up in the context dictionary), and the format string. The format string should include two string place holders.
The tag will output each field name and value according to the format string supplied.
Syntax:
{% math <argument, ..> "expression" as var_name %}
Evaluates a math expression in the current context and saves the value into a variable with the given name.
"$<number>" is a placeholder in the math expression. It will be replaced by the value of the argument at index <number> - 1.
Arguments are static values or variables immediately after 'math' tag and before the expression (the third last token).
Example usage,
{% math a b "min($1, $2)" as result %}
{% math a|length b|length 3 "($1 + $2) % $3" as result %}
Use in a with statement to set the translation to another locale for a block
>>> from django.utils.translation import ugettext
>>> ugettext('title')
u'title'
>>> with Translation('fr') as locale:
...: print locale.locale
...: print ugettext('title')
...:
...:
fr
titre
>>> ugettext('title')
u'title'
Motivation: We can't use GeometryCollections to do filters, etc in GeoDjango due to incomplete underlying libraries. But with this CollectionFrom field we can get all the benefits of working with GeometryCollections but still query based on points, lines, polys.
If you're using GeometryCollectionFields and see this error:
DatabaseError: Relate Operation called with a LWGEOMCOLLECTION type. This is unsupported.
Then this will probably be helpful for you.