jinja2引用js中的变量,提示变量未定义


#1

我有一段这样的代码,我想要根据ajax请求的结果动态创建jinja2模板中的超链接,但是系统提示我ajax回调函数的结果未定义

$.ajax({
            url:"http://127.0.0.1:5000/next",
            type:'GET',
            data:{'id':{{blog.id}},"select":"{{ blog.select }}"},
            cache:false,
            dataType:'json',
            success:function (data) {
                if (data.code===200){
                    $("#next-article")[0].innerText = data.title;
                    $("#next-article")[0].attr("href","{{ url_for('web.detail',id=data.id) }}")
                } else {
                    $("#next-article")[0].innerText = data.message;
                    $("#next-article")[0].attr("href","#")
                }
            }
        })

报错信息:

Traceback (most recent call last):
  File "D:\ABC\virtu-manage\blog\lib\site-packages\flask\app.py", line 2311, in wsgi_app
    response = self.full_dispatch_request()
  File "D:\ABC\virtu-manage\blog\lib\site-packages\flask\app.py", line 1834, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "D:\ABC\virtu-manage\blog\lib\site-packages\flask\app.py", line 1737, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "D:\ABC\virtu-manage\blog\lib\site-packages\flask\_compat.py", line 36, in reraise
    raise value
  File "D:\ABC\virtu-manage\blog\lib\site-packages\flask\app.py", line 1832, in full_dispatch_request
    rv = self.dispatch_request()
  File "D:\ABC\virtu-manage\blog\lib\site-packages\flask\app.py", line 1818, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "D:\ABC\edit\app\web\comment.py", line 31, in detail
    reply_form=reply_form,pageinations=pageinations,comments=comments)
  File "D:\ABC\virtu-manage\blog\lib\site-packages\flask\templating.py", line 135, in render_template
    context, ctx.app)
  File "D:\ABC\virtu-manage\blog\lib\site-packages\flask\templating.py", line 117, in _render
    rv = template.render(context)
  File "D:\ABC\virtu-manage\blog\lib\site-packages\jinja2\asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "D:\ABC\virtu-manage\blog\lib\site-packages\jinja2\environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "D:\ABC\virtu-manage\blog\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "D:\ABC\virtu-manage\blog\lib\site-packages\jinja2\_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "D:\ABC\edit\app\templates\detail.html", line 3, in top-level template code
    {% from 'macro/comment.html' import comment %}
  File "D:\ABC\edit\app\templates\base.html", line 134, in top-level template code
    {% block content %}{% endblock %}
  File "D:\ABC\edit\app\templates\detail.html", line 162, in block "content"
    $("#next-article")[0].attr("href","{{ url_for('web.detail',id=data.id) }}")
  File "D:\ABC\virtu-manage\blog\lib\site-packages\jinja2\environment.py", line 430, in getattr
    return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'data' is undefined

希望大家帮我看看


#2

这里是不是没有传递data变量给template?

File "D:\ABC\edit\app\web\comment.py", line 31, in detail reply_form=reply_form,pageinations=pageinations,comments=comments)

对照这里:

$("#next-article")[0].attr("href","{{ url_for('web.detail',id=data.id) }}")

#3

我这个请求是通过Ajax过来的,异步加载数据,我并不想在页面刚开始加载的时候就获取到这个值,只有页面滑动到dom元素是才会发起数据的请求