简体   繁体   English

加载10个项目的AJAX调用无法按预期工作

[英]AJAX call to load 10 more items doesn't work as expected

I'm trying to load 10 items each time the load more button is clicked. 每次点击加载更多按钮时,我正在尝试加载10个项目。 I initially send the first 10 items and use dajaxice to load more 10 items each time the button is pressed. 我最初发送前10个项目并使用dajaxice每次按下按钮时加载更多10个项目。

Here is my view 这是我的看法

def dj(request, slug):
    dj = DJ.objects.get(slug=slug)
    dj_song_list = Song.objects.filter(artist=dj, duplicate=False).order_by('-votes', '-release_date')[:10]
    return render(request, 'hunt/dj.html', {'dj_song_list': dj_song_list, 'slug': slug, 'dj': dj})

Here is my javascript 这是我的javascript

<script type="text/javascript">
    function load() {
        Dajaxice.hunt.load_more(Dajax.process, {'items': $("#dj_song_list").find('div').length - 1, 'dj_name': $('.dj_name').text().trim()})
    }
</script>

Here is my ajax.py 这是我的ajax.py

@dajaxice_register
def load_more(request, items, dj_name=None):
    if dj_name:
        dj = DJ.objects.get(name=dj_name)
        dj_song_list = Song.objects.filter(artist=dj, duplicate=False).order_by('-votes', '-release_date')[:items+10]
        render = render_to_string('hunt/dj.html', {'dj_song_list': dj_song_list, 'slug': dj.slug, 'dj': dj.name})
        dajax = Dajax()
        dajax.assign('#edmhunters_body','innerHTML',render)
        return dajax.json()

This code loads 10 more songs but the DJ objects is not passed correctly. 此代码加载了10首歌曲,但DJ对象未正确传递。 I get the following error 我收到以下错误

DoesNotExist: DJ matching query does not exist.

On investigating I found that slug was printing fine in the ajax call but in the view it was something like hardwell/hunt/img/.jpg . 在调查中我发现slug在ajax调用中打印正常,但在视图中它就像hardwell/hunt/img/.jpg It probably has something to do with how the slug is retrieved. 它可能与如何回收slug有关。 Here is my HTML where the slug is retrieved. 这是我检索slug的HTML。

<a href="{% url 'dj' slug=dj.slug %}"><img src="{{ STATIC_URL}}hunt/img/{{ dj.rank }}.jpg"/></a>

Why is the slug appending the string at the end? 为什么slug会在最后追加字符串?

I even added a code in the view to trim the extra string of the slug but it still doesn't work as expected. 我甚至在视图中添加了一个代码来修剪slug的额外字符串,但它仍然没有按预期工作。 It doesn't throw the DoesNotExist error but the DJ object is still not passed. 它不会抛出DoesNotExist错误,但仍然没有传递DJ对象。

if slug.endswith('/hunt/img/.jpg'):
        slug = slug[:-len('/hunt/img/.jpg')]

Here is the view initially loaded 这是最初加载的视图

这是最初加载的视图

Here is the view after the AJAX call 这是AJAX调用之后的视图

这是AJAX调用之后的视图

What am I doing wrong? 我究竟做错了什么? Is there any easier solution to this? 有没有更简单的解决方案?

In one method you are using the slug to find the Dj and in the load_more you are using the name. 在一种方法中,您使用slug来查找Dj,而在load_more您使用的是名称。 You should use dj slug in both and I would use Django pagination to get more items instead of passing the number of found divs. 你应该在两者中使用dj slug我会使用Django分页来获取更多的项目而不是传递找到的div的数量。

You can just increment the page parameter send to the load more. 您可以将页面参数增量发送到负载更多。

@dajaxice_register
def load_more(request, page, dj_slug=None):
    if dj_slug:
        try:
            dj = DJ.objects.get(slug=dj_slug)
        except DJ.DoesNotExist:
            return None
        queryset = Song.objects.filter(artist=dj, duplicate=False).order_by('-votes', '-release_date')
        paginator = Paginator(queryset)

        render = render_to_string('hunt/dj.html', {
            'dj_song_page': paginator.page(page), 
            'slug': dj.slug, 
            'dj': dj.name
        })
        dajax = Dajax()
        dajax.assign('#edmhunters_body','innerHTML', render)
        return dajax.json()

The dj_song_page is a page returned by the paginator. dj_song_page是paginator返回的页面。 It will contain the items for that page. 它将包含该页面的项目。 Read more on django docs about pagination here: https://docs.djangoproject.com/en/1.6/topics/pagination/ 阅读有关分页的django文档的更多信息: https//docs.djangoproject.com/en/1.6/topics/pagination/

The main problem I think is that you have to use the slug not the name. 我认为的主要问题是你必须使用slug而不是名字。 The queryset must be the same as the one in the dj view. 查询集必须与dj视图中的查询集相同。

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

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