我试图在我的对象中使用 get_absolute_url 方法,但是当我引用它时,它失败了,我已经给出了下面的所有代码
模型.py
from django.db import models
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})
context_processor.py
from ecomstore.catalog.models import Category
from ecomstore import settings
def ecomstore(request):
return {'active_categories':Category.objects.filter(is_active=True),
'site_name':settings.SITE_NAME,
'meta_keywords':settings.META_KEYWORDS,
'meta_description':settings.META_DESCRIPTION,
'request':request}
category_list.html
<h3>Categorie</h3>
{% for c in active_categories %}
<a href="{{ c.name }}">{{c.name}}</a><br/>
<a href="{{ c.get_absolute_url }}" class="category">{{ c.name }}</a><br />
{% endfor %}
错误:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.4.3
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'ecomstore.catalog',
'ecomstore.utils',
'django.contrib.admin')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Template error:
In template C:\python27\django-projects\ecomstore\ecomstore\preview\templates\tags\category_list.html, error at line 5
unexpected end of pattern
1 : <h3>Categorie</h3>
2 : {{active_categories}}
3 : {% for c in active_categories %}
4 : <a href="{{ c.name }}">{{c.name}}</a><br/>
5 : <a href=" {{ c.get_absolute_url }} " class="category">{{ c.name }}</a><br />
6 : {% endfor %}
7 :
Traceback:
File "c:\python27\lib\site-packages\django\core\handlers\base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\Django-projects\ecomstore\ecomstore\catalog\views.py" in index
8. return render_to_response(template_name,locals(),context_instance=RequestContext(request))
File "c:\python27\lib\site-packages\django\shortcuts\__init__.py" in render_to_response
20. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "c:\python27\lib\site-packages\django\template\loader.py" in render_to_string
176. return t.render(context_instance)
File "c:\python27\lib\site-packages\django\template\base.py" in render
140. return self._render(context)
File "c:\python27\lib\site-packages\django\template\base.py" in _render
134. return self.nodelist.render(context)
File "c:\python27\lib\site-packages\django\template\base.py" in render
823. bit = self.render_node(node, context)
File "c:\python27\lib\site-packages\django\template\debug.py" in render_node
74. return node.render(context)
File "c:\python27\lib\site-packages\django\template\loader_tags.py" in render
123. return compiled_parent._render(context)
File "c:\python27\lib\site-packages\django\template\base.py" in _render
134. return self.nodelist.render(context)
File "c:\python27\lib\site-packages\django\template\base.py" in render
823. bit = self.render_node(node, context)
File "c:\python27\lib\site-packages\django\template\debug.py" in render_node
74. return node.render(context)
File "c:\python27\lib\site-packages\django\template\loader_tags.py" in render
123. return compiled_parent._render(context)
File "c:\python27\lib\site-packages\django\template\base.py" in _render
134. return self.nodelist.render(context)
File "c:\python27\lib\site-packages\django\template\base.py" in render
823. bit = self.render_node(node, context)
File "c:\python27\lib\site-packages\django\template\debug.py" in render_node
74. return node.render(context)
File "c:\python27\lib\site-packages\django\template\loader_tags.py" in render
62. result = block.nodelist.render(context)
File "c:\python27\lib\site-packages\django\template\base.py" in render
823. bit = self.render_node(node, context)
File "c:\python27\lib\site-packages\django\template\debug.py" in render_node
74. return node.render(context)
File "c:\python27\lib\site-packages\django\template\loader_tags.py" in render
155. return self.render_template(self.template, context)
File "c:\python27\lib\site-packages\django\template\loader_tags.py" in render_template
137. output = template.render(context)
File "c:\python27\lib\site-packages\django\template\base.py" in render
140. return self._render(context)
File "c:\python27\lib\site-packages\django\template\base.py" in _render
134. return self.nodelist.render(context)
File "c:\python27\lib\site-packages\django\template\base.py" in render
823. bit = self.render_node(node, context)
File "c:\python27\lib\site-packages\django\template\debug.py" in render_node
74. return node.render(context)
File "c:\python27\lib\site-packages\django\template\defaulttags.py" in render
185. nodelist.append(node.render(context))
File "c:\python27\lib\site-packages\django\template\debug.py" in render
84. output = self.filter_expression.resolve(context)
File "c:\python27\lib\site-packages\django\template\base.py" in resolve
571. obj = self.var.resolve(context)
File "c:\python27\lib\site-packages\django\template\base.py" in resolve
721. value = self._resolve_lookup(context)
File "c:\python27\lib\site-packages\django\template\base.py" in _resolve_lookup
772. current = current()
File "c:\python27\lib\site-packages\django\utils\functional.py" in _curried
11. return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
File "c:\python27\lib\site-packages\django\db\models\base.py" in get_absolute_url
883. return settings.ABSOLUTE_URL_OVERRIDES.get('%s.%s' % (opts.app_label, opts.module_name), func)(self, *args, **kwargs)
File "c:\python27\lib\site-packages\django\db\models\__init__.py" in inner
35. return reverse(bits[0], None, *bits[1:3])
File "c:\python27\lib\site-packages\django\core\urlresolvers.py" in reverse
476. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "c:\python27\lib\site-packages\django\core\urlresolvers.py" in _reverse_with_prefix
363. possibilities = self.reverse_dict.getlist(lookup_view)
File "c:\python27\lib\site-packages\django\core\urlresolvers.py" in reverse_dict
276. self._populate()
File "c:\python27\lib\site-packages\django\core\urlresolvers.py" in _populate
253. for name in pattern.reverse_dict:
File "c:\python27\lib\site-packages\django\core\urlresolvers.py" in reverse_dict
276. self._populate()
File "c:\python27\lib\site-packages\django\core\urlresolvers.py" in _populate
243. p_pattern = pattern.regex.pattern
File "c:\python27\lib\site-packages\django\core\urlresolvers.py" in regex
162. compiled_regex = re.compile(self._regex, re.UNICODE)
File "c:\python27\lib\re.py" in compile
190. return _compile(pattern, flags)
File "c:\python27\lib\re.py" in _compile
242. raise error, v # invalid expression
Exception Type: error at /
Exception Value: unexpected end of pattern