每次我运行我的django项目时,都会收到此错误NoReverseMatch at /
Reverse for 'product_list_by_category' with keyword arguments '{'slug': 'legume'}'
。我想列出我的产品,说实话,我甚至不知道问题是什么。
完整跟踪:django.urls.exceptions.NoReverseMatch:反向'product_list_by_category',找不到关键字参数'{'slug':'legume'}'。尝试过1种模式:['(?P \ w +)/ $'] [29 / Nov / 2017 22:18:22]“GET / HTTP / 1.1”500 180670
我的主要网址
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('shop.urls', namespace = 'shop'))
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
我的 Models.py 页面
from django.db import models
from django.core.urlresolvers import reverse
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=120 , db_index = True)
slug = models.SlugField(max_length=200 , db_index = True , unique= True)
class Meta:
ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = 'categories'
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('shop:product_list_by_category', kwargs={'slug':self.slug})
class Product(models.Model):
category = models.ForeignKey(Category, related_name='products')
name = models.CharField(max_length=200, db_index = True)
slug = models.SlugField(max_length=200, db_index = True)
image = models.ImageField(upload_to='products/%y/%m/%d', blank=True)
description = models.TextField(blank = True)
price = models.DecimalField(max_digits=10, decimal_places=2)
stock = models.PositiveIntegerField()
available = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now= True)
class Meta:
ordering = ('-created',)
index_together = (('id','slug'),)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('shop:product_detail', args=[self.id, self.slug])
我的应用网址
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.product_list, name='product_list'),
url(r'^(?P<category_slug>\w+)/$', views.product_list, name='product_list_by_category'),
url(r'^(?P<id>\d+)/(?P<slug>[-\w]+)/$', views.product_detail, name='product_detail'),
]
这是我的 Base.HTML
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}AgroShop{% endblock %}</title>
<link rel = stylesheet href = '{% static "css/base.css" %}'>
</head>
<body>
<div class="header">
<a href="/" class = "logo">AgroShop</a>
</div>
<div class="subheader">
<div class="cart">
{% with total_items=cart|length %}
{% if cart|length > 0 %}
Your cart :
<a href="{% url "cart:cart_detail" %}">
{{ total_items }} item{{ total_items|pluralize }}
</a>
{% else %}
Your cart is empty.
{% endif %}
{% endwith %}
</div>
</div>
<div id="content">
{% block content %}
{% endblock %}
</div>
</body>
</html>
List.HTML
{% extends 'shop/base.html' %}
{% load static %}
{% block title %}
{% if category %}{{ category.name }}{% else %}{{ Products }}{% endif %}
{% endblock %}
{% block content %}
<div class="sidebar">
<h3>Categories:</h3>
<ul>
<li {% if not category %} class="selected">{% endif %}>
<a href="{% url 'shop:product_list' %}">All</a>
</li>
{% for c in categories %}
<li {% if category.slug == c.slug %} class = 'selected{% endif %}'>
<a href="{{ c.get_absolute_url }}">{{ c.name }}</a>
</li>
{% endfor %}
</ul>
</div>
<div id="main" class="product-list">
<h1>{% if category %}{{ category.name }}{% else %}Products{% endif %}</h1>
{% for product in products %}
<div class="item">
<a href="{{ product.get_absolute_url }}">
<img src="{% if product.image %}{{ product.image.url }}{% else %}{% static 'img/dirig.jpg' %}{% endif %}" alt="">
</a>
<a href="{{ product.get_absolute_url }}">{{ product.name }}</a><br>
{{ product.price }}
</div>
{% endfor %}
</div>
{% endblock %}
答案 0 :(得分:1)
您在网址格式中使用category_slug
,但在拨打反向时使用slug
。确保网址格式get_absolute_url
方法和视图都匹配。
答案 1 :(得分:0)
reverse('shop:product_list_by_category', kwargs={"category_slug": "myslug"})
或者:
reverse('shop:product_list_by_category', args=["myslug"])
但是reverse('shop:product_list_by_category', kwargs={'slug': "myslug"})
不起作用,因为你的kwarg中的键slug
与你的网址中的category_slug