在django模板中显示ManyToManyField

问题描述 投票:0回答:1

我一直试图用ManyToMany关系显示模型的确切值,但我不能,所有我所知道的是接收QuerySet

<QuerySet [<Stack: PHP>, <Stack: js>]> 

通过添加到模板标签

{{ brand.technologies.all }}

但我想收到并显示2个字段,名称和图标。我试过像一些循环

  {% for brand in brands %}
      {% for technologies in brand.technologies.all %} {{ technologies.name }} {{ technologies.icon }} {% endfor %}
  {% endfor %}

但它没有给出任何结果。语法没有问题,因为页面正在显示,看起来像这样

image

models.朋友

STACK = (
        ('PHP', 'PHP'),
        ('js', 'JavaScript'),
        ...
    )

STACK_ICONS = (
        ('/static/icons/stack/php.png', 'PHP'),
        ('/static/icons/stack/javascript.png', 'JavaScript'),
        ...
    )

class Company(models.Model):
    name = models.CharField(max_length=100, blank=False)
    students = models.CharField(max_length=3, choices=STUDENTS)
    type = models.CharField(max_length=15, choices=TYPES)
    workers = models.PositiveIntegerField(validators=[MinValueValidator(1)])
    city = models.CharField(max_length=15,choices=CITIES)
    company_about = models.TextField(max_length=500, blank=False)   
    slug = models.SlugField(unique=True)
    icon = models.ImageField(blank=True, null=True)
    image = models.ImageField(blank=True, null=True)
    logo = models.ImageField(blank=True, null=True)
    technologies = models.ManyToManyField('Stack')

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(Company, self).save(*args, **kwargs)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.name

# object stack relation manytomany with Company

class Stack(models.Model):
    name = models.CharField(max_length=30, choices=STACK)
    icon = models.CharField(max_length=80, choices=STACK_ICONS)

    def __str__(self):
        return self.name

views.朋友

from django.shortcuts import render, get_object_or_404, redirect
from django.utils import timezone
...

def comp_list(request):
    f = CompanyFilter(request.GET, queryset=Company.objects.all())
    return render(request, 'company/comp_list.html', {'filter': f})

def companies(request):
    company = get_object_or_404(Company)
    return render(request, 'company/comp_list.html', {'company': company})

def home(request):
    return render(request, 'company/home.html')

def brands(request, slug):
    brand = get_object_or_404(Company, slug=slug)
    return render(request, 'company/comp_view.html', {'brand': brand})

def stacks(request):
    stack = get_object_or_404(Stack)
    return render(request, 'company/comp_view.html', {'stack': stack})

comp_view.html

{% extends 'company/base.html' %}

{% block content %}
<div class="row company-details">
    <div class="col col-md-2"></div>
    <div class="col col-md-8">
    <input class="btn btn-success" type="button" value="Go Back" onclick="history.back(-1)" />
    <div class="col col-md-12 company-bg" style="background-image: url('{{ brand.image.url }}'); background-repeat: no-repeat, repeat;">
    </div>
    <div class="bottom-overlay"></div>
    <div class="company-description">
        <div class="heading">
            <div class="title">About us</div>
            <div class="social-media">
                <a href="">Facebook</a>
            </div>
        </div>
        <div class="company-about">
            {{ brand.company_about }}
        </div>



    </div>
    <div class="company-stats">
        <div class="company-logo-container">
            <img class="company-logo" src="{{ brand.logo.url }}">
        </div>
        <div class="company-attributes">
            <div class="field-group">
                <div class="field">Type</div>
                <div class="value">{{ brand.type }}</div>
            </div>
            <div class="field-group">
                <div class="field">Company size</div>
                <div class="value">{{ brand.workers }}+</div>
            </div>
            <div class="field-group">
                <div class="field">Headquarters</div>
                <div class="value">{{ brand.city }}</div>
            </div>
            <div class="field-group">
                <div class="field">Open for students</div>
                <div class="value">{{ brand.students }}</div>
            </div>
        </div>
        <div class="technologies-section">
            <p class="tech-heading">Technologies</p>
            <div class="technologies-wrapper">
                {{ brand.technologies.all }}
                {% for brand in brands %}
                    {% for technologies in brand.technologies.all %} {{ technologies.name }} {% endfor %}
                {% endfor %}

            </div>
        </div>
    </div>
    <div class="col col-md-2"></div>
</div>

{% endblock %}
django many-to-many django-orm manytomanyfield
1个回答
1
投票

我不明白为什么你突然通过brands添加了一个外环。您没有任何名为brands的内容,并且您通过brand成功访问了所有其他数据。继续这样做,然后放下外循环。

<div class="technologies-wrapper">
    {% for technologies in brand.technologies.all %} {{ technologies.name }} {% endfor %}
</div>
© www.soinside.com 2019 - 2024. All rights reserved.