This is the code for a template tag. Put this code in your template to render your messages:
{% for message in messages %}
{% render_user_message message %}
{% endfor %}
When you're adding a message to the user's message set, follow these rules: If you want a message to appear as an error, append "0001" to the end of it. To appear as a notice, append "0002" to it. To appear as a happy message, appear "0000" to it. If no code is present, it will default to displaying as an error. This makes use of the classes "error", "notice", and "success", so you need to define these in your CSS.
For help with custom template tags, see [the Django docs](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/)
This blog post outlined how to get the user from the session key:
http://scottbarnham.com/blog/2008/12/04/get-user-from-session-key-in-django/
Unfortunately, it assumes DB-backed session and auth backends. This isn't required, so this snippet provides a backend-agnostic way to do the same thing.
>>> skey = 'ea0ed02d35d43aeaf20b3ef516f51396'
>>> user_from_session_key(skey)
<User: jeremyd>
Use a `UserField` if you want to replace the usual select menu with a simple input field that only accepts valid user names. Should be easy to generalize for other models by passing a query set and the attribute name that represents the instance.
Example:
class Book(models.Model):
owner = models.ForeignKey(User)
class BookForm(forms.ModelForm):
owner = UserField()
class Meta:
model = Book
I needed to be able to synchronize my LDAP users and groups to the Django database. This may not be as efficient as some might like but it works like a charm. It returns a list of messages that I pipe into request.user.messages in my template.
**This is an alternative to User.get_profile.**
Rather than having you call `User.get_profile` directly, this retrieves the profile instance for a `User` and attaches the fields from the profile to the `User` object when instantiated. The special methods for `DateField`, `FileField`, `ImageField` and fields with `choices` are also created.
Since the profile object still has to be retrieved from the database before its fields can be added to the `User`, the costs for using this might outweigh the rewards unless you are heavily using profiles.
To install, place it in a module on your `PYTHONPATH` and add it to `INSTALLED_APPS`.
Many models are tightly coupled to the default Django `User` model (`django.contrib.auth.models.User`).
Sometimes this user model just doesn't fit everyone's needs. By using `UserForeignKey` it is possible to make the `User` model configurable, encouraging loose coupling.
Additionally, this can help to prevent circular imports between `User` and another model.
Use it like a standard `ForeignKey`... it accepts all the same arguments.
If you want to use a `User` model other than the default, just add `USER_MODEL` to your settings file.... it uses dotted notation (`app_label.model_name`).
Example:
class BlogPost(models.Model):
user = UserForeignKey(related_name="blog_posts")
title = models.CharField(...)
content = models.TextField(...)
**How to use**:
1. Install [**PyFacebook** package](http://wiki.developers.facebook.com/index.php/PythonPyFacebookTutorial).
2. After make all steps in tutorial above, put this code in your app's models.py module (you maybe prefer split it and put the middleware class in some other file).
3. Put the FacebookUserMiddleware python-path in the MIDDLEWARE_CLASSES in your settings.py (after facebook.djangofb.FacebookMiddleware).
You probably will add some fields to FacebookUser model class :)
This is a little helper for associating an owner to a newly created object, in this case making the assumption that the current user is always the owner. It removes the necessity of adding a custom save hook to your model.
get_current_user comes from this middleware trick to cache the current user:
http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser
**How to use:**
1. puts this code at the end of the `models.py` file who haves the User Profile class declared;
2. verify if your User Profile class has the name 'UserProfile'. If not, change the code to the right name.
**About:** this snippet makes the ORM create a profile each time an user is created (or updated, if the user profile lost), including 'admin' user.
This tag builds on top of the [ifusergroup tag](http://www.djangosnippets.org/snippets/282/), fixes a small bug and introduces support for else blocks.
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.
This code gives you the ability to authenticate a user by their email address instead of the username. I've also added the ability to authenticate via LDAP.
Some code used from this [snippet](http://www.djangosnippets.org/snippets/74/)
This very simple middleware will set the 'user' attribute of a mod_python request.. this way the username is logged into the apache log ... useful when making webalizer statistics to see the activity of users ..
(http://sct.sphene.net)