Django:在URL中使用Slug时出现“找不到页面”错误

时间:2019-11-14 12:25:13

标签: django django-models django-forms django-templates django-views

我是通过构建Django的{​​{1}}来学习app的初学者。它将存储与最新手机有关的评论。它还将显示电话品牌,以及相关的电话型号及其评论。

现在,我刚在PhoneReview中添加了使用slug的代码后就遇到了错误。当我转到URLs时,会看到以下页面: enter image description here

当我单击“三星”时,出现此错误

http://127.0.0.1:8000/index

我已经成功执行了迁移。但是,我仍然面临这个问题。 这是我位于PhoneReview文件夹中的 models.py 的代码:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/index/samsung/
Raised by:  PhoneReview.views.ModelView
No phone model found matching the query

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

这是我位于PhoneReview文件夹中的 urls.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)
    slug = models.SlugField(unique=True, max_length=150)

    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)
    slug = models.SlugField(unique=True, max_length=150)

    def __str__(self):
        return self.model_name

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

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文件夹中的 views.py 的代码:

from . import views
from django.urls import path

app_name = 'PhoneReview'

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

这是我位于PhoneReview文件夹中的 apps.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.DetailView):
    model = PhoneModel
    template_name = 'PhoneReview/phonemodel.html'
    context_object_name = 'phonemodel'

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

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

from django.apps import AppConfig


class PhonereviewConfig(AppConfig):
    name = 'PhoneReview'

这是我在模板文件夹中的 phonemodel.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 'PhoneReview:modellist' brand.slug %}">{{ brand.brand_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 %}

{% 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>
        <li><a href = "{% url 'PhoneReview:details' details.slug %}">{{ phonemodel.model_name }}</a></li>
    </ul>
<img src="{% static "images/brandlist.jpg" %}" alt="Super Mario Odyssey" /> <!-- New line -->
{% endblock %}

我觉得在index.html或phonemodel.html上都犯了一个错误。但是作为一个初学者,我无法理解。

如何解决此问题?

更新:按照@ c.grey的建议,我在phonemodel.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>

此外,我在index.html中添加了这一行:

<ul>
    {% for model_name in all_model_name %}
        <li><a href = "{% url 'PhoneReview:details' details.slug %}">{{ phonemodel.model_name }}</a></li>
    {% endfor %}
</ul>

此外,我在views.py中添加了以下代码:

<li><a href = "{% url 'PhoneReview:modellist' phonemodel.slug %}">{{ brand.brand_name }}</a></li>

但是现在,我收到此错误:

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

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

这是我的项目文件的链接: https://github.com/shawnmichaels583583/phoneradar

2 个答案:

答案 0 :(得分:3)

基本上,您正在传递品牌模型子弹以获取 PhoneModel模型数据

要从PhoneModel获取详细信息,您需要传递PhoneModel子句以获取数据

答案 1 :(得分:0)

您的错误是您的链接指向brand.slug(此处为samsung),但在我看来您没有名称仅为samsung的手机型号,您呢? ?

您将品牌和手机型号混在一起