我是通过构建名为PhoneReview的应用程序学习Django的初学者。它将存储与最新手机有关的评论。它还将显示电话品牌,以及相关的电话型号及其评论。
我已经创建了模型,视图和模板文件。现在,我面临一个问题。尝试使用子弹时,出现NoReverseMatch错误。看起来像这样:
ERROR: (gcloud.app.deploy) Error Response: [9] Cloud build eaf3e253-ee83-46d4-a640-fd7e68083a13 status: FAILURE.
Build error details: {"error":{"errorType":"BuildError","canonicalCode":"INVALID_ARGUMENT","errorId":"2BCB87EC","errorMessage":"INFO FTL version node-v0.17.0\n
INFO Beginning FTL build for node\n
INFO FTL arg passed: exposed_ports None\n
INFO FTL arg passed: cache_repository us.gcr.io/piva-primero/app-engine-tmp/build-cache/ttl-7d\n
INFO FTL arg passed: tar_base_image_path None\n
INFO FTL arg passed: export_cache_stats False\n
INFO FTL arg passed: builder_output_path \"\"\n
INFO FTL arg passed: name us.gcr.io/piva-primero/app-engine-tmp/app/ttl-2h:55db4aec-8e29-42ea-bea3-c457808a429c\n
INFO FTL arg passed: ttl 168\n
INFO FTL arg passed: global_cache False\n
INFO FTL arg passed: cache True\n
INFO FTL arg passed: upload True\n
INFO FTL arg passed: sh_c_prefix False\n
INFO FTL arg passed: fail_on_error True\n
INFO FTL arg passed: base us.gcr.io/gae-runtimes/nodejs10:nodejs10_20191019_10_16_3_RC00\n
INFO FTL arg passed: output_path None\n
INFO FTL arg passed: cache_key_version v0.17.0\n
INFO FTL arg passed: cache_salt \n
INFO FTL arg passed: directory /workspace\n
INFO FTL arg passed: entrypoint None\n
INFO FTL arg passed: additional_directory /.gaeconfig\n
INFO FTL arg passed: destination_path /srv\n
INFO FTL arg passed: verbosity NOTSET\n
INFO starting: full build\n
INFO starting: builder initialization\n
INFO Loading Docker credentials for repository 'us.gcr.io/gae-runtimes/nodejs10:nodejs10_20191019_10_16_3_RC00'\n
INFO Loading Docker credentials for repository 'us.gcr.io/piva-primero/app-engine-tmp/app/ttl-2h:55db4aec-8e29-42ea-bea3-c457808a429c'\n
INFO builder initialization took 0 seconds\n
INFO starting: build process for FTL image\n
INFO starting: rm_node_modules\n
INFO rm_node_modules rm -rf /workspace/node_modules\n
INFO `rm_node_modules` stdout:\n\n
INFO rm_node_modules took 0 seconds\n
INFO using descriptor:package-lock.json\n
INFO using descriptor:package.json\n
INFO starting: checking_cached_packages_json_layer\nDEBUG Checking cache for cache_key ddd19bc8f86cc8fedfd69dfce5aac6d21a6e2024dec42d6d3a96af6fc7a78dbd\n
INFO No cached base image found for entry: us.gcr.io/piva-primero/app-engine-tmp/build-cache/ttl-7d/node-cache:ddd19bc8f86cc8fedfd69dfce5aac6d21a6e2024dec42d6d3a96af6fc7a78dbd.\n
INFO Cache miss on local cache for us.gcr.io/piva-primero/app-engine-tmp/build-cache/ttl-7d/node-cache:ddd19bc8f86cc8fedfd69dfce5aac6d21a6e2024dec42d6d3a96af6fc7a78dbd\n
INFO No cached dependency layer for ddd19bc8f86cc8fedfd69dfce5aac6d21a6e2024dec42d6d3a96af6fc7a78dbd\n
INFO [CACHE][MISS] v0.17.0:NODE-\u003eddd19bc8f86cc8fedfd69dfce5aac6d21a6e2024dec42d6d3a96af6fc7a78dbd\n
INFO checking_cached_packages_json_layer took 0 seconds\n
INFO starting: building_packages_json_layer\n
INFO starting: npm_install\n
INFO npm_install npm install --production\n
INFO `npm_install` stdout:\n\n\u
003e grpc@1.7.3 install /workspace/node_modules/@google-cloud/video-intelligence/node_modules/grpc\n\u
003e node-pre-gyp install --fallback-to-build --library=static_library\n\n
make: Entering directory '/workspace/node_modules/@google-cloud/video-intelligence/node_modules/grpc/build'\n
make: Entering directory '/workspace/node_modules/@google-cloud/video-intelligence/node_modules/grpc/build'\n
CC(target) Release/obj.target/grpc/deps/grpc/src/core/lib/surface/init.o\n
CC(target) Release/obj.target/grpc/deps/grpc/src/core/lib/surface/init.o\n
grpc.target.mk:405: recipe for target 'Release/obj.target/grpc/deps/grpc/src/core/lib/surface/init.o' failed\n
make: Leaving directory '/workspace/node_modules/@google-cloud/video-intelligence/node_modules/grpc/build'\n
CC(target) Release/obj.target/grpc/deps/grpc/src/core/lib/channel/channel_args.o\n
Failed to execute '/usr/bin/node /usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js build --fallback-to-build --library=static_library --module=/workspace/node_modules/@google-cloud/video-intelligence/node_modules/grpc/src/node/extension_binary/node-v64-linux-x64-glibc/grpc_node.node --modul.
但是,在urls.py中使用主键时,我没有遇到任何问题。当我尝试在URL中使用Slug时,就会出现问题。
这是我位于PhoneReview文件夹中的 models.py 的代码:
NoReverseMatch at /index
Reverse for 'modellist' with arguments '('',)' not found. 1 pattern(s) tried: ['phonemodel/(?P<slug>[-a-zA-Z0-9_]+)$']
这是我位于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(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)
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('phonemodel/<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, 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'
这是我在模板文件夹中的 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' phonemodel.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>
{% for phonemodel in all_model_name %}
<li><a href = "{% url 'PhoneReview:details' review.slug %}">{{ phonemodel.model_name }}</a></li>
{% endfor %}
</ul>
<img src="{% static "images/brandlist.jpg" %}" alt="Super Mario Odyssey" /> <!-- New line -->
{% endblock %}
我在 index.html 和 phonemodel.html 中做错什么了吗?
答案 0 :(得分:1)
在您的index.html
中,而不是phonemodel.slug
中应该是brand.slug
{% for brand in all_brands %}
<!-- <li>{{ brand.brand_name }}</li>-->
<li><a href = "{% url 'PhoneReview:modellist' brand.slug %}">{{ brand.brand_name }}</a></li>
{% endfor %}
与您的phonemodel.html
{% for phonemodel in all_model_name %}
<li><a href = "{% url 'PhoneReview:details' phonemodel.slug %}">{{ phonemodel.model_name }}</a></li>
% endfor %}