Login

All snippets

Snippet List

Create PDF files using rml and django templates

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.

  • templates
  • pdf
  • rml
Read More

Google Maps Templatetag

Use: ... <head> ... {% gmap-script %} ... </head> ... <body> ... {% gmap name:mimapa width:300 height:300 latitude:x longitude:y zoom:20 view:hybrid %} Message for a marker at that point {% endgmap %} ... </body>

  • templatetag
  • google-maps
  • maps
Read More

Table

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

  • table
  • grid
Read More

keeptags: strip all HTML tags from output except a specified list of elements

Django has several filters designed to sanitize HTML output, but they're either too broad (striptags, escape) or too narrow (removetags) to use when you want to allow a specified set of HTML tags in your output. Thus keeptags was born. Some of the code is essentially ripped from the Django removetags function. It's not perfect--for example, it doesn't touch attributes inside elements at all--but otherwise it works well.

  • filter
  • html
  • escape
Read More

Use class name in templates

For use in templates: {% if object|classname:"entry" %} ... class="{{ object|classname }}" ... I couldn't find simpler solution for checking weather specific object belongs to specific class name, or just to output object's class name.

  • filter
  • classname
Read More

Google Geocode Lookup

Makes a call to Google's geocoder and returns the latitude and longitude as a string or returns an empty string. Called by the save method to save the lat and long to the db to be used when rendering maps on the frontend. Reduces the number of calls to geocoder by calling only when saving, not on every viewing of the object. Be sure to import *urllib* and the project's *settings*, and to define GOOGLE_API_KEY in settings.py. **Example:** def save(self): location = "%s+%s+%s+%s" % (self.address, self.city, self.state, self.zip_code) self.lat_long = get_lat_long(location) if not self.lat_long: location = "%s+%s+%s" % (self.city, self.state, self.zip_code) self.lat_long = get_lat_long(location) super(Foo, self).save()

  • google-maps
  • geocode
  • google-api
  • latitude
  • longitude
Read More

linebreaksli template filter

This template filter will split a string of text on newlines and return a string of <li></li>s with a newline before every line. This is handy for taking a paragraph of text and making an <ol> or <ul> from its lines. Don't forget to register your filter with the template library first or the filter won't work.

  • filter
  • linebreaks
  • li
Read More

SQL Printing Middleware

Just put it in your python path and add it into MIDDLEWARE_CLASSES. I know that there are a couple of snippets on this site that do something similar to this, but none of them quite suited me. I wanted something that would indent and space the SQL so that I could differentiate it from the other output from the development server. Also, I wanted something that would output total query execution time, which my solution does. I just hope that it's useful for someone else, too! UPDATE: Now this should no longer get upset when running it on windows.

  • sql
  • middleware
  • profile
  • debug
  • help
  • console
  • printing
  • speed
Read More

Simple solution for model schema evolution / database changelog

This is a very simple approach to "schema evolution" (Not sure if you can even call it so) - I use it for my project: [SCT](http://sct.sphene.net) and it seems to work quite nicely. The idea is, that if you add or change your models, you add a 'changelog' attribute to your class which defines the SQL queries required to update your models. Of course this only works for one database type... An example 'syncdb' call if a new changelog entry was detected: kahless@localhost ~/dev/python-new/sphenecommunity/sphenecommunity $ ./manage.py syncdb Executing module body. 2007-06-16 00: SQL Statement: ALTER TABLE "sphboard_post" ADD markup varchar(250) NULL Detected changes - Do you want to execute SQL Statements ? (yes,no): So if the SQL statement won't work with the database the user is currently running, he at least knows exactly what he is expected to change in his models. and he can stop automatic execution by simply entering 'no' here.

  • changelog
  • schema-evolution
Read More

Send information mails to related staff members.

You can use this method to send information mails to the related staff members about section specific site activity. All users which explicitly permitted to 'change' given object will be informed about activity. If you defined get_absolute_url in your model then you can simply use it like this; ` obj=form.save() mail2perm(obj) ` Or you can define your custom urls ; ` from util.mail2perm import mail2perm,domain reply=get_object_or_404(Reply,user=request.user,pk=id) mail2perm(reply,url='http://%s/admin/support/show/?id=%s'%(domain,reply.id)) `

  • mail
  • permission
Read More

Random Quotes

Display a random quote. Just add some quotes to the app (you may want to add the administration interface options) and then load the template tag: `{% load random_quote %}` and then call the tag to display a random quote from the app `{% random_quote %}` feel free to improve it/whatever.. :)

  • templatetag
  • random
  • quotes
Read More

Sending html emails with images using Django templates

I have not extensively test this yet. But working for templates with embedded images. If you want to use Django template system use `msg` and optionally `textmsg` as template context (dict) and define `template` and optionally `texttemplate` variables. Otherwise msg and textmsg variables are used as html and text message sources. If you want to use images in html message, define physical paths and ids in tuples. (image paths are relative to MEDIA_ROOT) example: images=(('email_images/logo.gif','img1'),('email_images/footer.gif','img2')) use them in html like this: `<img src="cid:img1"><br><img src="cid:img2">` stripogram and feedparser modules are used for extract plain text from html message source. If you are going to define text partition explicitly, than you can comment out line 10,11 and 48.

  • template
  • email
  • mail
  • html
Read More

staticview for app

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

  • static
Read More

3110 snippets posted so far.