python - Django:在 URL 中使用 Slug 时出现 FieldError
问题描述
我是 Django 的初学者。现在,我正在构建一个名为 PhoneReview 的应用程序。它将存储与最新手机相关的评论。它还将显示手机品牌,以及相关的手机型号及其评论。
现在,我正在尝试在 URL 中使用 slug。我已经在我的两个模板中成功使用了 slug,它们是index.html和phonemodel.html。但是,我遇到了第三个模板的问题,即details.html。
当我去http://127.0.0.1:8000/index时,我看到了这个页面:
当我点击Samsung时,我看到这个页面:
到此为止还好。但是当我点击任何手机型号时,比如Galaxy S10,我会收到FieldError错误。它看起来像这样:
FieldError at /details/galaxy-note-10
Cannot resolve keyword 'slug' into field. Choices are: date_published, id, link, phone_model, review_article
当我点击三星时,我应该会看到details.html页面,其中包含对手机的评论以及新闻链接。相反,我收到了 404 错误。
这是我位于 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(max_length=150, 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)
slug = models.SlugField(max_length=150, null=True, blank=True)
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)
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('phonemodel/<slug:slug>', views.ModelView.as_view(), name='modellist'),
path('details/<slug:slug>', views.ReviewView.as_view(), name='details'),
]
以下是位于 PhoneReview 文件夹中的views.py代码:
from django.shortcuts import render, get_object_or_404
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):
self.brand = get_object_or_404(Brand, slug=self.kwargs['slug'])
return PhoneModel.objects.filter(brand=self.brand)
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>
{% for phonemodel in all_model_name %}
<li><a href = "{% url 'PhoneReview:details' phonemodel.slug %}">{{ 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>
<a href={{ review.link }}>{{ review.link }}</a>
{% endblock %}
</html>
models.py或details.html有什么问题吗?
解决方案
推荐阅读
- c# - 计算实时并发流的数量
- caching - Laravel 7 Cache via Middleware 未访问缓存内容并发送 X-Proxy-Cache: MISS 标头
- ios - 如何使用 HMAccessoryPayLoad 添加 HomeKit 配件?
- html - 将css应用于内部(移动)
- gcc - Intel 线程积木 gcc 版本
- tensorflow - 如何将 PoseNet ResNet50 tfjs 模型转换为 tflite?
- amazon-ec2 - 使用 Terraform 通过 EC2 创建时,如何向 AWS EBS 添加标签?
- java - servlet [dispatcherServlet] 的 Servlet.service() 在路径 [] 的上下文中抛出异常 NullPointerException
- mysql - 重构 MySQL 表,为每个 id 创建一列
- python - 从网站请求数据时出现验证码错误