【问题标题】:Django ManyToMany relationship details of detailsDjango ManyToMany 关系详情详情
【发布时间】:2019-03-27 19:01:24
【问题描述】:

我已经很好地阅读了有关 ManyToMany 归档的 Django 文档。尤其是here 。我很清楚可以用它做的事情。例如,如果我有一个 Category 模型和一个 Startup 模型,其中一个类别可以有许多初创公司,而一个初创公司可以属于许多类别,那么 ManyToMany 关系在这里很有用。我可以在我的模板上列出所有类别,每个类别都是可点击的,并导致属于该类别的另一个模板上的所有初创公司的列表。 现在,有了这个,我想更进一步。转到特定类别的详细信息页面,这是属于它的初创公司的列表,我希望此详细信息页面中的每个项目也指向另一个详细信息页面,我可以在其中显示有关特定初创公司的更多信息。

我现在的主要问题是如何实现它。我已经处理了许多与 ManyToMany 字段及其查询相关的问题,尤其是here,但它们似乎都在讨论如何将相关对象访问到某个类别,例如在详细信息页面中,并且到此为止。我想从详情页转到另一个详情页。

from django.db import models

我的类别模型:

class Category(models.Model):
    name = models.Charfield(max_length=100)

    def __str__(self):
        return self.name

    def get_startups(self):
        return Startup.objects.filter(category=self)

我的创业模型:

class Startup(models.Model):
    name = models.CharField(max_length=100)
    founder_name = models.CharField(max_length=100)
    short_description = models.CharField(max_length=225)
    concept = models.TextField(help_text='how does this startup operate?')
    category = models.ManyToManyField(Category)
    website = models.URLField(max_length=225)
    logo = models.ImageField()


    def __str__(self):
        return self.name

    @property
    def logo_url(self):
        if self.logo and hasattr(self.logo, 'url'):
            return self.logo.url

在我的views.py文件中:

class CategoryView(ListView):
    template_name = 'myapp/startup-category.html'
    context_object_name = 'category_list'

    def get_queryset(self): 
        return Category.objects.all()


class DetailView(DetailView):
    model=Category
    template_name = 'myapp/startup-list.html'

在我的模板/myapp/startup-category 中:

{% if category_list %}
<ul>
    {% for category in category-list %}
        <li><a href="{% url 'detail' category.pk %}">{{category.name}}</a></li>
    {% endfor %}
</ul>
{% endif %}

在模板/myapp/startup-list.html 中:

{% for startup in category.get_startups %}
    <tr>
        {% if startup.logo %}
        <td><img src="{{ startup.logo_url|default_if_none:'#' }}" style="height: 50px; width: 50px;"></td>
        {% endif %}
        <td>{{startup.name}}</td>
        <td> {{startup.short_description}}</td>
        <td>
            <button type="button" ><a href="">View startup</a>
            </button></td>
    </tr>
{% endfor %}

在 myapp.urls 中:

from django.urls import path
from . import views as core_views

path('startup_categories/', core_views.CategoryView.as_view(), name='startups'),
path('startup_categories/<int:pk>/', core_views.DetailView.as_view(), name='detail'),

这很好用。我得到一个类别列表;当我点击一个类别时,它会将我带到一个详细信息页面,在该页面中,我会以表格形式获取与该类别相关的初创公司列表。在每个启动元素上,都有一个用于查看启动详细信息的按钮。 如何实现启动详情视图?

提前感谢您!

【问题讨论】:

    标签: django django-templates django-views manytomanyfield


    【解决方案1】:

    定义一个类似的网址:

     path('/startup/<int:startup_id>', views.view_startup, name='view-startup'),
    

    在 forms.py 中:(如果您没有 forms.py,请在同一个应用程序中创建它)

     from .models import Startup
     from django.forms import ModelForm
    
     class StartupForm(ModelForm):
          class Meta:
               model = Startup
               fields = ['name', 'founder_name'] #Put name of all the fields that you want to display in template here
    

    在views.py中

     from .forms import StartupForm
     def view_startup(request, startup_id):
         startup = get_object_or_404(Startup, pk=startup_id)
         startup_details_form = StartupForm(instance = startup)
    
         return render(request, "startup_details.html", {"form":startup_details_form})
    

    在startup_details.html中

      {% for field in form %}
           {{ field.label_tag }} : {{ field.value }}
      {% endfor %}
    

    现在在你的 startuplist.html 中你可以简单地放

      <button type="button" ><a href="{% url 'view-startup' startup.id %}">View startup</a>
    

    这只是给你一个基本的想法。在 url 中使用 ID 可能不是一个好习惯。您可以在启动模型中创建另一个包含 url 的 slug 字段,然后您可以修改相关文件以实际使用 slug 而不是 ID

    【讨论】:

    • 为什么需要 if?你不是一直寄表格吗?
    • 行得通!唯一的问题是,对于徽标,它显示的是名称而不是图像。为了解决这个问题,我停止了遍历字段,而是这样做了:{% if form %}&lt;p&gt;Name: {{form.name.value}}&lt;/p&lt;p&gt;Founder: {{form.founder_name.value}}&lt;/p&gt;&lt;img src='http://localhost:8000/{{form.logo.value}}'&gt;{% endif %} 我将看看如何修复硬编码的图像 url。我在我的 Startup 模型中定义了它,但似乎我无法通过表单访问它。或者也许我错过了什么。
    • @codeDude 是的,在这种情况下,没有必要。我喜欢使用if 以便能够处理错误。不过我这里不需要。
    • @JohnBagiliko 对于图像,如果您使用 {{ form.logo.value.url }},您将获得完整路径。 PS:如果运行正常,你能接受答案吗?
    猜你喜欢
    • 2012-07-07
    • 1970-01-01
    • 2012-10-27
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    • 2010-10-01
    相关资源
    最近更新 更多