【发布时间】:2019-11-14 12:25:13
【问题描述】:
我是一名初学者,通过构建一个名为PhoneReview 的app 来学习Django。它将存储与最新手机相关的评论。它还将显示手机品牌,以及相关的手机型号及其评论。
现在,我刚刚在URLs 中添加了使用slug 的代码后遇到了一个错误。当我转到http://127.0.0.1:8000/index 时,我看到了这个页面:
当我点击“三星”时,我收到了这个错误:
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 文件夹中的 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)
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 文件夹中的 urls.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'),
]
这是我的 views.py 代码,位于 PhoneReview 文件夹中:
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'
这是我位于 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 'PhoneReview:modellist' brand.slug %}">{{ 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>
<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 %}
这是我的 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>
我觉得我在 index.html 或 phonemodel.html 上犯了一个错误。但作为一个初学者,我无法掌握它。
我该如何解决这个问题?
更新:按照@c.grey 的建议,我在phonemodel.html 中添加了以下代码来循环手机模型:
<ul>
{% for model_name in all_model_name %}
<li><a href = "{% url 'PhoneReview:details' details.slug %}">{{ phonemodel.model_name }}</a></li>
{% endfor %}
</ul>
另外,我在 index.html 中添加了这一行:
<li><a href = "{% url 'PhoneReview:modellist' phonemodel.slug %}">{{ brand.brand_name }}</a></li>
另外,我在views.py中添加了这些代码:
class ModelView(generic.ListView):
template_name = 'PhoneReview/phonemodel.html'
context_object_name = 'all_model_name'
def get_queryset(self):
return PhoneModel.objects.all()
但是现在,我收到了这个错误:
django.urls.exceptions.NoReverseMatch: Reverse for 'modellist' with arguments '('',)' not found. 1 pattern(s) tried: ['index/(?P<slug>[-a-zA-Z0-9_]+)/$']
这是我的项目文件的链接: https://github.com/shawnmichaels583583/phoneradar
【问题讨论】:
-
在网址中尝试使用
'index/<slug>/'而不是'index/<slug:slug>/'。 -
能否显示完整的错误回溯
-
我不明白你想达到什么目的:点击一个品牌(三星)时,你想显示什么?现在您似乎正试图显示一个特定型号的详细视图,但品牌页面不是显示所有手机的列表吗?即它应该指向一个您尚未创建的 ListView。
-
@Clarity 它不起作用。我仍然遇到同样的错误。
-
@dirkgroten 当我点击品牌(三星)时,我应该会被带到phonemodel.html,该页面将显示三星的所有手机型号,如Galaxy S10。当我点击 Galaxy S10 时,我应该被带到 details.html 页面,该页面将查看特定手机型号(Galaxy S10)以及一个链接。
标签: django django-models django-forms django-templates django-views