在Django模板中显示ManyTomanyfield



我一直在尝试以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 %}

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

图像

models.py

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.py

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 %}

我不明白为什么您突然通过brands添加了外循环。您没有任何称为brands的东西,并且您成功地通过brand访问了所有其他数据。只是继续这样做,然后放下外循环。

<div class="technologies-wrapper">
    {% for technologies in brand.technologies.all %} {{ technologies.name }} {% endfor %}
</div>

相关内容

  • 没有找到相关文章

最新更新