繁体   English   中英

脚本在 Django 模板中不起作用

[英]script doesn't work in django template

我试图按照教程进行操作,但由于某种原因,JS 在我的模板中什么也没做。

我复制了其中的每一步,添加了一个 id 为“newItems”的锚点 div,并按要求包含了无限滚动脚本。

我的模板如下所示:

<body>
{% for i in "0123456789" %}
{% for j in "0123456789" %}
<li>{{i}} , {{j}}</li>
{% endfor %}
{% endfor %}
<a id="newItems">in here</a>

<script>
$(document).ready(function(){ 
$(window).bind('scroll', loadOnScroll);
});
// Scroll globals
...
SAME AS IN THE TUTORIAL
...
</script>
</body>

我的观点.py:

def debate_archive(request):
    debates = range(1,1000)
    paginator = Paginator(debates, 10)
    if request.method == 'GET':
        if request.is_ajax():
            if request.GET.get('page_number'):
                # Paginate based on the page number in the GET request
                page_number = request.GET.get('page_number');
                try:
                    page_objects = paginator.page(page_number).object_list
                except InvalidPage:
                    return HttpResponseBadRequest(mimetype="json")
                # Serialize the paginated objects
                resp = serialize_debates(page_objects)
                return HttpResponse(json.dumps(resp), mimetype='json')
    debates = paginator.page(1).object_list
    return render(request, 'polls/example.html', locals())

我的 urls.py:

    url(r'^test/$', views.debate_archive, name='home'), 

有什么想法吗?

问题似乎出在另一个模板中的模板上。 我遇到了同样的问题,但找不到解决方案。 如果静态脚本位于另一个模板的模板中,则不会加载它们,并且不会执行内联脚本。

我有同样的错误。 在控制台 chrome 中说: Uncaught TypeError: $ is not a function Here is Uncaught TypeError: $ is not a function solution work with me: you must use jQuery, and $ is not used (this is for compatible with other libraries) If you must use $(document).ready(function(){ ,您实际上可以将 $ 传递给函数调用:

jQuery(function ($) { ...

在这里找到更多

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM