If you require lots of forms in your project and do not want to be creating an extended template for each one I propose this solution.
Classes in the html correspond to bootstrap, you can work without them if you do not use bootstrap.
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>
This is useful if you have a string that is html encoded (i.e. "<p>Hello world!</p>") and you want to do something more complex than just display it as html, such as using the striptags filter.
The Django admin site has a feature to filter objects in change list by parameters supplied in the query string. Particularly, parameters such as date\__gte and date\__lte can be used.
This example is for filtering objects in change list by a date range (the date field is called expiration_date, but you can, of course, use any other name).
As the server side (Django) already supports such filtering, all you need to do is to edit this for your needs (you can also add some DRY if you want) and put it into templates/admin/app_name/model_name/change_list.html.
Some CSS for better layout:
div.date_filter select#from_month, div.date_filter select#to_month {
margin-left: 0;
}
div.date_filter label {
margin-right: 5px;
}
This is a function based on django's urlize modified to show different media based on their url. It supports images, links, mp3/ogg links, youtube videos, vimeo videos and dailymotion videos.
I added a switch called mini to have two modes to show things in different places. When mini is activated it will only parse the first url provided and discard the rest as well as limiting the height.
From: [incredible times](http://incredibletimes.org)
With inspiration from: [Unethical Blogger](http://unethicalblogger.com/2008/05/03/parsing-html-with-python.html)
This code parses any provided HTML content, and extracts a number of paragraphs specified, with all the content and tags inside them.
Example:
Template variable "content" contains:
<a href="#>some text</a>
<p><strong>Testing</strong>testing testing this is a tester's life</p>
<div>I wont see the world</div>
<p>Another paragraph</p>
So, place this code in any loaded template module (inside a templatetags folder of your app... i.e. myapp/templatetags/myutils.py)
{% load myutils %}
{{ content|paragraphs:"1"}}
Would return:
<p><strong>Testing</strong>testing testing this is a tester's life</p>
Whereas
{% load myutils %}
{{ content|paragraphs:"2"}}
Returns:
<p><strong>Testing</strong>testing testing this is a tester's life</p>
<p>Another paragraph</p>
This is an enhancement of snippet [#172](http://djangosnippets.org/snippets/172/). Here I use [BeautifulSoup](http://www.crummy.com/software/BeautifulSoup/) — far more easier to install through pip in a virtualenv, and possibly a bit more maintained — to format and properly indent the rendered HTML from templates.
I also added a check to only tidy contents in a `DEBUG=True` environment, regarding high impact on performance while doing so in production.
Last, it's compatible with Django 1.2.x.
This will allow you to attach HTML multipart emails (HTML/text) and use inline images.
In my example, I'm attaching an image that's stored as an 'attachment' to an 'event.' The file name of the attachment is called "inline.jpg" and I'm referencing it in my HTML message. I'm also attaching a VCAL file (.ics) file that has some information about an associated event.
The default Django urlize filter does not work with html nicely so here I've used an HTML parser [BeautifulSoup](http://www.crummy.com/software/BeautifulSoup/) to quickly search through each text node and run the django urlize filter on it.
Optimizations could be made to include a regex in the soup.findAll() method's text argument to only search those text nodes which matched a url pattern. You could also modify the method to convert the text to urls, such as using your own custom url filter.
A shortcut for generating img-s with predefined classes and attributes that mimics a html tag, while resolving context variables inside {% %} without crutches like tag/stuff/endtag.
Used as `{% icon test class="spam" eggs="{{ object.pk }}" %}` yields `<img src="http://host.tld/media/icons/test.png" alt="" class="icon16 spam" eggs="42"/>`.
Not customizable here for simplicity.
This Snippet allows a view to controle the printed forms on the templates, in a similar way to the fieldsets used by the django admin.
How to Use:
In the view in question, put:
def some_view(request):
...
fieldsets = (
(u'Title 1',
{'hidden' : ('field_1', 'field_2',),
'fields' : ('field_3',)}),
(u'Title 2',
{'hidden' : ('field_5', 'field_6',),
'fields' : ('field_4',)}),)
)
return render_to_response('some.html', {'fieldsets': fieldsets})
fieldsets = (
(None,
{'hidden' : ('evento', 'colaborador',),
'fields' : ('acompanhantes',)}),
)
Next, in the html just add:
<form enctype="multipart/form-data" id="edit" method="post" ...>
...
{% include "inc/form_snippet.html" %}
...
<input type="submit" value="Submit">
</form>
This Snippet allows for your project's apps names to be displayed as you want in the Admin, including translations.
The lists of apps and languages are created from your settings.py file.
**How to use**
1st part:
- Create a application called 'apps_verbose' in you project with the models.py and admin.py showed here
- Create a folder inside it with named 'templatetags' with the verbose_tags.py file inside it.
- Add this app to installed apps in your settings.py
- If you change this app name, dont forget to correct the imports.
2nd part:
- Create a folder named 'admin' in your templates folder and copy the following files form your /django/contrib/admin/templates/admin/ folder.
- /app_index.html
- /base.html
- /change_form.html
- /change_list.html
- /delete_confirmation.html
- /delete_selected_confirmation.html
- /index.html
- /object_history.html
- Make the necessary changes in each file, like shown here.
3rd part:
- Create translations in the Admin and enjoy.
Reworked version of [this snippet](http://www.djangosnippets.org/snippets/205/) that now accepts an argument so the user can specify which tags to allow, and which attributes should be allowed for each tag. Argument should be in form `tag2:attr1:attr2 tag2:attr1 tag3`, where tags are allowed HTML tags, and attrs are the allowed attributes for that tag.
It also uses code from [this post on stack overflow](http://stackoverflow.com/questions/16861/sanitising-user-input-using-python) to add XSS protection.