Most of the time when you want a dropdown selector based on
a ForeignKey, you'll want to use [snippet #26](http://www.djangosnippets.org/snippets/26/)
Here's an alternative approach, perhaps useful when you want to define choices once and reuse it in different views without overriding Form `__init__`.
In your site’s settings.py module (in your site root), TEMPLATE_DIRS takes absolute paths. Here is a way to dynamically determine the absolute path to the application directory so you only have to specify relative paths within settings.py. Obviously, replace “application_directory” with the name of your application’s directory.
The loremipsum tag generates random content for use in mockups. It takes this content from Cicero's De finibus bonorum et malorum. By default you get a random number of paragraphs up to 6, but you can affect this behaviour by passing in parameters for quantity and units. Units can be "paragraphs" or "words"; the default is paragraphs. Quantity dictates the number of units returned.
If your django.conf.settings.TEMPLATE_DEBUG is True, then no output is returned, so you should be OK in the event you leave the tag in a production template.
Syntax:
{% loremipsum %}
{% loremipsum quantity="3" units="paragraphs" %}
By leaving the behaviour as random, you can see how your layout handles varying amounts of content. Handy, especially for multi-column pages. [Here's a stoopid site](http://lakes.knoxzilla.com/) where I'm playing with random text, pictures, business listings, etc.
I do plan to add a "corpus" parameter to allow you to put your own corpus between lorumipsum and endloremipsum tags and a template parameter to specify the corpus should come from a template, but this has suited me well thus far.
This is a simple filter I use to display a list of links from a blog entry off in the sidebar ([example](http://www2.jeffcroft.com/blog/2007/feb/25/two-new-django-sites-both-source-available/)).
Requires beautifulsoup. Originally by [Nathan Borror](http://playgroundblues.com), tweaked slightly by me.
This is just a very short (and mostly useless on it's own) example of how the built in slugify filter can be used in a Python script to generate slugs. It was pulled from a script I've written to pull in items from Upcoming.org's API.
I post it because "sunturi" posted [Snippet #29](http://www.djangosnippets.org/snippets/29/), which duplicates the functionality of the built-in slugify filter. In the comments of that snippet, santuri (and others) seem to believe that template filters can only be used within templates. This is incorrect, and I think it's important people understand they can be used elsewhere.
sunturi's snippet does remove prepositions from values before slugifying them, so if you need that, his code will work work. But if all you need is slugification, the built-in slugify filter will work fine -- in a Python script, as well as in a template.
This is like [snippet 36](/snippets/36/). And it'll return css also. And it's not a filter.If the code parameter is skip, it'll test the code first, and if there is not a suiable lexer for the code, then use default python lexer to render the code. The code lanauage parameter is comes from pygments lexer alias.
How to use it
----------------
html = to_html(rest_text)
And there is a level parameter in to_html function, default is `2`, it's the sections level. And the html will be `css style + body`
How to write ReST
---------------------
Below is a example.
This is a test.
.. code::
def code(name, arguments, options, content, lineno,
content_offset, block_text, state, state_machine):
opt = {'display':'on'}
opt.update(options)
docnodes.Node(content, ''.join(arguments), **opt)
if opt['display'].lower() == 'on':
return [nodes.literal_block('', '\n'.join(content))]
else:
return []
.. code:: html+django
<h1 id="title">通讯录</h1>
<hr>
<div>
<table border="0" width="500">
<tr align="right">
<td>{% if has_previous %}
<a href="/address?page={{ previous }}">上一页</a>
{% endif %} {% if has_next %}
<a href="/address?page={{ next }}">下一页</a>
{% endif %}</td></tr>
</table>
This function takes a string (most likely from a template), searches it for `<code>[...]</code>`, highlights it with Pygments, and returns the entire thing back, as a string. (Note: the `<code>[...]</code>` must have a class corresponding to the language inside. If it lacks the class, then it's silently ignored.)
Put inside `mysite/templatetags/getattr.py`
Add `mysite` to your `INSTALLED_APPS`
In your template:
{% load getattr %}
{{ myobject|getattr:"theattr,default value" }}
Thanks to pterk for optimizations! \\o/
Though this may not be Django (just basic Python) this kind of thing sure does come in handy! __Plus, it's super easy to edit for any datetime format you want to throw at it.__
UPDATED:
This now supports an argument for the initial header level.
This is a modified version of `django.contrib.markup` that allows you to highlight code via [pygments](http://pygments.pocoo.org/). The code block can be used as:
`Here's a paragraph, and a code example:
.. code:: language
*insert code here*
continue with your normal document.`
Setup:
Insert the snippet into `mysite/templatetags/rest.py`, then add `mysite` to your installed apps in `settings.py`.
In your template, `{% load rest %}` and `{{ mycontent|rest }}`.
A very common field in forms is the `<textarea>`, but `newforms` has no such field. Instead, you must use a dummy field (such as `newforms.CharField`) and use the `newforms.widgets.Textarea()` widget to render a textarea.
I was faced with the fact that I wanted to post 2 paragraph-long summaries on one of my sites, and this is what I did (you could of course cut it down earlier, but I'd say this belongs to what is called "template logic")
Use like so:
{% load myExtraModule %}
{{ blogpost.content|paragraphs:"2" }}
The lines filter works the exact same way, and you might want to improve on these a bit, I don't maintain them as I don't use them anymore.