我试图通过单击显示产品下方的href链接导航到我的产品页面。但是由于某种原因,生成的绝对URL给了我404。这是我到目前为止尝试过的。 models.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=50)
slug = models.SlugField(max_length=50, unique=True,
help_text='Unique value for product page URL, created from name.')
description = models.TextField()
is_active = models.BooleanField(default=True)
meta_keywords = models.CharField("Meta Keywords", max_length=255,
help_text='Comma-delimited set of SEO keywords for meta tag')
meta_description = models.CharField("Meta Description", max_length=255,
help_text='Content for description meta tag')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return 'catalog_category', (), {'category_slug': self.slug}
class Meta:
ordering = ['-created_at']
verbose_name_plural = 'Categories'
class Product(models.Model):
name = models.CharField(max_length=255, unique=True)
slug = models.SlugField(max_length=255, unique=True,help_text = 'Unique value for product page URL, created from name.')
brand = models.CharField(max_length=50)
sku = models.CharField(max_length=50)
price = models.DecimalField(max_digits=9, decimal_places=2)
old_price = models.DecimalField(max_digits=9, decimal_places=2,blank = True, default = 0.00)
image = models.CharField(max_length=50)
is_active = models.BooleanField(default=True)
is_bestseller = models.BooleanField(default=False)
is_featured = models.BooleanField(default=False)
quantity = models.IntegerField()
description = models.TextField()
meta_keywords = models.CharField(max_length=255,help_text = 'Comma-delimited set of SEO keywords for meta tag')
meta_description = models.CharField(max_length=255,help_text = 'Content for description meta tag')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
categories = models.ManyToManyField(Category)
def __str__(self):
return self.name
def get_absolute_url(self):
return 'catalog_product', (), {'product_slug': self.slug}
def sale_price(self):
if self.old_price > self.price:
return self.price
else:
return None
class Meta:
ordering = ['-created_at']
urls.py
from django.conf.urls import patterns, url, include
urlpatterns = patterns('catalog.views',
(r'^index/$', 'index', {'template_name': 'catalog/index.html'}, 'catalog_home'),
(r'^category/(?P<category_slug>[-\w]+)/$', 'show_category',
{'template_name': 'catalog/category.html'}, 'catalog_category'),
(r'^product/(?P<product_slug>[-\w]+)/$', 'show_product',
{'template_name': 'catalog/product.html'}, 'catalog_product'),
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': '/home/surajt/Downloads/ecommerce/static'}),
)
这是我在html中执行代码检查时看到的。
这就是在html中生成href url的方式,这就是我单击url时得到的内容。 127.0.0.1:8000/demo/category/尿布/(u'catalog_product',%20(),%20%7Bu'product_slug':%20u'huggies'%7D)
我做错了什么?
答案 0 :(得分:0)
您需要通过reverse
传递参数,然后还要在中间省略那些空括号,有点像这样;
from django.core.urlresolvers import reverse
def get_absolute_url(self):
return reverse('catalog_product', kwargs={'product_slug': self.slug})
您可以在此处查看示例; https://godjango.com/67-understanding-get_absolute_url/