Python 我正在django中使用CreateView,但出现错误-查看blog.views.PostCreateView没有';t返回HttpResponse对象。它没有返回任何结果
我有一个post模型,其中一个用户与用于身份验证的内置用户模型具有OneToMany关系 我的URL.pyPython 我正在django中使用CreateView,但出现错误-查看blog.views.PostCreateView没有';t返回HttpResponse对象。它没有返回任何结果,python,django,Python,Django,我有一个post模型,其中一个用户与用于身份验证的内置用户模型具有OneToMany关系 我的URL.py from django.contrib import admin from django.urls import path, include # from views import PostView from . import views urlpatterns = [ path('', views.PostView.as_view(), name='blogHome'),
from django.contrib import admin
from django.urls import path, include
# from views import PostView
from . import views
urlpatterns = [
path('', views.PostView.as_view(), name='blogHome'),
path('post/<int:pk>/', views.PostDetailView.as_view(), name='post-detail'),
path('post/new/', views.PostCreateView.as_view(), name='post-create'),
path('about/', views.about, name='about')
]
from django.shortcuts import render
from django.http import HttpResponse
from .models import Post
from django.views.generic import (
ListView,
DetailView,
CreateView
)
# Create your views here.
def home(request):
context = {
'posts': Post.objects.all()
}
return render(request, 'blog/home.html', context)
def about(request):
return render(request, 'blog/about.html')
class PostView(ListView):
model = Post
template_name = 'blog/home.html'
context_object_name = 'posts'
ordering = ['-date_published']
class PostDetailView(DetailView):
model = Post
class PostCreateView(CreateView):
model = Post
fields = ['title', 'body']
#to add author before validation
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
后期模型
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
# Create your models here.
class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
body = models.TextField()
date_published = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.title
{% extends 'blog/layout.html' %}
{% load crispy_forms_tags %}
{% block body %}
<div class="content-section p-20">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group p-30">
<legend class="border-bottom mb-4">Create new post</legend>
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Create</button>
</div>
</form>
</div>
{% endblock %}
我使用post_form.html作为模板名
post_form.html
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
# Create your models here.
class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
body = models.TextField()
date_published = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.title
{% extends 'blog/layout.html' %}
{% load crispy_forms_tags %}
{% block body %}
<div class="content-section p-20">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group p-30">
<legend class="border-bottom mb-4">Create new post</legend>
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Create</button>
</div>
</form>
</div>
{% endblock %}
{%extends'blog/layout.html%}
{%load crispy_forms_tags%}
{%block body%}
{%csrf_令牌%}
创建新帖子
{{form | crispy}}
创造
{%endblock%}
我是Django的初学者,请告诉我是否需要更多的东西来解决这个问题。这也是为什么这种类型的错误只出现在Createview中而不出现在其他视图中的原因显然是
super()的返回。form\u valid(form)
是None
并且不是有效的响应。我对Django中的这种设计模式不太了解,但看到其他方法,这个视图似乎被某个返回有效响应的方法修饰过。因此,您不应该在实现中返回某些内容。因此,放下返回键
,然后再次测试