我有一个问题:
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))
singleproduct.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 %}
url.py
(r'^products/(?P<productslug>.*)/$', 'zakai.views.SpecificProduct'),
models.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
答案 0 :(得分:0)
您正在访问网址/products//
,因此网址中的参数productslug
没有值。这意味着您的Product
查询失败:
product = Product.objects.get(slug=productslug)
因为productslug
没有(正确)值。要解决此问题,请将您的网址格式更改为:
(r'^products/(?P<productslug>.+)/$', 'zakai.views.SpecificProduct'),
,productslug
参数至少需要一个字符。为了改善它,你可以使用以下正则表达式,它只接受-
和字符(这是一个slug组成的):
(r'^products/(?P<productslug>[-\w]+)/$', 'zakai.views.SpecificProduct'),