发了三次 都被移到水节点 不知道为啥,原贴链接: https://www.v2ex.com/t/534900#reply1 大家将就着看,在线等
1
diangdiang OP 求别沉,卡了好久 在线等大佬
|
2
diangdiang OP 目录结构:
- project 目录 + images/views.py + manage.py - project 目录下: python manage.py shell 导入 views.py 里的两个视图函数,其中函数 image_like 为 None, <img src="https://i.loli.net/2019/02/14/5c64faacd9a72.jpg"> - views.py 里两个函数的相关代码 @login_required def image_create(request): if request.method == "POST": form = ImageCreateForm(data=request.POST) if form.is_valid(): cd = form.cleaned_data new_item = form.save(commit=False) # assign current user to the item new_item.user = request.user new_item.save() messages.success(request, "Image added successfully.") # redirect to new created item detail view return redirect(new_item.get_absolute_url()) else: # build form with data provided by the bookmarked via GET: form = ImageCreateForm(data=request.GET) return render(request, 'images/image/create.html', {'section': 'images', 'form': form}) @ajax_required @login_required @require_POST def image_like(request): # import pdb # pdb.set_trace() image_id = request.POST.get('id') action = request.POST.get('action') if image_id and action: try: image = Image.objects.get(id=image_id) if action == 'like': image.users_like.add(request.user) else: image.users_like.remove(request.user) return JsonResponse({"status": "ok"}) except: pass return JsonResponse({"status": "ko"}) |
3
freakxx 2019-02-14 14:15:52 +08:00
|
4
diangdiang OP @freakxx 多谢大佬 ~~ 为什么可以了?我怎么没想到 ,想问是什么原理呢
|
5
freakxx 2019-02-14 14:18:46 +08:00 1
|
6
freakxx 2019-02-14 14:21:06 +08:00 1
@diangdiang
排错思路如下: 问题: reutrn none 不报错,不是函数内部问题, 3 个修饰器,2,3 都是官方出的,只有第 1 个不是,注释掉 1, if 没问题,查看修饰器内部是不是写错。 |
7
diangdiang OP @freakxx 破案了 多谢大佬,ajax 那个装饰写的有问题 🤣
|