Python Ajaxing刷新一个div,一旦添加了一个;项目「;
我已获得成功将我的项目添加到购物车的视图。但是,我只想让它在保持原始页面的同时刷新“#cart”div(某种程度上)。换句话说,项目将添加到购物车中,但仅在手动刷新时显示。我只是想让它看起来光滑,像ajax-y Views.py:Python Ajaxing刷新一个div,一旦添加了一个;项目「;,python,ajax,django,jquery,Python,Ajax,Django,Jquery,我已获得成功将我的项目添加到购物车的视图。但是,我只想让它在保持原始页面的同时刷新“#cart”div(某种程度上)。换句话说,项目将添加到购物车中,但仅在手动刷新时显示。我只是想让它看起来光滑,像ajax-y Views.py: @login_required def add(request, profile): if request.method != 'POST': return HttpResponseNotAllowed('POST') try:
@login_required
def add(request, profile):
if request.method != 'POST':
return HttpResponseNotAllowed('POST')
try:
item = Item.objects.get(slug=slugify(request.POST['itemname']))
except ObjectDoesNotExist:
item = Item(name=request.POST['itemname'])
item.save()
profile = Profile.objects.get(user=request.user)
profile.items.add(item)
response = simplejson.dumps(
{"status": "Successfully added."}
)
return HttpResponse (response, mimetype='application/json')
模板:
<form id="addItemForm" class="form-style" method="POST" action="/item/add/{{ profile }}/">
{% csrf_token %}
<input id="item-input" class="input-text inline required" name="itemname" type="text" />
<button class="blue" type="submit" id="additem">
Add Item
</button>
</form>
控制台错误:
Uncaught TypeError: Cannot call method 'toLowerCase' of undefined
(我为格式问题道歉)
我是ajax初学者。很明显我的剧本有问题。我只是不知道接下来该怎么办
提前感谢您的帮助。我在您的代码中看不出您的意思。但这应该重新加载#cart元素。。尽管可能有更好的方法,不必再次下载整个页面,只需下载一个元素上的内容
$('#cart').load('ajax/test.html #cart');
这也不管用。。。查看控制台,我得到了以下错误:
uncaughttypeerror:无法调用未定义的方法“toLowerCase”
。。但我相信这并不是它的全部错误。在一个地方你把它当作一个类。在另一个地方,你用id#item selection
或者那些相同的元素来调用它?删除了“$('.item selection').html(数据);”全部加在一起。仍然有效,只是没有成功刷新div。请确保您在load()
中使用的url是您购物车页面的url,而不是更新购物车的url。例如:index.php?show=cart
,或者您如何访问购物车
$('#cart').load('ajax/test.html #cart');