我在进行视图计数,但是会导致错误 [英] I'm doing a view count, but it causes an error

查看:54
本文介绍了我在进行视图计数,但是会导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误名称"page_hits"用takes_context = True修饰,因此它的第一个参数必须为"context"我让视图计数器.该功能必须处理人员进入站点并将其输出到模板的情况.

Name of error 'page_hits' is decorated with takes_context=True so it must have a first argument of 'context' I make the view counter. It is necessary that the function handles the entry of a person to the site and output it to a template.

请提供现成的代码以解决已阅读的很多内容 https://docs.djangoproject.com/en/2.2/howto/custom-template-tags/

Please give ready-made code for fix already read a lot about 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

推荐答案

名字应该是 context ,而不是 ctx ,如 simple_tag 的文档代码> [Django-doc] :

The first name should be context, not ctx, as is specified in the documentation of simple_tag [Django-doc]:

如果您的模板标记需要访问当前上下文,则可以使用注册标签时 takes_context 参数:

If your template tag needs to access the current context, you can use the takes_context argument when registering your tag:

(...)

请注意,第一个参数必须称为 context .

Note that the first argument must be called context.

因此您需要将其重写为:

So you need to rewrite it to:

@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

这篇关于我在进行视图计数,但是会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆