Use this script to import the Maxmind GeoIP lite CSV datasets into your database. This takes at least 200MB of RAM; the resulting database will be ~400MB. Stick in the same directory as the [models](http://www.djangosnippets.org/snippets/327/). Make sure to set `DEBUG=False` to prevent running out of memory during import.
This provides GeoDjango models for the maxmind GeoIP Lite data products. Use the corresponding [CSV import script](http://www.djangosnippets.org/snippets/328/) for data import. Requires: [GeoDjango](http://code.djangoproject.com/wiki/GeoDjango) and the [BigIntegerField patch](http://code.djangoproject.com/attachment/ticket/399/django-bigint-20070712.patch) by Peter Nixon.
Originally posted on [skam.webfactional.com](http://skam.webfactional.com/blog/2007/07/16/babel-integration-django/)
This is a very simple middleware that uses babel (http://babel.edgewall.org) for accessing locale data in request objects through request.LOCALE attribute.
It also provides a function to get locale data outside views.
settings.py:
MIDDLEWARE_CLASSES = (
... cut ...
'django.middleware.locale.LocaleMiddleware',
'middleware.locale.BabelMiddleware',
... cut ...
)
[ToofPy](http://pyds.muensterland.org/wiki/toolserver.html) is a python server that supports WSGI so you can integrate Django with it via the WSGI handler.
There is already some default Django integration in the source, but it looked really hacked up with many `if hasdjango:` lines all over the place and hardcoded URLs.
A really simple solution is to create a Tool for wrapping around Django. That is what the above file does. So I removed most of the hardcoded django lines and created the tool above.
ToofPy dynamically loads tools based on paths on the filesystem, so you'll need to put the file above in the WSGI folder path.
Or, if you want to pre-integrate, import the file in the _initops function of WSGIMainTool just after the code to scan the directory.
It allows you to create pdf much like normal html code taking advantage of django's template engine. It requires the trml2pdf module from [OpenReport](http://sourceforge.net/projects/openreport) and can be used like `render_to_response()`. *prompt* specifies if a "save as" dialog should be shown to the user while *filename* is the name that the browser will suggest in the save dialog.
This is rough code that will allow you to create a table using a sequence. It is based on the for loop tag in django template. It works basically the same except that certain variables are set based on what cell is being rendered. The tag itself does not output any html at all. This allows for simple code and very flexible creation of nice tables/grids. Enjoy
This module is comes from the original staticview.py from django. But when you using it, it can looks for static files in your app's subdirectory. So that you can put static files which concerned with your app in a subdirectory of the app. I should say it method only suit for developing, so if you want to deploy your application to apache, you should copy static folder to the real media folder. And if you keep the same structure of the directory, then it'll be very easy. And you can write a little script to automatically do that. But for now, I didn't have written one yet :P
How to use it in urls.py
--------------------------
Here's an example:
(r'^site_media/(.*)$', 'utils.staticview.serve', {'document_root': settings.SITE_MEDIA, 'app_media_folder':'media'}),
It seems just like the original one in django. But there is a new parameter "app_media_folder", it's used for subdirectory name of app. So your django project folder structure maybe seem like this:
/yourproject
/apps
/appone
/media
/css
/img
/js
/media
/css
/img
/js
This snippet is a template filter based on ["truncate letters"](http://www.djangosnippets.org/snippets/126/) only it will either truncate or pad a string to ensure the output is always a set width.
Django ImageBundle tag, an implementation of image bundle by google: http://code.google.com/p/google-web-toolkit/wiki/ImageBundleDesign. Here is how to use it: http://www.djangosnippets.org/snippets/278/
A simple template filter for taking a list and humanizing it, converting:
`["foo", "bar"]` to `"foo and bar"`
`["foo", "bar", "baz"]` to `"foo, bar and baz"`
`["foo", "bar", "baz", "42"]` to `"foo, bar, baz and 42"`
Q() value in Django is identity for & operation: Q() & foo & bar & ... == foo & bar & ...
QEmpty() is complimentary identity for | operation: QEmpty() | foo | bar | ... == foo | bar | ...
QEmpty() itself returns empty queryset.
Handy for complex query generation.
Ever want to call stored procedures from Django easily? How about PostgreSQL functions? That's that this manager attempts to help you with. To use, just stick this in some module and in a model do:
class Article(models.Model):
objects = ProcedureManager()
Now you can call procedures (MySQL or PostgreSQL only) to filter for models like:
Article.objects.filter_by_procedure('ProcName', request.user)
This will attempt to construct a queryset of objects. To just get random values, do:
Article.objects.values_from_procedure('ProcName', request.user)
Which will give you a list of dictionaries.
A merged version of the many jQuery autocomplete widgets.
See [1](http://php.scripts.psu.edu/rja171/widgets/autocomplete.php) and [2](http://www.dyve.net/jquery/?autocomplete) for more information.
A very basic app centered around a template tag that takes an e-mail address and optional label and returns a link to an e-mail form. This way, you can easily throw e-mail addresses into your template that will automatically link to an e-mail form instead of displaying the e-mail address itself.
The model for this app is extremely simple, with only one field to store the e-mail address. When an e-mail is passed to the email_link template tag, the e-mail address is added to the database if it is not already there. This is stored in the database in order to keep the full e-mail address hidden from the viewer.
To use, simply create an app called 'email_links' (or you can change the name if you also change line 4 of email_extras.py to reflect the change). Then use the models.py and views.py provided. Add a templatetags directory under your app, and add email_extras.py and a blank __init__.py
In your urls.py, add the following to your urlpatterns (subsitute project_name for your own):
`(r'^email/(?P<email_id>[0-9]+)/$', 'project_name.email_links.views.email_form'),`
Create two templates: email_links/email_form.html and email_links/thanks.html (the examples here are extremely basic - make them however you want).
Finally, in a template where you want to provide a link to e-mail someone, first load email_extras:
`{% load email_extras %}`
Then do one of the following:
`{% email_link "[email protected]" %}`
`{% email_link "[email protected]" "E-mail someone" %}`
Both will create a link to the e-mail form. The first will use mask_email (from the snippet by jkocherhans) to create the link. The second will display the second argument "E-mail someone" in the link.
Also note that I've used the Sites add-on to generate the links, but you can easily change this to suit your needs.
I hope all of this makes sense. I'm a bit tired, but wanted to get this up before bed.
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.