简体   繁体   English

脚本在 Django 模板中不起作用

[英]script doesn't work in django template

I tried to follow this tutorial but for some reason, the JS does nothing in my template.我试图按照教程进行操作,但由于某种原因,JS 在我的模板中什么也没做。

I copied each step in it, added is an anchor div with an id of “newItems” and included the infinite scroll script as asked.我复制了其中的每一步,添加了一个 id 为“newItems”的锚点 div,并按要求包含了无限滚动脚本。

my template looks like this:我的模板如下所示:

<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>

my view.py:我的观点.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())

my urls.py:我的 urls.py:

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

Any ideas?有什么想法吗?

The problem appears to be with templates within another template.问题似乎出在另一个模板中的模板上。 I have had the same problem, but couldn't find the solution.我遇到了同样的问题,但找不到解决方案。 Scrips from static are not loaded and inline scripts are not executed if they are positioned in a template of another template.如果静态脚本位于另一个模板的模板中,则不会加载它们,并且不会执行内联脚本。

I have the same error.我有同样的错误。 In console chrome said that: Uncaught TypeError: $ is not a function Here is solution work with me: you must use jQuery, and $ is not used (this is for compatibility with other libraries) If you must use $(document).ready(function(){ , you can actually pass $ into the function call:在控制台 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 ($) { ...

Find more here 在这里找到更多

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

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