How can I include user profile's images/logos on django comments

别来无恙 提交于 2020-01-01 15:05:14

问题


I've posted this on django-users but haven't received a reply!

So I have my own profile objects for users (subclass of User). One of the fields is imagefield, which is (obviously) used for users to upload their logo/thumbnail.

The question is how I can include this on their comments?

Any ideas? Thanks in advance!


回答1:


Subclassing User is not recommended in Django. Instead, create a separate profile model.

Let's assume you have your app, foo.

In foo's models.py, add:

class FooUserProfile(models.Model):
   user = models.ForeignKey(User, unique=True)
   user_image = models.ImageField()

Then, in your settings.py, add:

AUTH_PROFILE_MODULE = 'foo.FooUserProfile'

Now, whenever you have a user object, you can get its profile object with the get_profile() function. In your case, in the template you could add:

<img src="{{ comment.user.get_profile.user_image }}"/>

A caveat: you will need to create a FooUserProfile and associate it with your User any time you create new User.

You can read more in the Django documentation, Storing additional information about users or in the article Django tips: extending the User model



来源:https://stackoverflow.com/questions/492987/how-can-i-include-user-profiles-images-logos-on-django-comments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!