This was a much better way of incrementing variables that only changed a small amount in name. deltab in #python also said I could have used tuples, but I'm not familiar with them yet, so I added this to a list of things to look into.
month_ids is a list of months like this...
[('Apr07', 'April 2007'), ('Mar07', 'March 2007'), ('Feb07', 'February 2007')]
which can be used in a select box like this..
month = forms.ChoiceField(choices=(months))
A patch (against django svn trunk [4649](http://code.djangoproject.com/browser/django/trunk/?rev=4649)) that allows users to log in with Basic HTTP Authentication i.s.o. login forms using some simple middleware (entire patch is ~50 lines). I was unaware of http://code.djangoproject.com/wiki/GenericAuthorization so I'm not sure about its usefulness in the long run.
You can enable it by including 'django.contrib.auth.middleware.BasicAuthenticationMiddleware' in your MIDDLEWARE_CLASSES and then adding the following lines in your settings.py:
BASIC_WWW_AUTHENTICATION = True
WWW_AUTHENTICATION_REALM = "djangolures.com"
Updated: See also http://code.djangoproject.com/ticket/3609 (patch now availble here as well).
If you use javascript code with Django-template filter or other related things, it will be not sufficient to qoute string in javascript. This filter escape the string and quote it.
added commands:
altersql - shows sql code with alter queries
alterdb - apply alter queries. parameters:
--showsql - show queries
--app=APPLICATION - alter only selected application
[you need clone this repo](https://bitbucket.org/certator/django_snippets)
Temporary and permanent view redirect decorators. Simplifies views that always redirect to a specific or configured url. You can use the reverse() function (or any other runtime-required lookup) via `lambda` to define the redirect url.
A fast way to implement an iPhone template switcher, especially if you have a lot of existing views using the render_to_response() shortcut. This checks for the iPhone browser and then modifies the chosen template by adding -mobile to the html's file name.
Check out [this more complete list of user agents](http://minidetector.googlecode.com/svn/trunk/minidetector/tests/mobile_useragents.txt) if you need to detect specific mobile devices.
I work a little with [web.py framework](http://webpy.org/) and I like a lot the view definition.
For each view you define a class and in that class you can define two method, GET and POST. If the http request is a GET request the GET method will be called and if http request is a POST request the POST method is called.
Then you can define common stuff in another method that could be called inside each method, and you have a class for each view.
unique_together doesn't work with ManyToMany, yet. See: http://code.djangoproject.com/ticket/702
Here a simple test in save() that would raise a IntegrityError.
I wanted to be able to limit which types of requests a view will accept. For instance, if a view only wants to deal with GET requests.
@methods(GET)
def index(request):
# do stuff
Now, calling this view with a non-GET request will cause a 403.
You can easily change this to a 404, by using a different return function: which you may wish to do with openly available sites, as a 403 indicates there is a resource present.
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.