サンプルの DJango コードを確認して、URL がどのように解決されるかを理解しようとしていますか?
list.html
カテゴリー
{% for c in active_categories %}
<a href="{{c.get_absolute_url}}">{{c.name}}</a><br />
{% endfor %}
urls.py
from django.conf.urls import *
urlpatterns = patterns('ecomstore.catalog.views',
(r'^$','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'),
)
上記のhtmlは、問題なくすべてのカテゴリをリストし、ブラウザに次のように入力すると呼び出されます.. [http:127.0.0.1:8000]
カーソルを合わせると - a href="{{p.get_absolute_url}} - URL が [http://127.0.0.1:8000/category/electronics/] に解決されます。
p.get_absolute_url はエレクトロニクスにのみ解決されますが、URL で「カテゴリ」がどのように解決されるのか疑問に思っています..
models.py
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_add=True)
class Meta:
db_table = 'categories'
ordering = ['-created_at']
verbose_name_plural = 'Categories'
def __unicode__(self):
return self.name
@models.permalink
def get_absolute_url(self):
return ('catalog_category',(),{'category_slug':self.slug})
私の質問が明確であることを願っています...