Django:获取正确对象的问题

时间:2019-11-15 01:52:25

标签: python django

我是一个初学者,可以通过构建一个名为PhoneReview的应用程序来学习Django。它将存储与最新手机有关的评论。它还将显示电话品牌,以及相关的电话型号及其评论。

现在,我面临一个问题。我通过Django管理员添加了两个电话品牌,三星和苹果。它们显示在http://127.0.0.1:8000/index

enter image description here

当我单击Samsung时,我看到三个电话型号,Galaxy S10,Galaxy Note 10和iPhone 11,它们已经通过Django admin添加了。但是,不应在此显示iPhone 11,因为它的品牌是Apple,而不是三星。因此,基本上,当我单击Samsung时,应该只显示Galaxy S10和Galaxy Note 10,而不显示iPhone 11。 enter image description here 我该如何解决该问题?

这是我位于PhoneReview文件夹中的 models.py 的代码:

from django.db import models
from django.template.defaultfilters import slugify

# Create your models here.
class Brand(models.Model):
    brand_name = models.CharField(max_length=100)
    origin = models.CharField(max_length=100)
    manufacturing_since = models.CharField(max_length=100, null=True, blank=True)

    def __str__(self):
        return self.brand_name

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

class PhoneModel(models.Model):
    brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
    model_name = models.CharField(max_length=100)
    launch_date = models.CharField(max_length=100)
    platform = models.CharField(max_length=100)

    def __str__(self):
        return self.model_name

class Review(models.Model):
    phone_model = models.ManyToManyField(PhoneModel, related_name='reviews')
    review_article = models.TextField()
    date_published = models.DateField(auto_now=True)
    # slug = models.SlugField(max_length=150, null=True, blank=True)
    link = models.TextField(max_length=150, null=True, blank=True)

    def __str__(self):
        return self.review_article

这是我位于PhoneReview文件夹中的 urls.py 的代码:

from . import views
from django.urls import path

urlpatterns = [
    path('index', views.BrandListView.as_view(), name='brandlist'),
    path('phonemodel/<int:pk>/', views.ModelView.as_view(), name='modellist'),
    path('details/<int:pk>/', views.ReviewView.as_view(), name='details'),
]

这是我位于PhoneReview文件夹中的 views.py 的代码:

from django.shortcuts import render
from django.views import generic
from .models import Brand, PhoneModel, Review


class BrandListView(generic.ListView):
    template_name = 'PhoneReview/index.html'
    context_object_name = 'all_brands'

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


class ModelView(generic.ListView):
    template_name = 'PhoneReview/phonemodel.html'
    context_object_name = 'all_model_name'

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

class ReviewView(generic.DetailView):
    model = Review
    template_name = 'PhoneReview/details.html'

这是我位于PhoneReview文件夹中的 apps.py 代码:

from django.apps import AppConfig


class PhonereviewConfig(AppConfig):
    name = 'PhoneReview'

这是我在模板文件夹中的 index.html 代码:

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

{% load static %}

{% block title%}
Brand List
{% endblock %}

{% block content %}
<!--Page content-->
<h1>This is Brand List Page</h1>
<h2>Here is the list of the brands</h2>
    <ul>
        {% for brand in all_brands %}
<!--            <li>{{ brand.brand_name }}</li>-->
            <li><a href = "{% url 'modellist' brand.id %}">{{ brand.brand_name }}</a></li>
        {% endfor %}
    </ul>
<img src="{% static "images/brandlist.jpg" %}" alt="Super Mario Odyssey" /> <!-- New line -->
{% endblock %}

这是我在模板文件夹中的 phonemodel.html 代码:

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

{% load static %}

{% block title%}
Phone Model Page
{% endblock %}

{% block content %}
<!--Page content-->
<h1>This is Phone Model Page</h1>
<h2>Here is the phone model</h2>
    <ul>
        {% for phonemodel in all_model_name %}
            <li><a href = "{% url 'details' phonemodel.id %}">{{ phonemodel.model_name }}</a></li>
        {% endfor %}
    </ul>
<img src="{% static "images/brandlist.jpg" %}" alt="Super Mario Odyssey" /> <!-- New line -->
{% endblock %}

这是我的 details.html 代码,位于模板文件夹中:

{% extends 'PhoneReview/base.html' %}
{% load static %}

<html>

<link rel="stylesheet" type="text/css" href="{% static "css/style.css" %}">


<html lang="en">

{% block title%}Details{% endblock %}

{% block content %}

<h1>This is the Details Page</h1>

<h2>Review:</h2>
<p>{{ review.review_article }}</p>

<h2>News Link:</h2>
<p>{{ review.link }}</p>
{% endblock %}
</html>

我在 views.py 中做错了什么吗?

1 个答案:

答案 0 :(得分:3)

更改列表视图,请参见this

class ModelView(generic.ListView):
    template_name = 'PhoneReview/phonemodel.html'
    context_object_name = 'all_model_name'

   def get_queryset(self):
        self.brand = get_object_or_404(Brand, pk=self.kwargs['pk'])
        return PhoneModel.objects.filter(brand=self.brand)