我正在做一个基于开源的项目。它叫askbot。我想给它添加a/b测试,在做了一些研究之后,我发现了django-lean。我不是django的专家,但我设法将django-lean的剥离版本降低到我的askbot-dlevel版本。我用了blog post。但我的问题是我得到了以下错误:
TemplateSyntaxError at /questions/
Encountered unknown tag 'experiment'.
Request Method: GET
Request URL: http://localhost:8001/questions/
Django Version: 1.4.10
Exception Type: TemplateSyntaxError
Exception Value:
Encountered unknown tag 'experiment'.只是为了给出更多关于我如何合并django-lean的信息:正如博客文章提到的那样,我只使用了实验模块:
我从django-lean的askbot文件夹中添加了实验文件夹,并使用设置文件将其公开为另一个install-app。所以现在它看起来像是另一个应用程序。
我将experiments.py和smartif.py复制到askbot-dlevel/templatetags,因为这是基于django documentation的正确做法
在askbot-dlevel中有一个utils文件夹,其中有decorators.py,我添加了以下内容:
def set_experiment_user(view_func):
'''Decorator for setting the WebUser for use with ab split testing assumes
first argument is the request object'''
@functools.wraps(view_func)
def decorator(request, *args, **kwargs):
WebUser(request).confirm_human()
return view_func(request, *args, **kwargs)
return decorator现在,正如我在博客中提到的,我在我的视图中添加了以下内容:
@csrf.csrf_protect
@decorators.set_experiment_user
def ask_widget(request, widget_id):
def post_question(data, request):
thread = models.Thread.objects.create_new(**data)
question = thread._question_post()
request.session['widget_question_url'] = question.get_absolute_url()
return question
widget = get_object_or_404(models.AskWidget, id=widget_id)
...在我的模板中,就像博客文章提到的那样,我做了以下事情:
在模板中,我做了以下工作:
{% import "macros.html" as macros %}
{% load experiments %}
{% experiment experiment_name top_contributors %}
{% if contributors and settings.SIDEBAR_MAIN_SHOW_AVATARS %}
{% include "widgets/contributors.html" %}
{% endif %}
{% endexperiments %}在这一点上,正如博客文章中提到的那样,事情应该是可行的。使用我创建的管理控制台进行实验,实验的名称是top_contributors。如果一切顺利的话,所有的用户都会参与实验。但是我得到了上面提到的错误,它告诉我模板标签还没有注册。
askbot-dlevel项目使用了jinja2 (我认为),但看起来博客帖子已经在常规的django模板模式下编写了模板代码。这会是一个问题吗?如果是这样的话,我怎么能把它转换成jinja呢?如果不是,我在这里遗漏了什么?
据我所知,我试图将博客帖子转换为jinja2:
{% if experiments.experiment('top_contributors') %}
{% if contributors and settings.SIDEBAR_MAIN_SHOW_AVATARS %}
{% include "widgets/contributors.html" %}
{% endif %}
{% endif %}在此之后,我得到以下错误:
UndefinedError at /questions/
'experiments' is undefined
Request Method: GET
Request URL: http://localhost:8001/questions/
Django Version: 1.4.10
Exception Type: UndefinedError
Exception Value:
'experiments' is undefined
Exception Location: ..python2.7/site-packages/Jinja2-2.7.1-py2.7.egg/jinja2/environment.py in getattr, line 397
Python Executable: ../ABFrameWork/bin/python发布于 2018-08-10 13:55:55
在运行服务器之前尝试执行迁移。
https://stackoverflow.com/questions/21142591
复制相似问题