如果request.method ==“ POST”:不适用于编辑功能

时间:2019-02-04 07:46:30

标签: django

我可以在填充所有字段的情况下加载实例,但是当我单击“提交”时,所做的修改不会保存。实际上,但是在我的函数中添加了打印语句,该代码不会加载到if request.method ==“ POST” :(请参阅追溯)之外。

回溯

[04/Feb/2019 15:38:30] "GET /testimonypost/ HTTP/1.1" 200 8081 Not Found: /testimonypost/.jpg
[04/Feb/2019 15:38:30] "GET /testimonypost/.jpg HTTP/1.1" 404 17479 a [04/Feb/2019 15:38:35] "GET /40/testimony/edit HTTP/1.1" 200 5321 Not Found: /40/testimony/.jpg
[04/Feb/2019 15:38:35] "GET /40/testimony/.jpg HTTP/1.1" 404 17476
[04/Feb/2019 15:38:56] "POST /40/testimony/ HTTP/1.1" 200 4224 Not Found: /40/testimony/.jpg
[04/Feb/2019 15:38:56] "GET /40/testimony/.jpg HTTP/1.1" 404 17476

模板

链接到表单

<a href="{% url 'testimonyedit' objects.id %}">edit</a>

模板表格

<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>

urls.py

urlpatterns = [
    path('<int:id>/testimony/edit', views.TestimonyUpdate, name='testimonyedit'),
]

views.py

@login_required
def TestimonyUpdate(request, id=None):
    instance=get_object_or_404(Testimony, id=id, user=request.user)
    if instance.user == request.user:
        form = TestimonyForm(request.POST or None, request.FILES or None, instance=instance)
        print('a')
        if request.method == "POST":
            print('4')
            if form.is_valid():
                form = TestimonyForm(request.POST or None, request.FILES or None)
                print('3')
                instance=form.save(commit=False)
                instance.save()
                context={
                    "instance":instance
                }

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

    else:
        return HttpResponse('You are unauthorized to edit this post')

1 个答案:

答案 0 :(得分:0)

我认为您做出了一些错误的假设,并且您的代码在更新实例方面存在一些缺陷。让我们一一解决:

首先,当您点击“提交”按钮时,它会发出post请求。因此,request.method == "POST"之后的行将被执行。

第二,当表单验证失败时,或者您正在发出get请求(只是用url击中地址),它将不会在request.method == "POST"之后执行代码。

第三,实例未更新,因为您需要在TestimonyForm代码块内的if request.method=='POST'中传递实例,如下所示:

form = TestimonyForm(request.POST or None, request.FILES or None)
if form.is_valid():
    instance=form.save(commit=False)
    instance.save()

最后,我将像这样重构您的代码:

def update_testimony(request, id=None):  # used snake case for method name. Please see PEP8 style guide regarding naming. 
    instance=get_object_or_404(Testimony, id=id, user=request.user). # getting  Testimony object
    form = TestimonyForm(request.POST or None, request.FILES or None, instance=instance). # initiating TestimonyForm form
    if request.method == "POST":  # checking request type
        if form.is_valid():     # checking if form is valid
           instance=form.save()  # saving the form to update the instance
           context={
              "instance":instance
           }
           return render(request, 'details.html', context)

     return render(request, 'variablized_form.html', {"form": form}). # returning form for get request
相关问题