1

我有个问题:

DoesNotExist at /products//

Product matching query does not exist.

Request Method: GET

Django Version: 1.4
Exception Type: DoesNotExist
Exception Value:

Product matching query does not exist.

Exception Location: /Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/db/models/query.py in get, line 366
Python Executable: /usr/bin/python
Python Version: 2.7.1 

vews.py

def SpecificProduct(request, productslug):
    product = Product.objects.get(slug=productslug)
    context = {'product': product}
    return render_to_response('singleproduct.html',
                           context, context_instance=RequestContext(request))

单品.html

{% extends "base.html" %}

{% block content %}
<div id = "singleproduct">
        <p>Name: {{ product }}</p>
        <p>Description: {{ product.en_description }}</p>
        <p>Description: {{ product.sp_description }}</p>

</div>
{% endblock %}

网址.py

(r'^products/(?P<productslug>.*)/$', 'zakai.views.SpecificProduct'),

模型.py

class Product(models.Model):
    en_name = models.CharField(max_length=100)
    sp_name = models.CharField(max_length=100)
    slug = models.SlugField(max_length=80)
    en_description = models.TextField(blank=True, help_text="Describe product in english")
    sp_description = models.TextField(blank=True, help_text="Describe product in spanish")
    photo = ThumbnailerImageField(upload_to="product_pic", blank=True)

    def __unicode__(self):
        return self.en_name    
4

1 回答 1

0

您正在访问 URL /products//,因此 URL 中的参数没有值productslug。这意味着您的Product查询失败:

product = Product.objects.get(slug=productslug)

因为它没有(正确的)值productslug。要修复它,请将您的 URL 模式更改为:

(r'^products/(?P<productslug>.+)/$', 'zakai.views.SpecificProduct'),

参数至少需要一个字符productslug。为了进一步改进它,您可以使用以下仅接受-和字符的正则表达式(这是 slug 的组成部分):

(r'^products/(?P<productslug>[-\w]+)/$', 'zakai.views.SpecificProduct'),
于 2012-07-17T09:49:52.037 回答