This middleware adds a "is_mobile" (boolean) to the request object if the user's browser is a mobile browser (iPhone, Nokia, etc).
**Example of use inside a view:** `request.is_mobile`
**Example of use inside a template:** *You must activate the template processor "django.core.context_processors.request" in your settings. (see TEMPLATE_CONTEXT_PROCESSORS at djangoproject.com)* `{{ request.is_mobile }}`
This snippet adds simple partial support to your templates. You can pass data to the partial, and use it as you would in a regular template. It is different from Django's {% include %}, because it allows you to pass a custom variable (context), instead of reusing the same context for the included template. This decouples the templates from each other and allows for their greater reuse.
The attached code needs to go into templatetags folder underneath your project. The usage is pretty simple - {% load ... %} the tag library, and use {% partial_template template-name data %} in your template. This will result in template passed as template-name to be loaded from your regular template folders.
The variables are passed in a with compatiable syntax, eg.
VAR as NAME and VAR as NAME
No limitations on the number of variables passed.
Django loads tests found in models.py and tests.py (if present) or actually a module or package named 'tests' under the app. Since tests can be a package, one can create a 'tests' directory, split the test cases across multiple files under 'tests' and import them from tests/__init__.py with:
# tests/__init__.py
from test_mod1 import *
from test_mod2 import *
...
from test_modN import *
For a small number of files that's not too bad but it gets old as more files are added, plus it is error prone (e.g. test cases shadowing others with the same name). The snippet above simplifies the test splitting without importing everything into the same namespace.
Typical usage:
# tests/__init__.py
from ... import get_suite
suite = lambda: get_suite(__name__)
A general AntiSpamForm using some tricks to prevent spam based on current [django.contrib.comments.forms](http://code.djangoproject.com/browser/django/trunk/django/contrib/comments/forms.py). It uses a timestamp, a security hash and a honeypot field. See [AntiSpamModelForm](http://www.djangosnippets.org/snippets/1856/) too.
A simple template tag that returns the favicon URL for a given arbitrary URL.
Put the code into a python module in the templatetags package of a Django app (e.g. myapp/templatetags/mytags.py), and use it like this:
{% load mytags %}
...
<img src="{% favicon posting.url %}/>
<a href="{{ posting.url }}">{{posting.title}}</a>
...
Place the above code in your view, and then you can use it to specify what template to use. Set elements of the context as a dictionary and that gets passed to the template as well. For example:
In view.py:
####################################
@with_template('friends/index.html')
def friends(request, context, username):
context['user'] = User.objects.get(username = username)
in friends/index.html:
{% extends "base.html" %}
{% block content %}
<h1>{{ user.username }}'s Friends</h1>
This is a Django Template Tag to protect the E-mail address you publish on your website against bots or spiders that index or harvest E-mail. It uses a substitution cipher with a different key for every page load.
It basically produce a equivalent Javascript code for an email address. This code when executed by browser make it mailto link.
**Usage**
`{% encrypt_email user.email %}`
**More Info **
[Template Tag to protect the E-mail address](http://www.nitinh.com/2010/02/django-template-tag-to-protect-the-e-mail-address/)
There's no direct way to save the contents of a URL to a Django File field: you're required to use a File instance but those can only safely wrap normal files, not the file-like object returned by urllib2.urlopen. Several examples online use urllib.urlretrieve() which creates a temporary file but performs no error handling without writing a ton of hackish code.
This demonstrates how to create a NamedTemporaryFile, fill it with the URL contents and save it, all using APIs which raise exceptions on errors.
This Snippet allows for your project's apps names to be displayed as you want in the Admin, including translations.
The lists of apps and languages are created from your settings.py file.
**How to use**
1st part:
- Create a application called 'apps_verbose' in you project with the models.py and admin.py showed here
- Create a folder inside it with named 'templatetags' with the verbose_tags.py file inside it.
- Add this app to installed apps in your settings.py
- If you change this app name, dont forget to correct the imports.
2nd part:
- Create a folder named 'admin' in your templates folder and copy the following files form your /django/contrib/admin/templates/admin/ folder.
- /app_index.html
- /base.html
- /change_form.html
- /change_list.html
- /delete_confirmation.html
- /delete_selected_confirmation.html
- /index.html
- /object_history.html
- Make the necessary changes in each file, like shown here.
3rd part:
- Create translations in the Admin and enjoy.
When doing a list_filter on some object in the Django interface, every single item will be displayed. This is not always smart when very long list of choices can be displayed, even though the majority of these choices might not even exist in the database at all.
Using this script will only display a filtering possibility for those items actually present in the database.
This script is not generic, and you will need to change the lines noted "Change this", since I cannot know what exactly you need to filter.
There is possibly a way to make this completely generic, and I hope this will inspire someone to make it!
A very plugable way to get Stanislaus jquery dynamic formset working in the admin with adding just one template.
Add the following to templates/admin/APP/MODEL/change_form.html and also update the MODEL in the prefix setting.
Thanks Stanislaus
[http://elo80ka.wordpress.com/2009/10/10/jquery-plugin-django-dynamic-formset/](http://elo80ka.wordpress.com/2009/10/10/jquery-plugin-django-dynamic-formset/)
[http://go2.wordpress.com/?id=725X1342&site=elo80ka.wordpress.com&url=http%3A%2F%2Fcode.google.com%2Fp%2Fdjango-dynamic-formset%2F](http://go2.wordpress.com/?id=725X1342&site=elo80ka.wordpress.com&url=http%3A%2F%2Fcode.google.com%2Fp%2Fdjango-dynamic-formset%2F)
[http://www.djangosnippets.org/snippets/1389/](http://www.djangosnippets.org/snippets/1389/)
A general AntiSpamModelForm using some tricks to prevent spam based on current [django.contrib.comments.forms](http://code.djangoproject.com/browser/django/trunk/django/contrib/comments/forms.py). It uses a timestamp, a security hash and a honeypot field. See [AntiSpamForm](http://www.djangosnippets.org/snippets/1925/) too.
it is an rewrite of [http://www.djangosnippets.org/snippets/74/]
in settings.py
AUTHENTICATION_BACKENDS = (
'yourproject.email-auth.EmailBackend',
)
Author: chris
**NOTE: I now have a better implementation of this (nicer api, less signal wrangling) available [on PyPI here](https://pypi.python.org/pypi/django-exclusivebooleanfield)**
Sometimes you want to be able to make one (and only one) row in your model 'featured' or 'the default one'
If you have some kind of parent model you could have a ForeignKey on the parent to hold that info, but that won't always be the case - eg you may have no parent, or multiple parent models.
With the exclusive_boolean_fields() helper you can do it with or without a parent model and it possibly makes the admin interface a bit simpler.
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.