**Attention: this snippet depends on jQuery library to works**
The Admin templates hierarchy does not allow you change the submit line (that buttons bar in the footer of the change view of a model class).
This code shows how to resolve this, using a simple javascript trick. You can read the code and implement using your favorite javascript library (instead of jQuery) or pure JavaScript.
This example regards to User Profile, a common used model class that adds custom fields to a user in the authentication system.
To use this, you must put this template code in a file with the following name:
templates/admin/auth/user/change_form.html
Hello,
This is a port of a php class used to generate XML taconite command documents, useful for (very) easy and powerful ajaxy stuff, if you don't know what that is just check it there in french : http://www.desfrenes.com/playground/taconite/ or there in english : http://www.malsup.com/jquery/taconite/.
Basically what it does is generate an XML document that is later processed by a javascript plugin which executes a serie of DOM modifications.
About the code, I'm a Django beginner as well as a Python beginner so kind advices are welcome.
Cheers.
This module has two template filters allowing you to dump any template variable. Special handling for object instances. Pretty output.
Usage:
{% load dumper %}
...
<pre>{{ var|rawdump }}</pre>
or
{% load dumper %}
...
{{ var2|dump }}
How to install:
As usual, put into `<your-proj>/<any-app>/templatetags/dumper.py`.
This server-side middleware implements some of the functionality in the Yahoo
User Interface Loader component. YUI JavaScript and CSS modules requirements
can be declared anywhere in the base, inherited or included templates, and the
resulting, optimized `<script>` and `<link rel="stylesheet">` tags are inserted at
the specified position of the resulting page.
Requirements may be specified in multiple locations. This is useful when zero
or more components are included in the HTML head section, and inherited and/or
included templates require possibly overlapping sets of YUI components in the
body across inherited and included templates. All tags are collected in the
head section, and duplicate tags are automatically eliminated.
The middleware understands component dependencies and ensures that resources
are loaded in the right order. It knows about built-in rollup files that ship
with YUI. By automatically using rolled-up files, the number of HTTP requests
is reduced.
The default syntax looks like HTML comments. Markup for the insertion point is
replaced with `<script>` and `<link>` tags:
<!-- YUI_init -->
Component requirements are indicated, possibly in multiple locations, with the
`YUI_include` markup. It is removed from the resulting page by the
middleware. Example:
<!-- YUI_include fonts grids event dragdrop -->
Non-minified and compressed versions are requested, respectively, by:
<!-- YUI_version raw -->
<!-- YUI_version debug -->
Example:
<html><head>
<!-- YUI_init -->
<!-- YUI_include dom event -->
</head><body>
<!-- YUI_include element selector reset fonts base -->
</body></html>
Renders:
<html><head>
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.5.1/build/reset-fonts/reset-fonts.css" />
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.5.1/build/base/base-min.css" />
<script type="text/javascript" src="http://yui.yahooapis.com/2.5.1/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.5.1/build/element/element-beta-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.5.1/build/selector/selector-beta-min.js"></script>
</head><body>
</body></html>
The markup format can be customized with global Django settings. Example:
YUI_INCLUDE_PREFIX_RE = r'{!'
YUI_INCLUDE_SUFFIX_RE = r'!}'
would change markup to e.g. `{! init !}` and `{! include dom event !}`.
The base URL is customized with the `YUI_INCLUDE_BASE` setting, e.g.:
YUI_INCLUDE_BASE = 'http://localhost:8000/yui/build/'
To remove the XHTML trailing slash from the `<link>` tag, use:
YUI_INCLUDE_CSS_TAG = '<link rel="stylesheet" type="text/css" href="%s">'
See also the [home page for this module](http://trac.ambitone.com/ambidjangolib/wiki/YUI_include).
Standarization of image fields (for being used when saving models). Automatically creates thumbnail. Change image name to /path/to/images/<my_field>-<id>.<ext>. Resize image and thumbnail to specified size (optionally can crop image to force size).
This simple middleware replaces all 'a href' links to the current page to the 'span' elements. This very usefule from the usability point of view.
For example, user open in bowser page http://svetlyak.ru/blog/, and this middleware will replace all 'a' elements on this page, which refer to the '/blog/'. Because of this, link 'Blog' in the main menu, become a simple 'span'.
Next, when user goes to the next page, a post with full comments list ('/blog/123/'), for example, the item 'Blog' in the main menu become a link again!
To use this middleware, just add it to the list of middleware classes:
MIDDLEWARE_CLASSES = ('utils.middleware.RemoveSelfLinks',)
A simple adaptation of RegistrationFormUniqueEmail from django-registration http://code.google.com/p/django-registration to allow users to register without using a username. This works great with the code from this comment: http://www.djangosnippets.org/snippets/74/#c195 to allow users to completely eliminate the need for a username.
This decorator handle a extra "action" parameter from an url and call this desired action in the provided views module.
Example:
from posts import views
urlpatterns = patterns('posts.views',
...
url(r'^(?P<id>\d+)/(?P<action>delete|publish|edit)/$', action(views), name="posts-action"),
...
)
In templates:
{% url posts-action id=post.id,action="delete" %}
This template tag build a Form splitted in fieldsets. The fieldsets are configured with a second parameter, that is a tuple like the one used in the Admin class in models in the attribute "fields".
You pass to the template the form and the tuple and than use them as parameters for the templatetag.
You can take a look at the source and modify It to build forms the way you like.
It is very useful If you do not like the way Django build forms with the methods as_p, as_ul or as_table and also do not like to write html by hand.
This is an extract of an example for use of "pisa" <http://www.htmltopdf.org> in "django". It shows the easiest way possible to create PDF documents just using HTML and CSS. In "index" we see the definition of the output of a form in which HTML code can be typed in and then on the fly a PDF will be created. In "ezpdf_sample" we see the use of templates and contexts. So adding PDF to your existing Django project could be just a matter of some lines of code.
This is a simple URL based breadcrumb filter I'm using on a site I'm currently building. To use it, just throw the code into template-tag file, and simply pass the current url in a page template like so
{{ request.get_full_path|breadcrumbs }}
As an example of how to style it, wrap it in a unordered list with an id of breadcrumb, and then in your styles use #breadcrumb li { display: inline; }.
On a contact form for a site user, this will produce something along the lines of
you are here : [home](/) » [users](/users/) » [username](/username/) » contact form
Hopefully someone may find it useful.
This exception middleware abstracts the functionality of the builtin exception handling mechanisms, but makes them extensible by inheritance.
Just add it (or some subclass) to the top of your active middleware list.
You can use this to [make your admin emails more informative](http://www.djangosnippets.org/snippets/631/) or [log errors to a file](http://www.djangosnippets.org/snippets/639/).
Ever wanted to add an atypical [field lookup](http://www.djangoproject.com/documentation/db-api/#field-lookups) to the Django Admin list_filter filters, like `__isnull` or `__in`? This jQuery snippet allows you to do just that.
Since you can access those additional filters by directly typing them into in the Admin URL, the tricky part is to add those to the regular list_filter display. A lot of this code is spent checking on querystring matches which is ugly and error-prone -- if you see any problems or room for improvement, drop me a comment!
A suggestion of where to place this code is in `templates/admin/yourapp/yourmodel/change_list.html`. Mine kinda looks like this:
{% extends "admin/change_list.html" %}
{% block content %}
<script src="/static/js/jquery-1.2.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
// the JavaScript posted in this snippet
</script>
{{ block.super }}
{% endblock %}
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.