2014-03-25 47 views
0

我目前收到試圖去網址... /調查/產品時,這個錯誤/Django的失蹤url參數>視圖

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}) 

urls.py

from django.conf.urls import patterns, url 

from polls import views 

urlpatterns = patterns('', 
    url(r'products/$', views.productindex, name='productindex'), 
) 

回答

0

視圖期待request和產物峯現在它正在得到請求,但不是PK。

這是一種將product.pk傳遞給視圖的方法。

urls.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>