当我试图通过AJAX进行Django查询时,哪里出错了?

2024-09-30 01:37:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个页面,基本上是一个店面出售的项目。我在导航栏有一些过滤器来过滤产品。当我单击一个过滤器时,我试图通过ajax加载与该过滤器相关的产品(不刷新页面)。你知道吗

我一直在遵循一个教程,但我不能让它工作。当前,当我单击过滤器时,我实际上在flickr页面上看到了一秒钟的产品,但是相同的产品仍然留在页面上。我知道我在查询本身中没有犯错误,因为我尝试过不同的查询,如按日期重新排序、按名称排序等。你知道吗

不管怎样,下面是我的代码,如果有用的话,下面是我尝试遵循的教程http://westiseast.co.uk/blog/django-ajax-update-queryset-introduction/

我在chrome开发控制台中也遇到了这个错误:

GET http://127.0.0.1:8000/storefront/?xhr=true&filter=recent 500 (INTERNAL SERVER ERROR

你知道吗视图.PY你知道吗

def storefront(request):
    latest_entries = Entry.objects.order_by('-pub_date')[:16]
    context = {'latest_entries': latest_entries}

    if request.is_ajax():
        if request.GET.get('filter') == 'recent':
            new_list = []
            latest_entries = Entry.objects.filter(entrytype=3)
            new_list.append(render_to_string('storefrontload.html', {'latest_entries': latest_entries}))
            json =  simplejson.dumps(new_list, cls=DjangoJSONEncoder)
            return HttpResponse(json, mimetype='application/json')
    return render(request, 'storefront.html', context)

你知道吗网址.PY你知道吗

urlpatterns = patterns('',
    url(r"^$", home),
    url(r"^storefront/", storefront))

你知道吗店面.HTML你知道吗

<a href="" id=most_recent>Show Recent</a>

{% include "storefrontload.html" %}

<script>
function filter(type) {
$.getJSON("/storefront/", {xhr: "true", filter: type},
  function(data) {
    $('.container').children().remove();
    $(data).appendTo('.container');
  });
}

$("#most_recent").click(function () { 
  filter("recent");
  $('#most_recent').attr('style', 'font-weight: bold').css('color', '#000'); 
});
return false;
</script>

你知道吗STOREFRONTLOAD.HTML你知道吗

<div class="bodydiv">
    <div class="container">
    {% for i in latest_entries %}
      <div class="grid_4">
        <div class="imgcontainer">
          <img src="{{ i.item_picture.url }}" alt="" />
          <div class="overlayname">{{ i.headline }}</div>
          <div class="overlayprice">{{ i.price1 }}</div>
          </div>
        <div class="textcontainer">
        <p>{{ i.body_text }}</p>
      </div>
      <div class="sellerdiv">
        <div class="sellerpic"><a href="/accounts/{{ i.author }}"><img src="{{ i.author.get_profile.get_mugshot_url }}"></a></div> 
        <div class="sellerinfo">Sold by <b>{{ i.author }}</b></div>
        <div class="hearts"><img src="/static/img/stars.png"></div>
        <div class="reviewnum"></div>
        <div class="posteddate">Posted {{ i.pub_date|timesince|upto:',' }} ago</div>
      </div>
    </div>
    {% endfor %}

Tags: divurl过滤器imgget产品requestajax
1条回答
网友
1楼 · 发布于 2024-09-30 01:37:55

您还没有停止触发链接的默认操作:您的JS工作并更新页面,但是浏览器跟随该链接,您返回到原来的状态。确保从click函数的末尾return false,或者让它接受event参数并执行event.preventDefault()。你知道吗

相关问题 更多 >

    热门问题