Login

All snippets

Snippet List

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

Template tag to render collections.Counter as an html table

Render a given instance of collections.Counter into a 2 column html table. Optionally accepts `column_title` keyword argument which sets the table key column header. Usage: {% counter_table event_counter column_title='event type' %} The above will render the a table from the `event_counter` variable with the first (key) column set to "event type". See below for an example template (i.e `counter_table.html`) {% load i18n %} <table> <thead> <tr> <th>{{column_title|capfirst}}</th> <th>{% trans "count"|capfirst %}</th> </tr> </thead> <tbody> {% for key, count in most_common %} <tr> <td>{{key}}</td> <td>{{count}}</td> </tr> {% endfor %} </tbody> <tfoot> <tr> <td>{% trans "total"|capfirst %}</td> <td>{{total_count}}</td> </tr> </tfoot> </table>

  • html
  • table
  • inclusion-tag
  • collections.Counter
  • Counter
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

Dynamically adding forms to a formset. OOP version.

**What It Is** This is a JavaScript-based solution to dynamically add and remove forms in formsets and inlineformsets. It requires jQuery. Originally based on this Snippet: https://djangosnippets.org/snippets/1389/ I have done a lot of work to make it OO, and am using it in production on pages with multiple inlineformsets, and even nested inlineformsets (I call it, "Inlineformset Inception"). My hope is that the code and example are enough to show how it works. **Usage Details** In the example usage, I am using a CSS class, 'light', to make every other form have a light background color. My form placeholder is an element with an ID of 'formset-placeholder' (the default). And the form selector is a class name of 'dynamic-form' (the default). When I have time, I will create a GitHub repository with the code and completed examples.

  • django
  • javascript
  • jquery
  • formset
  • inlineformset
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

Template Tag to protect the E-mail address

Update to https://djangosnippets.org/snippets/1907/ to be a bit more flexible, and code cleaned up a tiny bit. To use, add this snippet as a file in a templatetags folder in an app or in a project. Then include and call the tag with {% obfuscate_email 'email' %} or {% obfuscate_email 'email' 'link_text' %} if 'link_text' is provided, it will be used as the text in the body of the <a> tag. If not, then the email will be used.

  • email
  • spam
  • obfuscate
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

Grouped Model Choice Field

This class lets me have a model choice field that includes optgroups . Django has built-in support for optgroups if you explicitly set all the choices in a ChoiceField, but that doesn't help with ModelChoiceFields. Optgroups let you have nice-looking subscategories in huge dropdowns. They're even more useful if you're using something like selectize.js, because you have a ton of options. If you inherit from GroupedModelChoiceField and override the optgroup_from_instance function, as in SampleChoiceField, you'll get a dropdown with your models with the expected optgroup tags in the html. Be sure to have your queryset first order by whatever you're displaying in optgroup.

  • ModelChoiceField
  • OptGroup
  • Forms
Read More

3109 snippets posted so far.