.. _ref-change-user-image-or-avatar: =========================== Change user image or avatar =========================== .. _Gravatar: http://gravatar.com/ .. _django-avatar: https://github.com/grantmcconnaughey/django-avatar .. _django-contrib-comments: https://django-contrib-comments.readthedocs.io/ .. _admin UI: http://localhost:8000/admin/ By default django-comments-xtd uses its own template filters to fetch user profile images from Gravatar_ and put them in comments. It is a simple solution that makes the app dependant on an external service. But it is a good solution when the only confirmed record we get from the person who posted the comment is an email address. This page describes how to setup django-comments-xtd so that user images displayed in comments come from other sources. For such purpose, in addition to the default filters, we will use django-avatar_. Your Django project can use another application to procure user images, perhaps with a built-in solution or via a social service. Just adapt your case to the methodology exposed here. .. contents:: Table of Contents :depth: 1 :local: Purpose ======= This how-to will combine both solutions, template filters provided with django-comments-xtd and template tags provided with django-avatar, to fetch user images for two different type of comments, those posted by registered users and those posted by mere visitors: * Avatar images in comments posted by **non-registered users** will be fetched from Gravatar via django-comments-xtd filters, as the only record we get from those visitors is their email address. * On the other hand avatar images in comments posted by **registered users** will be provided by django-avatar templatetags, as django-avatar's templatetags require a user object to fetch the user's image. Django-avatar will make those images available from different sources depending on how we customize the app. We can add user images directly using Django's admin interface or we can let the app fetch them via providers, from social networks or writing our own specific provider function. For the purpose of this how-to we will use the :ref:`example-comp` introduced in the Demo projects page. Before you continue reading please, visit the samples :ref:`example-setup` page and get the :ref:`example-comp` up and running. Add django-avatar to the Comp project ===================================== Install django-avatar with ``pip install django-avatar``, and be sure that the ``comp/settings.py`` module contains the following entries: .. code-block:: python MEDIA_ROOT = os.path.join(PROJECT_DIR, "media") MEDIA_URL = '/media/' INSTALLED_APPS = [ ... 'avatar', ... ] Then run the migrate command to create django-avatar tables: ``python manage.py migrate``. Also change the ``comp/urls.py`` module to serve media files in development and get access to users' images stored with django-avatar: .. code-block:: python from django.conf import settings from django.conf.urls.static import static [...] # At the bottom of the module. if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) When using HTML templates to render the comments ================================================ If your project uses django-comments-xtd HTML templates to render comments, like the Comp project's **quotes app** does, then you only have to adapt your project's HTML templates to use django-avatar. Let's go then through the following changes: * Change the ``comment_tree.html`` template. * Create the ``comments/preview.html`` template. * Create the ``django_comments_xtd/comment.html`` template (bonus). Change the ``comment_tree.html`` template ----------------------------------------- The :ref:`example-comp` uses the **render_xtdcomment_tree** template tag to render the tree of comments in ``quotes/quote_detail.html``. **render_xtdcomment_tree** in turn renders the ``django_comments_xtd/comment_tree.html`` template. The comp project overrides the ``comment_tree.html`` template. Let's edit it (in ``comp/templates/django_comments_xtd``) to make it start as follows: .. code-block:: html+django {% load l10n %} {% load i18n %} {% load comments %} {% load avatar_tags %} {% load comments_xtd %} {% for item in comments %}