代码之家  ›  专栏  ›  技术社区  ›  Ali Akhtari

view newsletter.views.control新闻稿编辑未返回HttpResponse对象。但没有返回

  •  1
  • Ali Akhtari  · 技术社区  · 6 年前

    这是我的url.py:

    from django.urls import path
    from newsletter.views import control_newsletter, control_newsletter_list, control_newsletter_detail, control_newsletter_edit
    
    urlpatterns = [
        path('newsletter/', control_newsletter, name='control_newsletter'),
        path('newsletter_list/', control_newsletter_list, name='control_newsletter_list'),
        path('newsletter_detail/<int:pk>/', control_newsletter_detail, name='control_newsletter_detail'),
        path('newsletter_edit/<int:pk>/', control_newsletter_edit, name='control_newsletter_edit'),]
    

    这是我的观点.py:

    def control_newsletter_edit(request, pk):
        newsletter = get_object_or_404(Newsletter, pk=pk)
        if request.method == 'POST':
    
            form = NewsletterCreationForm(request.POST, instance=Newsletter)
    
            if form.is_valid():
                newsletter = form.save()
    
                if newsletter.status == 'Published':
                    subject = newsletter.subject
                    body = newsletter.body
                    from_email = global_settings.EMAIL_HOST_USER
                    for email in newsletter.email.all():
                        send_mail(subject=subject, from_email=from_email, recipient_list=[email], message=body,
                                  fail_silently=True)
                    messages.success(request, 'Your Changes Write Successfully.',
                                     'alert alert-success alert-dismissible')
                else:
                    messages.warning(request, 'SomeThing Went Wrong..',
                                     'alert alert-warning alert-dismissible')
                return redirect('control_newsletter_detail', pk=newsletter.pk)
    
            else:
                form = NewsletterCreationForm(instance=newsletter)
    
                context = {
                    'form': form,
                }
    
                return render(request, 'control_panel/control_newsletter.html', context)
    

    当我试图接近 newsletter_edit/1/ 从这里开始,在我的teplate代码中:

    <div class="col-sm-8">
                <div class="col-sm-2">
                    <a href="{% url 'control_newsletter_edit' pk=newsletter.pk %}">
                        <button class="btn-success">Edit</button>
                    </a>
                </div>
    

    我面对这个错误:

    ValueError at /panel/newsletter_edit/1/
    The view newsletter.views.control_newsletter_edit didn't return an HttpResponse object. It returned None instead.
    

    我一遍又一遍地检查我的url.py和views.py,但我无法找出我的问题所在。 有人知道我为什么要面对这个错误吗? 另外,我很抱歉在我的问题上写了错误。 如果你需要我的全部观点,请告诉我。

    2 回复  |  直到 6 年前
        1
  •  3
  •   Mauricio Cortazar    6 年前

    你的问题在这里 if request.method == 'POST': 如您所见,只有在请求方法为post时才返回模板,请确保返回if条件之外的内容

        2
  •  0
  •   JPG    6 年前

    你没有提到如果一个请求 HTTP POST 来。它可以通过使用一个简单的 else 如下图所示,

    
    def control_newsletter_edit(request, pk):
        newsletter = get_object_or_404(Newsletter, pk=pk)
        if request.method == 'POST':
    
            form = NewsletterCreationForm(request.POST, instance=Newsletter)
    
            if form.is_valid():
                newsletter = form.save()
    
                if newsletter.status == 'Published':
                    subject = newsletter.subject
                    body = newsletter.body
                    from_email = global_settings.EMAIL_HOST_USER
                    for email in newsletter.email.all():
                        send_mail(subject=subject, from_email=from_email, recipient_list=[email], message=body,
                                  fail_silently=True)
                    messages.success(request, 'Your Changes Write Successfully.',
                                     'alert alert-success alert-dismissible')
                else:
                    messages.warning(request, 'SomeThing Went Wrong..',
                                     'alert alert-warning alert-dismissible')
                return redirect('control_newsletter_detail', pk=newsletter.pk)
    
            else:
                form = NewsletterCreationForm(instance=newsletter)
    
                context = {
                    'form': form,
                }
    
                return render(request, 'control_panel/control_newsletter.html', context)
        else:
            form = NewsletterCreationForm()  # if method not HTTP POST
        return render(request, 'control_panel/control_newsletter.html', {"form":form})