I want that each user in my website will have an image in his profile. I don't need any thumbnails or something like that, just picture for each user. The simpler the better. The problem is I don't know how to insert this type of field to my user profile. Any suggestions?
相关问题
- Django __str__ returned non-string (type NoneType)
- Django & Amazon SES SMTP. Cannot send email
- Django check user group permissions
- Django restrict pages to certain users
- UnicodeEncodeError with attach_file on EmailMessag
相关文章
- Profiling Django with PyCharm
- Why doesn't Django enforce my unique_together
- MultiValueDictKeyError in Django admin
- Django/Heroku: FATAL: too many connections for rol
- Django is sooo slow? errno 32 broken pipe? dcramer
- Django: Replacement for the default ManyToMany Wid
- Upgrading transaction.commit_manually() to Django
- UnicodeEncodeError when saving ImageField containi
Assuming you're using standard
contrib.auth
, you can designate a model as an 'user profile' model, viaAUTH_PROFILE_MODULE
setting. You can then use it to attach any additional information you want to theUser
objects, e.g.You need to make a form that has a clean method that validates the properties you're looking for:
Hope that helps you out.
To connect the UserProfile model to your User model make sure you are extending your User model as fully explained in this tutorial: http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/
This will allow you to access UserProfile attributes for your User, including the avatar, using user.get_profile().avatar. (Note the syntax in different in your template, see below for how to display the avatar in your template.)
You can use an image field in your UserProfile model for the avatar:
This works exactly like a FileField but is specific for images and validates that the uploaded object is a valid image. To limit the file size you can use the answer given here by @pastylegs: Max image size on file upload
Then, assuming your userprofile model is called UserProfile, you access the avatar in your template as follows:
More about the image field here: https://docs.djangoproject.com/en/dev/ref/models/fields/#imagefield