django中的动态URL

时间:2017-02-22 01:06:35

标签: python django django-models django-views django-urls

我是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

1 个答案:

答案 0 :(得分:1)

我认为你从不在视图中使用newshop_id。

因此,您无需在视图和网址中传递newshop_id。