For use in templates:
{% if object|classname:"entry" %}
... class="{{ object|classname }}" ...
I couldn't find simpler solution for checking weather specific object belongs to specific class name, or just to output object's class name.
Display a random quote. Just add some quotes to the app (you may want to add the administration interface options) and then load the template tag:
`{% load random_quote %}`
and then call the tag to display a random quote from the app
`{% random_quote %}`
feel free to improve it/whatever..
:)
I have not extensively test this yet. But working for templates with embedded images.
If you want to use Django template system use `msg` and optionally `textmsg` as template context (dict) and define `template` and optionally `texttemplate` variables.
Otherwise msg and textmsg variables are used as html and text message sources.
If you want to use images in html message, define physical paths and ids in tuples.
(image paths are relative to MEDIA_ROOT)
example:
images=(('email_images/logo.gif','img1'),('email_images/footer.gif','img2'))
use them in html like this:
`<img src="cid:img1"><br><img src="cid:img2">`
stripogram and feedparser modules are used for extract plain text from html message source.
If you are going to define text partition explicitly, than you can comment out line 10,11 and 48.
when running a console script to access django or your models, you need to setup django environment properly and that is very annoying if you have quite a few scripts. this script can be used as a wrapper just like the manage.py.
put it in your project root and type this:
python run.py myproj.appname.filename.functionname [arguments]
and define your function as
def functionname(argv[]):
...
I wanted to mark rows with an erroneous input with 'class="error"' and move the errorlist below the input tag so I wrote a NormalRowFormatter which behaves like a format string by overwriting \_\_mod\_\_.
Examples:
class MyForm(PrettyBaseForm, forms.Form):
# add your fields here
SomethingForm = form_for_model(Something, form=PrettyBaseForm)
Simple contact form, using a javascript form checker which you can use any you want to you can just modify the form in the way you want to.
The form checker I use I include in the 'base.html' page before the </head> section end. You can use it freely. http://media.xorl.de/form.js . A lot of this code is comprised of other peoples forms that didn't suit the simple purpose of a *really* basic contact form which I wanted, so I rebuilt it up from bits and pieces to make a simple basic form.
This will hide the scrolling output from the development server so you can ssh and run the server in peace, for those of us with only one SSH session active developing on a remote server.
There are a lot of reasons why you should not do this.
But nevertheless it might be useful in certain situations.
If your database does not change and you want to use validators etc you better use inspectdb from django-admin.py
`{% tablify questions name response 8 %}`
Will generate a table structure of a list, ensuring that a
set number of columns is not exceeded.
It takes 4 arguments:
*Context Variable
*Cell Header Attribute
*Cell Data Attribute
*Max Number of Columns
The code will loop through the given variable, creating th and td elements for each item. It will create a new tr when the max number of columns is exceeded.
This middleware remembers the last URLs that the user visited on your Django-site and saves them into the `request.session`.
The fields are `currently_visiting` for the URL that is opened by the user and `last_visited` which is the URL before. Most of the time, you'll need only `last_visited`, as `currently_visiting` is just an implementation detail.
For what is this good for? Imagine, you have to implement something like JavaScripts `history.back()` or `history.forward(-1)` but without JavaScript. Things start to get difficult when using JavaScript is not possible, for whatever reason.
This snippet was created because I needed to redirect the user to the page he's been watching before clicking on the "translate" link. One other alternative would be adding the URL the user was visiting as a GET field to the "translate" link, but I hoped to find a possibility to avoid GET.
This snippet works quite well as a proof of concept, the only known wart is when the user uses tabs or multible windows on the site, things get messed up. This cannot be solved, it's a restriction imposed by the design of HTTP.
You may want to access to "global" errors in your template, typically when you have a custom `clean()` method which checks several fields. `{{ form.errors }}` gives an < ul >, which is often not what you want, and `{{ form.errors|join:", " }}` iterates in the keys of `errors` (which are the names of fields, uninteresting).
The global `errors`'s key is "__all__", and Django doesn't allow us to do `{{ form.errors.__all__ }}`.
This method returns an array, like a classic `{{ form.field.errors }}`, and can be used with a join : `{{ form.get_global_errors|join:", " }}`.
Keeps database table names out of custom SQL code, but still allows for correct parameter passing in the execute function. (psycopg doesn't substitute table or field names, only data, in the execute function)
This isn't really any trick, its just that I didn't find any documentation of this or any references in searching. There are a few changes proposed for css classes which might make this redundant, but for now this works!
If you want to add attributes for any fields, just include them in the widget constructor. They get written out in key value pairs when the input field is being created. e.g. in the example above, this will come out like:
`
'<input type="text" name="start_date" id="id_start_date" class="vDateField required" size="10"/>'
`
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.