Editpost功能不起作用。 “ if request.method =='POST':”问题

时间:2019-01-30 15:49:10

标签: django

问题似乎出在“ if request.method =='POST':”这一行。任何帮助表示赞赏。

回溯

[30/Jan/2019 23:44:22] "GET /testimonypost/ HTTP/1.1" 200 8031
Not Found: /testimonypost/.jpg
[30/Jan/2019 23:44:22] "GET /testimonypost/.jpg HTTP/1.1" 404 17479

boo
5
boo
a

[30/Jan/2019 23:44:25] "GET /37/testimony/edit HTTP/1.1" 200 5320
Not Found: /37/testimony/.jpg
[30/Jan/2019 23:44:25] "GET /37/testimony/.jpg HTTP/1.1" 404 17476
[30/Jan/2019 23:44:28] "POST /37/testimony/ HTTP/1.1" 200 4261
Not Found: /37/testimony/.jpg
[30/Jan/2019 23:44:28] "GET /37/testimony/.jpg HTTP/1.1" 404 17476

模板

{%extends 'base_form.html'%}
{%block content%}

<form class="form-group" action="." method="POST" enctype="multipart/form-data">
{%csrf_token%}
{%for field in form %}



    <div class="col-sm-offset-2 col-sm-10">
      <span class="text-danger small">{{field.errors}}</span>
    </div>
      <label class="control-label col-sm-2"{{field.label_tag}}</label>
    <div class="col-sm-10">{{field}}</div>



{%endfor%}
  <button type="submit" class="btn btn-success">Submit</button>
</form>
{%endblock content%}

views.py

@login_required
def TestimonyEdit(request, id=None):
    instance=get_object_or_404(Testimony, id=id)
    print(request.user)
    print('5')
    print(instance.user)
    if instance.user == request.user:
        form = TestimonyForm(request.POST or None, request.FILES or None, instance=instance)
        print('a')
        return render(request, 'variablized_form.html', {"form": form})
        if request.method=='POST':
            form = TestimonyForm(request.POST or None, request.FILES or None, instance=instance)
            if form.is_valid():
                print('3')
                instance=form.save(commit=False)
                instance.save()
                print('2')
                context={
                    "testimony":testimony
                }

                return render(request, 'testimony_post.html', context)
    else:
        return HttpResponse('You are unauthorized to edit this post')

1 个答案:

答案 0 :(得分:0)

1. if instance.user == request.user:
2.    form = TestimonyForm(request.POST or None, request.FILES or None, instance=instance)
3.     print('a')
4.     return render(request, 'variablized_form.html', {"form": form})
5.     if request.method=='POST':
6.         form = TestimonyForm(request.POST or None, request.FILES or None, instance=instance)

查看此代码块。 在第1行中,它检查instance.user == request.user是否为 True ,然后转到第2行 然后在控制台/终端机中的第3行中打印其a。 然后转到第4行并返回模板。 因此,无论如何都无法转到第5行

因此,如果要到达第5行,则需要删除第4行,然后找到任何错误来解决此问题。

更新:

print('a')
# here you remove previous line
if request.method=='POST':
        form = TestimonyForm(request.POST or None, request.FILES or None, instance=instance)
        if form.is_valid():
            print('3')
            instance=form.save(commit=False)
            instance.save()
            print('2')
            context={
                "testimony":testimony
            }

            return render(request, 'testimony_post.html', context)
return render(request, 'variablized_form.html', {"form": form})