Login

3110 snippets

Snippet List

Template filter for internal links in TextFields

Allows for in internal link markup to be saved inside markdown-formatted Textfield entries. Using the filter, the link is only looked up at display time, so if your view's URL has changed, that should automatically update with the reverse() lookup. You could tweak the regex pattern to match whatever link markup you prefer. I also use Markdown to process my description fields, so I make the link return a markdown-formatted link instead of HTML, but you could tweak that too. If you use Markdown, you'd want to put this filter first. So to display a description TextField with internal links, in the template would be something like this: `{{ entity.description|internal_links|markdown }}` (See the [Django docs on writing your own custom filters](https://docs.djangoproject.com/en/1.6/howto/custom-template-tags/#writing-custom-template-filters) for more details on writing and registering filters.) Written for [my own website](http://opticalpodcast.com), and a basic version was shared as the [answer to this Stack Overflow question](http://stackoverflow.com/a/26812762/429070).

Read More

Forcing DB on relationship manager with multiple database project

I have a master/slave SQL setup and sometimes I need to access the data immediately after write, but the data has not yet propagated to the slave yet. This forces me to require `.using('default')` every time that relationship is accessed. Ex: self.books.using('default').all().delete() Setting `objects = ForceDefaultDBManager()` on the related object removes this requirement throughout your code. This now does the same as the last example: self.books.all().delete()

Read More

Virtualfish Hook

When you use Virtualfish, the virtualenv wrapper for the fish shell, you can add hooks for whenever the virtualenv is activated. This little snippet sets some important environment variables and adds the manage.py command with autocompletion so that you won't ever need to write "python ./manage.py help" ever again, but only "manage <TAB>". For this to work you need to source or paste this code in your ~/.config/fish/config.fish

  • manage
  • virtualenv
  • fish
  • virtualenvwrapper
  • virtualfish
Read More

Extended Seconds-to-Duration Template Tag

Extended Template Tag initially created by Dan Ward (http://d-w.me): https://djangosnippets.org/snippets/1398/ The default setting has been renamed to normal, output stays the same. If you have used the old tag with ':short' you have to remove or rename it to ':normal' for the same ouput! As an example, given the duration 84658: Short: 23:30:58 Normal (default): 23 hrs 30 mins 58 secs Long: 23 hours, 30 minutes and 58 seconds Best regards, Gregor Volkmann

Read More

log django exceptions to file

Log Django exceptions to file. Contains snippet for both WatchedFileHandler and RotatingFileHandler handlers. Configurations mentioned are separate And only works if the DEBUG = False. Use "python manage.py runserver --no-reload" If you get file in use error.

Read More

Seconds-to-Duration Template Tag

Does exactly what it says on the tin! This template tag, when implemented, converts a duration (in seconds) to a more meaningful format. It has a short and long setting, which is easy to manipulate for your needs. Apologies if something already exists like this, however I felt that writing this would be quicker than trying to find it online. As an example, given the duration 84658: Short (default): 23 hrs 30 mins 58 secs Long: 23 hours, 30 minutes and 58 seconds All the best, [Dan Ward](http://d-w.me).

  • template
  • tag
  • seconds
  • to
  • duration
Read More

render_as_template template tag

This is a template tag that works like `{% include %}`, but instead of loading a template from a file, it uses some text from the current context, and renders that as though it were itself a template. This means, amongst other things, that you can use template tags and filters in database fields. For example, instead of: `{{ flatpage.content }}` you could use: `{% render_as_template flatpage.content %}` Then you can use template tags (such as `{% url showprofile user.id %}`) in flat pages, stored in the database. The template is rendered with the current context. Warning - only allow trusted users to edit content that gets rendered with this tag.

  • template
  • tag
  • context
Read More

PDF generation directly using HTML

This is an extract of an example for use of "pisa" <http://www.htmltopdf.org> in "django". It shows the easiest way possible to create PDF documents just using HTML and CSS. In "index" we see the definition of the output of a form in which HTML code can be typed in and then on the fly a PDF will be created. In "ezpdf_sample" we see the use of templates and contexts. So adding PDF to your existing Django project could be just a matter of some lines of code.

  • pdf
  • html
  • css
Read More

Google Analytics Template Tag

Includes the Javascript for Google Analytics. Will not show Google Analytics code when DEBUG is on or to staff users. Use {% googleanalyticsjs %} in your templates. You must set something like GOOGLE_ANALYTICS_CODE = "UA-1234567-1" in your settings file. Assumes 'user' in your template variables is request.user, which it will be if you use: return render_to_response('template.html',{ }, context_instance=RequestContext(request)) (Assuming django.core.context_processors.auth is in TEMPLATE_CONTEXT_PROCESSORS, which it is by default)

  • google
  • urchin
  • analytics
Read More

Read-only Model Form Base Class

The simplest way of displaying a "details" table about any model, is to show a ModelFrom with all fields readonly or (selects) disabled. Also, the labels are preferably translatable, not just capitalized names of the column tables in your models. So the constructor translates the field labels as well.

  • form
  • readonly
Read More