Login

Top-rated snippets

Snippet List

Make fixtures and follow relationships.

Inspired and based on https://djangosnippets.org/snippets/918/ Improvements: - Supports natural keys - Uses Django's Collector so hopefully follows reverse relationships - Fixes problem when you don't specify a slice - PEP8 etc.

  • fixtures
  • command
Read More

Inject request into functions

Long story short: * Django lets you call functions in templates, but you can't pass any parameters. * Sometimes you need to use the request object to perform certain tasks, such as determining whether the current user has permission to do something. * The recommended approach is to call functions that require parameters in the view, and then pass the results as variables in the context. This sometimes feels a bit overkill. * Creating a templatetag to call any function with any parameter will definitely break the intention for not letting functions to be called with parameters in templates. * So, what if we could tell django to inject the request into certain functions? That's what this decorator is for. For instance, suppose you have a model: class SomeModel(models.Model): ... def current_user_can_do_something(self, request): ...some logic here using request... You could use the decorator like this: class SomeModel(models.Model): ... @inject_request def current_user_can_do_something(self, request=None): ...some logic here using request... And in the template go straight to: {{ somemodel_instance.current_user_can_do_something }} So that the decorator would perform some operations to find the request in the frame tree and inject it if found. The assertions are intented to make sure things will work even if the request cannot be found, so that the coder may program defensively.

  • templates
  • request
  • decorators
Read More

tweetparser filter

Django Template Filter that parse a tweet in plain text and turn it with working Urls Ceck it on [GitHub](https://github.com/VincentLoy/tweetparser-django-template-filter) # tweetParser Django Template Filter this is a port of [tweetParser.js](https://github.com/VincentLoy/tweetParser.js) to work as a Django template filter ## How does it work ? Once installed, just : ``` <p>{{ your_tweet|tweetparser }}</p> ``` ## Installation Take a look at the [Django Documentation](https://docs.djangoproject.com/en/1.8/howto/custom-template-tags/) #### You can change the classes given to each anchor tags ``` USER_CLASS = 'tweet_user' URL_CLASS = 'tweet_url' HASHTAG_CLASS = 'hashtag' ```

  • filter
  • templatetag
  • templatetags
  • twitter
  • parse
  • parser
  • tweet
Read More

Combined CreateView and UpdateView

By combining the CreateView and UpdateView, you can significantly reduce repetition when processing complex forms (for example, with multiple inline formsets), by only writing the get_context_data and form_valid functions once. This class can be used just like a normal CreateView or UpdateView. Note that if you're trying to use it as an UpdateView but it cannot find the requested object, it will behave as a CreateView, rather than showing a 404 page.

  • generic-view
  • class-based-generic-view
  • UpdateView
  • CreateView
Read More

Load template from specific app

It's an update of snippet [https://djangosnippets.org/snippets/1376/](https://djangosnippets.org/snippets/1376/) to work with Django 1.8. With this piece of code, you can override admin templates without copy or symlink files. Just write your template and extend the target.

  • template
  • loader
  • app
Read More

Cancel URL Mixin

**CancelMixin** A simple mixin to use with ```generic.CreateView``` and ```generic.UpdateView``` view form templates to effortlessly implement a "Cancel" button. This smart mixin will add a URL to your context, ```{{ cancel_url }}```, that can be used as a cancel link in your form template. If no referrer URL is provided, the cancel button will link to ```default_cancel_url```, which can be overridden by view. ** **

  • template
  • django
  • mixin
  • update
  • create
  • cbv
Read More

Quick line profiler decorator

Requires [line_profiler](https://pypi.python.org/pypi/line_profiler) pip install line_profiler Will print profile info into console @line_profiler def my_view(request): context = some_quick_func() return some_heavy_func(context) It does not work good when nested, so don't wrap `some_heavy_func`. If you want to profile also some nested call - use `extra_view` parameter: @line_profiler(extra_view=[some_heavy_func]) def my_view(request): context = some_quick_func() return some_heavy_func(context) It will profile `my_view` and `some_heavy_func`

  • profile
  • decorator
  • line_profiler
Read More

Iterable SelectDateWidget

I wanted to be able to restyle the inputs, which required having access to each of the select widgets. When used in a form, you can simply iterate over the field to access each element. Example: {% for form_field in form.date %} <div class="select-wrap"> {{ form_field }} </div> {% endfor %}

  • widget
  • selectdatewidget
Read More

Post-post-syncdb and post-post-migrate

The `post_syncdb` (Django pre-1.7) and `post_migrate` (>=1.7 and South) signals are fired for every single app. What I really wanted was one signal fired after the migration or syncdb completed. There's no official one, and all the snippets were doing horribly hacky things (and wouldn't work for South, anyway). This one will work for Django syncdb, and South migrate.

  • post-migrate
  • post-syncdb
Read More

Enhance django postgresql orm for trigram search

This is a quite simple snippet to integrate postgresql trgm search in django 1.6.10 This snippet is easy to adapt to other special operators by changing the trgm_search function. This example uses the operator `%%` but you could use `ts_vector(fieldname) @@ to_tsquery(%s)`

  • django
  • orm
  • postgresql
  • trgm
Read More

Custom Auth Backend to use E-Mail in Django

Instead of the default Django User the Auth Model is 'customer' from the usercp App. User's can use username as a display to the public, without disclosing their login information. This way they can use a forum with their username, which is seen by everyone. The login credentials, however are more privat, since it is the e-mail address. Allowing the user to not disclose any information.

  • django
  • authentication
  • email
Read More

3110 snippets posted so far.