python - 我正在计算观看次数,但这会导致错误

标签 python html django

错误名称“page_hits”用takes_context=True修饰,因此它必须有第一个参数“context” 我制作了 View 计数器。该函数必须处理人员对网站的输入并将其输出到模板。

请提供现成的代码进行修复已经阅读了很多 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'

View .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,如 documentation of simple_tag [Django-doc] 中指定的那样。 :

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

(...)

Note that the first argument must be called context.

所以你需要将其重写为:

@register.simple_tag(takes_context=True)
def page_hits(<b>context</b>, page_url=None):
    counter = PageHit.objects.filter(
        url=<b>context</b>['request'].path if page_url is None else page_url
    ).first()
    return 0 if counter is None else counter.count

关于python - 我正在计算观看次数,但这会导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57027321/

相关文章:

python - Django Social Auth - 用户已创建,配置文件已创建,但用户未登录

python - 将 Tastypie REST API 投影到 python 对象中

python - 组合 Pandas 中的行

python - 动态模块创建

python - 用于验证字典的内置解决方案

python - 从 Python 写入机器人框架控制台

html - 在保持列对齐的同时将文本居中放置在图像上

javascript - CSS float 弹出窗口 : Tricky CSS Positioning

Android 4.1.2 默认浏览器 - 禁用输入字段的渲染问题

python - 是否可以生成整个 Django webapp 的图表?