2017-02-22 125 views
0

我對django非常陌生。請快速幫助,將不勝感激。django中的動態網址

url(r'^shops/(?P<Newshop_id>[0-9]+)/$', views.shop_single, name='singleshop') 

此路徑給我找不到頁面錯誤。

models.py

from __future__ import unicode_literals 
from django.db import models 

class SliderTitle(models.Model): 
    slider_title = models.CharField(max_length=20) 
    def __str__(self): 
     return self.slider_title 

class Slider(models.Model): 
    slider_type = models.OneToOneField(SliderTitle) 
    slider = models.FileField(blank=True) 
    def __str__(self): 
     return str(self.slider_type) 

class ShopCategories(models.Model): 
    category = models.CharField(max_length=50, unique=True) 
    def __str__(self): 
     return self.category 

class NewShop(models.Model): 
    category = models.ForeignKey(ShopCategories) 
    main_image = models.FileField() 
    name = models.CharField(max_length=100, unique=True) 
    tagline = models.CharField(max_length=50, default='Enter tagline here2') 
    description = models.TextField(default='enter shop description') 
    shop_image = models.FileField() 
    def __str__(self): 
     return self.name 

urls.py

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'^$', views.homepage, name='homepage'), 
    url(r'^about/', views.about, name='about'), 
    url(r'^shops/(?P<Newshop_id>[0-9]+)/$', views.shop_single, name='singleshop') 

views.py

def shop_single(request, Newshop_id): 
    cat1 = NewShop.objects.filter(category_id=1) 
    cat2 = NewShop.objects.filter(category_id=2) 
    cat3 = NewShop.objects.filter(category_id=3) 
    cat4 = NewShop.objects.filter(category_id=4) 
    name1 = ShopCategories.objects.filter(id=1) 
    name2 = ShopCategories.objects.filter(id=2) 
    name3 = ShopCategories.objects.filter(id=3) 
    name4 = ShopCategories.objects.filter(id=4) 

    return render_to_response('shop_single.html', {'shop_name1': name1, 'shop_name2': name2, 'shop_name3': name3, 
              'shop_name4': name4, 'Shop_cat1': cat1, 'Shop_cat2': cat2, 
              'Shop_cat3': cat3, 
              'Shop_cat4': cat4, }) 

my DB from phpmyadin

+0

你看到的堆棧跟蹤是什麼? – karthikr

+0

包含像「(?P [0-9] +)」這樣的正則表達式的URL通常用於* retreive *現有的對象記錄。 'Newshop_id'變量顯然不會在你的視圖中使用,這意味着你沒有檢索任何東西。這種觀點你打算髮生什麼? – ChidG

+0

你甚至沒有說過你實際上要到什麼網址。 –

回答

1

我想你永遠不會使用newshop_id在您的視圖。

因此,您不必在您的視圖和網址中傳遞該newshop_id。

+0

非常感謝,我不得不再次閱讀URL的完整文檔。我修好了。學習編程語言需要大量的耐心和持久性 –