如何将参数中的值用于另一个参数Django Views

时间:2015-09-09 12:15:14

标签: python django

我在django中有一个视图

def shoplist(request, shop, subcategory, discount, format=None):
    args = {}
    args.update(csrf(request))
    args['subc'] = ShopSubCategory.objects.get(id = subcategory).category_id
    args['shopfilters'] = Shop.objects.filter(shop_category_id = 'subc')
return render_to_response('shop-catlist.html', args)

此视图从请求(OK)

获取“子类别”

“子”根据子类别获取类别的ID(确定)

在模板中它可以很好地工作,但如果我尝试在“args ['shopfilters'}中使用”subc“,那么它会给出(带有基数10的int()的无效文字:'sub')

如何使用'sub'值作为'shopfilters'的过滤键?

修改

MODELS.PY(重要字符串)

class ShopCategory(models.Model):
    class Meta():
        db_table = "shop_category"
        verbose_name = "Категория магазина"
        verbose_name_plural = "Категории магазинов"
    section = models.ForeignKey(ShopSection,  blank=False)
    name = models.CharField('Name', max_length = 100)
    def __unicode__(self):
        return u'%s' % self.name

class ShopSubCategory(models.Model):
    class Meta():
        db_table = "shop_subcategory"
        verbose_name = "Подкатегория магазина"
        verbose_name_plural = "Подкатегории магазинов"
    category = models.ForeignKey(ShopCategory,  blank=False)
    name = models.CharField('Name', max_length = 100)
    def __unicode__(self):
        return u'%s' % self.name

class Shop(models.Model):
    class Meta():
        db_table = "shop"
        verbose_name = "магазин"
        verbose_name_plural = "магазины"
    shop_title = models.CharField(verbose_name = "Название магазина", max_length = 150, blank = False)
    shop_text = models.TextField(verbose_name = "Описание магазина", blank=False)
    shop_category = models.ForeignKey(ShopCategory, blank=False, verbose_name = "Категория магазина")

VIEWS.PY

def shoplist(request, shop, subcategory, discount, format=None):
    args = {}
    args.update(csrf(request))
    args['subc'] = ShopSubCategory.objects.all()
    if subcategory:
        args['subc'] = ShopSubCategory.objects.get(id = subcategory).category_id
    #args['subc'] = '1'
    args['shopfilters'] = Shop.objects.all()
    if subcategory:
        args['shopfilters'] = Shop.objects.filter(shop_category_id = args['subc'])
    args['products'] =  ShopProduct.objects.all().order_by('-id')
    if shop:
        args['products'] = ShopProduct.objects.filter(shop_product_shop_id= shop)
    if subcategory:
        args['products'] = ShopProduct.objects.filter(shop_product_category_id=subcategory)
    if discount:
        args['products'] = ShopProduct.objects.all().exclude(shop_product_price_old__isnull=True).exclude(shop_product_price_old__exact='')
    args['sections'] = ShopSection.objects.all().order_by('name')
    args['categories'] =  ShopCategory.objects.all().order_by('-name')
    args['subcats'] =  ShopSubCategory.objects.all().order_by('-name')
    args['username'] = auth.get_user(request).username
    args['reg_form'] = MyRegistrationForm()
    return render_to_response('shop-catlist.html', args)

URLS.PY

urlpatterns = patterns('',
    url(r'^store.aspx$', 'shop.views.shop_main'),
    url(r'^productlist.aspx&shop(?P<shop>\d*)&subcategory(?P<subcategory>\d*)&discount(?P<discount>\d*)$', 'shop.views.shoplist'),
)

0 个答案:

没有答案