Login

Top-rated snippets

Snippet List

Read more link

I couldn't find any code for a blog-style "Read more after the jump," so I made a custom filter. It will look for **<!--more-->** for the jump, like in Wordpress. In **settings.py** set **READ_MORE_TEXT** to what you want the text of the link to be. `READ_MORE_TEXT = 'Read more after the jump.'` When you call the filter in your template, pass it the absolute link of that entry. Of course, you have to have your **get_absolute_url** function defined in your model, but you should have that already, right? :P In this example **entry.body** is the content of the blog entry. `{% load blog_filters %}` `{{ entry.body|read_more:entry.get_absolute_url }}` If anyone has a better way to do this, it is, of course, welcome.

  • template
  • filter
  • blog
  • find
  • jump
  • read
  • more
Read More

activation_required

Simple decorator that checks for authentication and activation of account and redirect to login or activation page if needed Your ulrsconf file must have named urls with parameters that you call that decorator Dont forget to import reverse function from django.core.urlresolvers import reverse

  • decorator
  • auth
Read More

Templatetag startswith + message tuned

Template: `<div id="messageBox_{{ forloop.counter0 }}" style="border:1px solid #ccc;background-color:white" onclick="document.getElementById ('messageBox_{{ forloop.counter0 }}').style.display = 'none';"> {% ifstartswith message "#ok#" %} <font color="green"> {% endifstartswith %} {% ifstartswith message "#error#" %} <font color="red"> {% endifstartswith %} {{ message|cut:"#ok#"|cut:"#error#" }} </font> </div>` In a view you can now do something like that: ` request.user.message_set.create(message="#ok#Hello User, this is a ok message!")`

  • template
  • templatetags
  • message
Read More

ugettext tag

This translates a given message with ugettext. That's it. `{% load where_you_have_it %}` `{% ugettext "German" %}`

  • tag
  • i18n
  • ugettext
Read More

Get child model

With the child_object function defined in the parent model, you can get it's child object.

  • model-inheritance
Read More

HTTP method_required Decorator

Edit: As James pointed out, `django.views.decorators.http` already provides stuff for this. Use that instead. Old description: Should be pretty straightforward; you give it the method you can accept, it returns 405's for other methods. EG, `@method_required("POST")` at the top of your view.

  • http
  • decorator
Read More

OwnerField

This is a little helper for associating an owner to a newly created object, in this case making the assumption that the current user is always the owner. It removes the necessity of adding a custom save hook to your model. get_current_user comes from this middleware trick to cache the current user: http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser

  • model
  • user
  • field
  • owner
  • ownerfield
Read More

Integer list to pattern function

This function can be util for transform pattern lists like these to strings: >>> list_to_pattern([42, 43, 44, 45]) '42-45' >>> list_to_pattern([15, 49, 50, 51, 52]) '15,49-52' >>> list_to_pattern([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]) '0-13' You can use also the pattern to list function at [http://www.djangosnippets.org/snippets/495/](http://www.djangosnippets.org/snippets/495/)

  • list
  • integer
Read More

Table of Contents

Simple template filter that extracts from a text ids, replaces '_' with spaces and produces hyperlinked Table of Contents. More info and usage example please see at [http://www.mysoftparade.com/blog/autogenerated-toc/](http://www.mysoftparade.com/blog/autogenerated-toc/)

  • toc
  • table-of-contents
Read More

A simple rest template filter

**Attention! This snippet must be ignored**, like [zgoda](http://www.djangosnippets.org/users/zgoda/) pointed with reason: already exists this functionality in `markup` contrib. **Explanations:** This template filter allows you to render easily a reStructuredText to **HTML** or another format it supports. **Setup:** Insert the snippet into an_app/templatetags/restutils.py. **Use in template:** `{% load restutils %}` and use it as following: - `{{ entry.content|rest:"html" }}`

  • rest
  • template-filters
Read More

Partial JSON template rendering

this decorator will render template and encode it as JSON string if it find **ajax_request** variable in GET. It will not parse parent (extended) templates, only given template and nested (included). Only blocks will be rendered and encoded to JSON naming block__*BLOCKNAME*. Simple javascript can do ajax request adding *?ajax_request* or *&ajax_request* (if needed) and update given html elements if they id's are same as block__*BLOCKNAME*. you can find a full code (including javascript) at my (http://projects.barbuza.info/trac/browser/django_addons/django_ajax/trunk/ sandbox).

  • ajax
Read More

If in list template tag

Given an item and a list, check if the item is in the list ----- item = 'a' list = [1, 'b', 'a', 4] ----- {% ifinlist item list %} Yup, it's in the list {% else %} Nope, it's not in the list {% endifinlist %}

  • template
  • tag
  • list
  • in
Read More

3110 snippets posted so far.