This takes advantage of a recently added feature to django, being able to give it real functions as the view instead of having it be a string that is has to look up itself.
It takes advantage of how decorators work and how `cache_control` works, normally you'd do something like this:
@cache_control(private=True, public=False)
def view_stuff(request):
# ...
return response
Which is equal to doing `view_stuff = cache_control(private=True, public=False)(view_stuff)` after definition. `cache_control` is a function factory, more or less, which we use to our advantage.
This is a bit of a hack, but as far as I can see currently the only way to specify a validation error that is specific to a field in form.clean().
I am aware of clean_<fieldname>, but those are difficult to use when the validation process for a field involves other fields as well, because the necessary data might at that point not be yet available in form.cleaned_data.
UUIDField is a field which stores an uuid generated by pythons new uuid module.
it's included with the python 2.5 distribution by default, but if you run an older version of python you can happily copy the file from 2.5 to django/utils/uuid.py or your project directory.
Some time you want to add some common fields to a group of models, for example, in a **Generalization/Specialization** relationship. One could have a base model as the generalization class and specialized models with a foreign key to that base model with an unique attribute but I don't like it that way so, I just do this code to add some commons attributes to a lot of models.
If you have many models that all share the same fields, this might be an option. The fields are added directly to each model, e.g. while they will be duplicated on the database level, you only have to define them once in your **python** code.
This code is a cleaner way(I think!!!) to do it and will do the same that [this one](http://www.djangosnippets.org/snippets/317/). I hope this piece of code will be useful for you.
As an alternative to using forms rendered with the
default hard-coded html, this factory function will
take a template name as argument, and return a Form class based on django.newforms.BaseForm, which will
render each form field using the supplied template.
As an example, I'm using this class as follows in one
project (slightly edited with respect to project and
app names):
# Get a form class which renders fields using a given template
CustomForm = proj.utils.makeTemplatedForm(template="app/formfield.html")
# Get base form class for the details model
BaseAppForm = forms.form_for_model(models.AppDetail, form=CustomForm)
using this template:
{% if errors %}
<ul class="errorlist">
{% for error in errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
{% if field.required %}<span class="required">*</span>{% endif %}{{ text }}
{{ help_text }}
This is a django filter which will create an html mailto link when you use the syntax:
{{ "[email protected]"|encode_mailto:"Name" }}
Results in:
<a href="mailto:[email protected]">Name</a>
Except everything is encoded as html digits.
The encoding is a string of html hex and decimal entities generated randomly.
If you simply want a string encoding use:
{{ "name"|encode_string }}
This was inspired by John Gruber's [Markdown](http://daringfireball.net/projects/markdown/syntax#autolink) project
This will return a template-friendly list of dictionaries when using custom SQL. The dictionary can be accessed just as a normal model/queryset.
Example of use (in view):
qkeys = ['artist','album']
query = """
SELECT "artist","album" FROM "music"
"""
new_list = csql(request,query,qkeys)
(in template)
{% for row in new_list %}
{{ row.artist }} {{ row.album }}
{% endfor %}
This is to be used as a template filter. See [django documentation](http://www.djangoproject.com/documentation/templates_python/#writing-custom-template-filters) for adding custom filters.
Example of use:
name = "bart simpson"
`{{ name|cap }}` will convert 'name' to "Bart Simpson"
It works on space-separated names, as well as the following:
* converts "mcfly" to "McFly";
* converts "o'neill" to "O'Neill";
* converts "barker-bradley" to "Barker-Bradley"
This is some very simple middleware that keeps track of the last 3 succesful requests for each visitor. This can be useful if you want to redirect the visitor to a previous path without relying on a hidden field in a form, or if you simply want to check if a visitor has recently visited a certain path.
Note that this relies on the session framework and visitors actually accepting cookies.
This can be easily modified to hold more requests if you have a need for it.
Sometimes it's nice to know if your visitors came from a search engine such as Google or Yahoo. I use this as a component in determining the popularity of blog articles for search keywords, so I know which articles to automatically feature or suggest. Maybe you'd like to use it to highlight the visitor's search terms on the page.
This isn't actually middleware in the sense that it defines any of the middleware interfaces. It's intended to be the base for a middleware method that you write, depending on what you want to happen when you detect that the visitor came from a search engine. In the example `process_view` method, I detect when the request is going to use the `object_detail` view and log the search query for that object in the database.
I have users in many timezones and I let them set their preferred display timezone in their user profile as a string (validated aginst the pytz valid timezones).
This is a filter that takes a datetime as input and as an argument the user to figure out the timezone from.
It requires that there is a user profile model with a 'timezone' field. If a specific user does not have a user profile we fall back on the settings.TIME_ZONE. If the datetime is naieve then we again fallback on the settings.TIME_ZONE.
It requires the 'pytz' module: [http://pytz.sourceforge.net/](http://pytz.sourceforge.net/)
Use: `{{ post.created|user_tz:user|date:"D, M j, Y H:i" }}`
The example is assuming a context in which the 'user' variable refers to a User object.
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.