Snippet List
Позволяет получить типизированный словарь из входных параметров.
Может быть использован, например, для дальнейшей передаче параметров в objects.filter(**rez).
Django does not have a clean, built-in mechanism to separate GET and POST implementations. This simple decorator provides this behavior. Django does provide an alternate way using class-based views, but defining class for each of your view functions may be an overkill. You can name the get and post functions anything you wish, but you need to make sure they are returned in the same order (get first and then post).
Example usage:
@formview
def edit(request, id):
form = EditForm(id, request.POST or None)
def get():
return render(request, 'edit.html', {'form' : form})
def post():
if form.is_valid():
form.save(id)
return redirect('list')
return get, post
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
- get
- request
- parameters
- add
Example usage:
Add static var with static value to get :
{% urlget 'var'='val' %}
Add dynamic val (from template vars) to static variable:
{% urlget 'var'=val %}
Using dynamic variable names works similiar - adding dynamic varialbe (from template vars) :
{% urlget var='val' %}
Clearing variable from GET string :
{% urlget 'var'='' %}
Retrieving GET string:
{% urlget %}
This template tag attempts to get specific GET variables from request.GET. If such variables exist, it shows them as a query string (with optional "include_ampersand" mode when it puts an "&" at the end if there is a result string, or a "?" if there's none: it is used when you need to add a new variable to the query string) or as hidden input fields ("html_form" mode).
- get
- template-tag
- request
- variables
- get-variables
This tag is designed to facilitate pagination in the case where both the page number and other parameters (eg. search criteria) are passed via GET.
It takes one argument - a dictionary of GET variables to be added to the current url
Example usage:
{% for page_num in results.paginator.page_range %}
<a href="{% append_to_get p=page_num %}">{{ page_num }}</a>
{% endfor %}
Note that the passed arguments are evaluated within the template context.
This snippet shows a way to preserve GET arguments with pagination. Many people make mistakes to omit the query arguments besides page arguments for the pagination, and making sure correct may sphagettize your code.
- template
- get
- view
- pagination
- arguments
This template tag takes the current GET query, and modifies or adds the value you specify. This is great for GET-query-driven views, where you want to provide URLs which reconfigure the view somehow.
**Example Usage:**
`{% get_string "sort_by" "date" %}` returns `all=your¤t=get&variables=plus&sort_by=date`
- templatetag
- get
- context
- request
I'm using Django 0.96 for a project, and the url tag does not have all the capabilities I need.
I want a way to persist all or some of the GET parameters between requests. I also want a way to add to the current url a list of extra parameters, or nullify certain parameters.
In this snippet I am defining 2 tags ... link_persist and link_add.
It only works with Django 0.96, as the API changed ... but maybe a good soul can fix it for 1.0, as I haven't had any time available.
A tip for usage: if you specify a parameter as being the empty string, or None, the parameter will be removed from the link. When you specify a parameter already available in GET, it will replace it.
RowCacheManager is a model manager that will try to fetch any 'get' (i.e., single-row) requests from the cache. ModelWithCaching is an abstract base model that does some extra work that you'll probably want if you're using the RowCacheManager. So to use this code, you just need to do two things: First, set objects=RowCacheManager() in your model definition, then inherit from ModelWithCaching if you want the invalidation for free. If you are unusually brave, use the metaclass for ModelWithCaching and you won't even need the "objects=RowCacheManager()" line.
- get
- model
- manager
- model-inheritance
- caching
I tried to use [Joshua's](http://www.djangosnippets.org/users/joshua/) nice and very useful [getattr template filter (#38)](http://www.djangosnippets.org/snippets/38/), but ran into a few problems.
I used it on objects outside of my control (admin internals, coughcough) and on some of them the code didn't catch the resulting exceptions. So I improved the error handling a bit.
Furthermore, the code now also returns the *value of a callable* instead of the callable *itself* (last 4 lines).
Looking at my code though, it can certainly be improved further.
Put inside `mysite/templatetags/getattr.py`
Add `mysite` to your `INSTALLED_APPS`
In your template:
{% load getattr %}
{{ myobject|getattr:"theattr,default value" }}
Thanks to pterk for optimizations! \\o/
14 snippets posted so far.