Django:视图没有 return HttpResponse 对象。它 return 改为 None

Django: The view didn't return an HttpResponse object. It returned None instead

当我尝试使用 'edit' url.

时,我不断收到此错误

视图 to_do_list_app.views.edit 没有 return HttpResponse 对象。它 return 改为 None。

我的url.py

urlpatterns = [
    path('', views.todo, name='todo'),
    path('delete/<job_id>', views.delete, name='delete'),
    path('cross_off/<job_id>', views.cross_off, name='cross_off'),
    path('uncross/<job_id>', views.uncross, name='uncross'),
    path('edit/<job_id>', views.edit, name='edit'),
]

从django.db导入models.py

class Jobs(models.Model):
    # Relationships
    to_do_owner = models.ForeignKey("auth.User", on_delete=models.SET_NULL, blank=True, null=True)

    # Fields
    item = models.CharField(max_length=200)
    completed = models.BooleanField(default=False)

    def __str__(self):
        return self.item +' | ' +  str(self.completed)

这是我的veiws.py

from django.shortcuts import render, redirect
from .models import Jobs
from .forms import ListForm
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required

def edit(request, job_id):
    if request.method == 'POST':
        item = Jobs.objects.get(pk=job_id)
        form = ListForm(request.POST or None, instance=item)
        if form.is_valid() and request.POST['item'] != '':
            form.save()
            messages.success(request, request.POST['item']+ ' er blevet redigeret i din opgave list')
            return HttpResponseRedirect('todo')
    else:
        item =Jobs.objects.get(pk=job_id)
        context = {'item': item}
        return render(request, 'edit.html', context)

这是我的模板 edith.html

<form class="d-flex" method="POST" action="{% url 'edit' item.id %}">
      {% csrf_token %}
        <input class="form-control me-2" type="search" placeholder="{{  item.item  }}"
               value="{{  item.item  }}" aria-label="Search" name="item">
        <input type="hidden" value="{{  user.username  }}" name="job_owner">
        <input type="hidden" value="{{  item.completed  }}" name="completed">
        <button class="btn btn-outline-secondary" type="submit">Rediger opgave</button>
    </form>

self.check_response(响应,回调)的输出是:

callback
<function edit at 0x7f07ac82b430> callback_args () callback_kwargs
{'job_id': '5'} middleware_method <bound method CsrfViewMiddleware.process_view of <django.middleware.csrf.CsrfViewMiddleware object at 0x7f07ac89b760>> request
<WSGIRequest: POST '/todo/edit/5'> response
None self
<django.core.handlers.wsgi.WSGIHandler object at 0x7f07adbea760> wrapped_callback
<function edit at 0x7f07ac82b430>

你可以这样做:-

def edit(request, job_id):
    if request.method == 'POST':
        item = Jobs.objects.get(pk=job_id)
        form = ListForm(request.POST or None, instance=item)
        if form.is_valid() and request.POST['item'] != '':
            form.save()
            messages.success(request, request.POST['item']+ ' er blevet redigeret i din opgave list')
            return HttpResponseRedirect('todo')
    else:
        item =Jobs.objects.get(pk=job_id)

    context = {'item': item}
    return render(request, 'edit.html', context)

我编辑的内容:- 最后两行的缩进