I was using flup to run django in fcgi mode and encountered the dreaded "Unhandled Exception" page quite frequently. So apart from all the precautions about handling this except, I wrote the above code snippet, which checks the import across your ENTIRE project. Ofcourse this can be used on any python project, but I have written it for my favorite framework django. It is now written as a Django command extension, an can be run as:
**python manage.py imports_checker**
This is a generic command, it does not check the settings.INSTALLED_APPS setting for cleaning. But can be improved to do the same.
Public Clone Url: [git://gist.github.com/242451.git](git://gist.github.com/242451.git)
Update: Now it supports checking imports, just only at the app level also
usage: python manage.py imports_checker <appname>
Allows you to hide the filters in the pages of lists of admin.
Very useful when filters hide one or more columns.
Add this code after the block "extrastyle"
this solves a common problem where you want to specify html tag attributes for form fields in the template itself and not have to do it by writing a custom form class.
eg. the size of the field, css classes, tabindex etc.
usage:
{% render_field form.comments "cols=40,rows=5,class=text,tabindex=2" %}
where form.comments is a form field with a text area widget
it will show data (if the form is bound or if there is initial data) and will display errors if the field has errors
This tag appends the current git revision as a GET parameter to a media files so the web server can set an expires header far in the future. Your project must be structured such that MEDIA_ROOT/../.git exists.
Usage:
`<link rel="stylesheet" type="text/css" href="{% media myapp/css/base.css %}">`
This is a replacement for Django's built-in ImageField. It uses the Google AppEngine image APIs in order to validate.
Notes:
1. Validation of the field counts against your App Engine transformations quota.
2. This code assumes you're only using the in-memory file upload handler. None of the other stock handlers work well on App Engine; you should probably disable them.
A generic base class for extending ModelAdmin views. This can be used likewise:
def myview(self, request, object_id):
obj = self._getobj(request, object_id)
< do something >
def get_urls(self):
urls = super(MyAdmin, self).get_urls()
my_urls = patterns('',
url(r'^(.+)/myview/$',
self._wrap(self.myview),
name=self._view_name('myview')),
)
return my_urls + urls
you have a multilingual site and need to change languages in admin. Previously this was easy in the site itself and more difficult in admin. Now it is dead easy. Set up your languages in settings.py. Make a directory called 'admin' in your templates directory, copy ~/django/contrib/admin/templates/base.html to that directory. Add the following code (below breadcrumbs is a good place)
this will give you a switcher for all installed languages. You would need to refresh the browser on changing the language.
This is an improvement on [snippet 1079](http://www.djangosnippets.org/snippets/1079/). Please read its description and [this blog post](http://zerokspot.com/weblog/2008/08/13/genericforeignkeys-with-less-queries/) for any information.
This is a manager for handling generic foreign key. Generic foreign objects of the same type are fetched together in order to reduce the number of SQL queries.
To use, just assign an instance of GFKManager as the objects attribute of a model that has generic foreign keys. Then:
`MyModelWithGFKs.objects.filter(...).fetch_generic_relations()`
The generic related items will be bulk-fetched to minimize the number of queries.
**Improvement:** Problem I had with previous version from snippet 1079 : if two or more items shares the same generic foreign object, then only the first one is cached. Next ones generates new unwanted SQL queries. I solved this problem by putting all the needed foreign objects in a temporary data_map dictionary. Then, the objects are distributed to every items, so that if two items shares the same foreign object, it will only be fetched once.
there have been many posts on running django on tornado with static media served by nginx. But for dumb people like me, the whole thing needs to be spelt out. So here is how I succeeded in serving django from a virtual host using nginx and tornado. The key thing to note is that 'root' refers to the **parent** directory of the root and not the full path. Also remember to put in ':' as a line end. Procedure - start the tornado server with the python script on localhost:8888, start nginx. Relax and enjoy your django at the speed of light. Nginx can be got by apt-get or yum, but you need the latest git clone of Tornado - the default tarball does not support django. btw, this install is for FC11 on my laptop - I have done it in production on lenny.
Improved version of my snippet #1346. Now works correctly with multiple usernames and hash tags.
Both twitter usernames and hashtags are converted into links to twitter profiles and twitter search.
Updated, forgot about underscores in usernames.
If you want anonymous visitors to your site, or parts of your site to be authenticated as real users so that you can treat them as such in your views and models, use this snippet. Add the above AuthenticationBackendAnonymous middleware into AUTHENTICATION_BACKENDS in your settings.py and use the snippet anonymous_or_real(request) in your views, which returns a user. Comment out the bit where it creates a profile if you are not using profiles.
This adds an 'fbshell' management command which starts up a Python shell with an authenticated [pyfacebook](http://code.google.com/p/pyfacebook/) instance ready to make requests.
This is very useful for testing out facebook requests or performing administration tasks without hooking a debugger into your application.
This snippet should be saved to
/yourproject/management/commands/fbshell.py
See [custom management commands](http://docs.djangoproject.com/en/dev/howto/custom-management-commands/) for a description of how this works.
If you are already using pyfacebook in your app then you'll already have the right settings, so just run :
$ python manage.py fbshell
A browser window will pop up, prompting you for authentication (unless you're already logged in to facebook). Press enter in the shell when you're finished this, and you'll be dropped into a shell with the session key, uuid, and name printed.
Now you can use the facebook instance:
>>> facebook.friends.get()
>>> [...]
If you haven't used pyfacebook in your app, you'll need at least the following settings in your settings.py
FACEBOOK_API_KEY = 'your_api_key'
FACEBOOK_SECRET_KEY = 'your_secret_key'
Truncates a string after a certain number of chars.
Question:
> *Why don't you use the built-in filter slice?*
I need the "three points" (...) only when it really truncates.
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.