I'm having an issue displaying a ManyToManyField in a character profile 
template using Django.

The template has to show an avatar, alongside some information about the 
character, including a list of guilds this character had joined. But, when 
I try to display that list, it appears as if it is empty. I had a look into 
my site administration page, and that information is there, so I have 
confirmation that the data can be displayed in my template. I just don't 
know how. How could I handle that?

These are my models:

""" Modelos de la aplicación de personajes """
from django.db import models


class Guild(models.Model):
    """ Modelo del clan """
    guild_name = models.CharField(max_length=25)
    rank = models.PositiveSmallIntegerField()


    def __str__(self):
        return self.guild_name


class Character(models.Model):
    """ Modelo del personaje """
    character_name = models.CharField(max_length=25)
    level = models.PositiveSmallIntegerField()
    race = models.CharField(max_length=25)
    gender = models.CharField(max_length=25)
    job = models.CharField(max_length=25)
    avatar = models.ImageField(upload_to='', blank=True)
    character_guilds = models.ManyToManyField(Guild, blank=True)


    def __str__(self):
        return self.character_name+': '+self.race+' '+self.job+' level '+str
(self.level)+'. '

My view.py file:

def characterprofile(request, pk):
    """ Método para mostrar el perfil de un personaje """
    template = '../templates/character/characterProfile.html'
    profile = Character.objects.get(pk=pk)
    guilds = profile.character_guilds.all()
    context = {
        'profile': profile,
        'guilds': guilds
    }
    return render (request, template, context)

And my template.html:

{% extends 'base.html' %}


{% block content %}
    <header>
        <h2>{{ profile.character_name }}'s profile</h2>
    </header>
    <nav class="nav nav-pills nav-justified">
        <a class="nav-item nav-link" href="{% url 'characters' 
%}">Return</a>
    </nav>
    <br>
    <div class="container">
        <div class="container">
            {% if profile.avatar %}
            <img class="rounded" src="{{ profile.avatar.url }}" width="184" 
height="184">
            {% else %}
            <p>There is no avatar to display here.</p>
            {% endif %}
        </div>
        <br>
        <h4>Information</h4>
        <ul class="list-group list-group-flush-sm">
            <li class="list-group-item">Name: {{ profile.character_name 
}}</li>
            <li class="list-group-item">Level: {{ profile.level }}</li>
            <li class="list-group-item">Race: {{ profile.race }}</li>
            <li class="list-group-item">Gender: {{ profile.gender }}</li>
            <li class="list-group-item">Job: {{ profile.job }}</li>
            <li class="list-group-item">
                Guilds: 
                {% for g in guilds %} 
                    {{ g }} 
                {%endfor%}
            </li>
        </ul>
        <br>
    </div>
    <br>
    <footer>
        <span>Copyright© 2020</span>
    </footer>
{% endblock %}

Many thanks in advance!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e8ede78e-a01a-4bcf-8895-005af4fd8a60%40googlegroups.com.

Reply via email to