错误名称“ page_hits”装饰有takes_context = True,因此它的第一个参数必须为“ context” 我让视图柜台。该功能必须处理人员进入站点并将其输出到模板的情况。
请提供现成的代码以解决已阅读的很多内容 https://docs.djangoproject.com/en/2.2/howto/custom-template-tags/
Traceback:
File "D:\Users\MAestro\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "D:\Users\MAestro\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "D:\Users\MAestro\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Users\MAestro\Desktop\RapterGame.com\RapterGames\news\views.py" in Detail
195. return render(request,'news/post_detail.html')
File "D:\Users\MAestro\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\shortcuts.py" in render
36. content = loader.render_to_string(template_name, context, request, using=using)
File "D:\Users\MAestro\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\loader.py" in render_to_string
61. template = get_template(template_name, using=using)
File "D:\Users\MAestro\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\loader.py" in get_template
15. return engine.get_template(template_name)
File "D:\Users\MAestro\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\backends\django.py" in get_template
34. return Template(self.engine.get_template(template_name), self)
File "D:\Users\MAestro\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\engine.py" in get_template
143. template, origin = self.find_template(template_name)
File "D:\Users\MAestro\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\engine.py" in find_template
125. template = loader.get_template(name, skip=skip)
File "D:\Users\MAestro\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\loaders\base.py" in get_template
30. contents, origin, origin.template_name, self.engine,
File "D:\Users\MAestro\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in __init__
156. self.nodelist = self.compile_nodelist()
File "D:\Users\MAestro\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in compile_nodelist
194. return parser.parse()
File "D:\Users\MAestro\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in parse
478. raise self.error(token, e)
File "D:\Users\MAestro\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in parse
476. compiled_result = compile_func(self, token)
File "D:\Users\MAestro\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\library.py" in compile_func
121. kwonly, kwonly_defaults, takes_context, function_name,
File "D:\Users\MAestro\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\library.py" in parse_bits
250. "have a first argument of 'context'" % name)
Exception Type: TemplateSyntaxError at /Detail/
Exception Value: 'page_hits' is decorated with takes_context=True so it must have a first argument of 'context'
views.py
from django.template.loader_tags import register
from news.models import PageHit
from django import template
@register.simple_tag(takes_context=True)
def page_hits(ctx, page_url=None):
counter = (PageHit.objects
.filter(url=(ctx['request'].path if page_url is None else page_url))
.first())
return 0 if counter is None else counter.count
答案 0 :(得分:2)
名字应该是context
,而不是documentation of simple_tag
[Django-doc]中指定的ctx
:
如果您的模板标记需要访问当前上下文,则可以使用 注册标签时使用
takes_context
参数:(...)
请注意,第一个参数必须称为
context
。
因此您需要将其重写为:
@register.simple_tag(takes_context=True)
def page_hits(context, page_url=None):
counter = PageHit.objects.filter(
url=context['request'].path if page_url is None else page_url
).first()
return 0 if counter is None else counter.count
答案 1 :(得分:1)
来自the docs(在takes_context的首次提及中)“请注意,第一个参数必须称为上下文。” 更改为
@register.simple_tag(takes_context=True)
def page_hits(context, page_url=None):
counter = (PageHit.objects
.filter(url=(context['request'].path if page_url is None else page_url))
.first())
return 0 if counter is None else counter.count