This assumes that you have a method called **decode_signed_request** which will validate the signed_request parameter and return None if the validation check fails.
A similar method can be found here - https://github.com/iplatform/pyFaceGraph/blob/70e456c79f1ac1c7eddece03af323346a00481ef/src/facegraph/canvas.py
In one situation I needed to join strings in template, so I wrote this filter.
Use it like this:
1)
var = 23
{{"I have eat %d apples today."|joinstrings:var}}
-> "I have eat 23 apples today."
var = '23'
{{"I have eat %s apples today."|joinstrings:var}}
-> "I have eat 23 apples today."
2)
var = [23, 45] #or any iterable object (except string - see pt. 1)
{{"I have eat %d apples and %d pears today."|joinstrings:var}}
-> "I have eat 23 apples and 45 pears today."
Ok let's descrive what i have done
I subclassed the django admin to create a form that makes you choose if activate a delete and replace login inside your admin.
Then i have added a form with a modelChoiceField to make you select another model instance when you are selecting an istance to delete. If you select another instance the current instance will be replaced.
A custom templatetag for inlining image in the browser.
The principe is to base64 encode the image and avoid a http request.
There is a cache handling, you just have to specify a writable directory.
An example of the utilisation (template part): [http://djangosnippets.org/snippets/2267/](http://djangosnippets.org/snippets/2267/)
The explication on [http://raphaelbeck.wordpress.com/2010/11/14/make-inline-images-to-improve-performance-with-django-template-tags/](http://raphaelbeck.wordpress.com/2010/11/14/make-inline-images-to-improve-performance-with-django-template-tags/)
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 %}
An accept middleware, which is based on the code of http://djangosnippets.org/snippets/1042/ but adds a workaround for the buggy accept header, sent from webkit browsers such as safari and chrome.
The workaround affects any accept header, that has xml and (x)html in the best q, but also the xml mediatype at first place in the list.
If this is the case, the header is rearanged, by shifting the xml mediatype to become the last element of the best quality entries in the header.
If the workaround did manipulate the header, and there is a html entry in the list with lower quality as an xhtml entry that is also in the list (with best q), then the html entry is also raised in q to be one entry in front of xml.
This is an expanded version of ["Resolve URLs to view name"](http://djangosnippets.org/snippets/1378/) without the monkey-patching.
Simply pass in a URL such as '/events/rsvp/some_conference/' and you'll get back the view name or function (if name isn't available) and the arguments to it, eg 'events_rsvp', [], {'event_slug':'some_conference'}.
Example (blatantly copied from previous snippet):
=== urlconf ====
urlpatterns = patterns(''
(r'/some/url', 'app.views.view'),
url(r'/some/other/(?P<url>\w+)', 'app.views.other.view', name='this_is_a_named_view'),
url(r'/yet/another/(?P<url>\w+)/(\d+)', 'app.views.yetanother.view', name='one_with_args'),
)
=== example usage in interpreter ===
>>> from some.where import resolve_to_name
>>> print resolve_to_name('/some/url')
('app.views.view',[],{})
>>> print resolve_to_name('/some/other/url')
('this_is_a_named_view',[],{'url':'url'})
>>> print resolve_to_name('/yet/another/url/5')
('one_with_args',[5],{'url':'url'})
From [fahhem.com](http://fahhem.com/) and [Recrec Labs](http://recreclabs.com/)
This is an updated snippet based on http://djangosnippets.org/snippets/2260/
The updated snippet can limit the filtering options for a foreign key field to only those entries that are related to the current model. I.e. if you have an Author model with a FK to Institution model, you can configure Author's changelist to include a filter on Institution, but only allow you to select institutions that have authors. Institutions that do not have authors won't show up on the list.
To enable this, in your model's ModelAdmin class, set
<fieldname>_fk_filter_related_only=True
<fieldname>_fk_filter_name_field=<display this field of the related model in filter list>
For example, in your AuthorAdmin class, you can do
institution_fk_filter_related_only=True
institution_fk_filter_name_field='name'
Note that for the effect described above to work, you just need the last few lines of the big else clause in __init__, so if you don't care about filtering by FK property, you can just grab those few lines and create a simpler FilterSpec.
Testing low-level functionality sometimes requires a WSGIRequest object. An example of this is testing template tags.
This will monkey-patch the test Client object to return WSGIRequest objects
Normal Django behavior:
>>> client.get('/')
<HttpResponse >
With this code, get the request object:
>>> client.request_from.get('/')
<WSGIRequest >
Installation:
For this to work, you simply need to import the contents of this file.
If you name this file `clientrequestpatch.py`, do this inside your Django tests.
from django.test.testcases import TestCase
from myproject.test import clientrequestpatch
Django's builtin `removetags` filter removes the supplied tags, but leaves the enclosed text alone. Sometimes you need the complete tag, including its content to go away. Example:
<h1>Some headline</h1>
<p>Some text</p>
Applying `removetags:"h1"` to this html results in
Some headline
<p>Some text</p>
while `killtags:"h1"` leaves
<p>Some text</p>
Create in your template dir html files named example.static.html and with this snippet you can get the static page with the url /example/. If you put static file in a sub-directory, the url will be /sub-directory/example/
**Example:**
`static_urls = StaticUrls()`
`urlpatterns = patterns('', *static_urls.discover())`
`urlpatterns += patterns('',`
`(r'^admin/doc/', include('django.contrib.admindocs.urls')),`
`(r'^admin/', include(admin.site.urls)),`
`)`