Python Django在url>视图中缺少参数
我当前在尝试转到url…/polls/products/Python Django在url>视图中缺少参数,python,django,Python,Django,我当前在尝试转到url…/polls/products/ TypeError at /polls/products/ productindex() takes exactly 2 arguments (1 given) 我已经测试了附加在视图上的url,我确信它可以正常工作,所以我猜我的视图有问题 models.py class Product(models.Model): product_name = models.CharField(max_length=200) pro
TypeError at /polls/products/
productindex() takes exactly 2 arguments (1 given)
我已经测试了附加在视图上的url,我确信它可以正常工作,所以我猜我的视图有问题
models.py
class Product(models.Model):
product_name = models.CharField(max_length=200)
product_description = models.TextField()
def __unicode__(self):
return self.product_name
class Image(models.Model):
product_image = models.ForeignKey(Product)
image = models.ImageField(upload_to='image')
views.py
def productindex(request, product_image_id):
product = get_object_or_404(Product, pk=product_image_id)
return render(request, 'polls/products.html', {'product': product})
url.py
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'products/$', views.productindex, name='productindex'),
)
视图需要请求和产品pk。现在它得到的是请求,而不是pk
下面是将product.pk传递给视图的一种方法
URL.py
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'products/(?P<pk>\d+)/$', views.productindex, name='productindex'),
)
你的html应该是这样的
<a href="{% url 'productindex' product.pk %}">{{ product }}</a>