执行网址时位于/ post / new /的FieldError:<http:// localhost:8000 / post / new />

时间:2020-07-20 18:53:45

标签: python django

我正在使用Django 3.0.8 在Python版本3.8.2中

我在/ post / new /处收到FieldError 异常类型:FieldError 例外值:为Post指定的未知字段(内容)

PostCreateView是我当前项目博客应用程序下的views.py中基于类的视图

我的blog / views.py在这里:-

from django.shortcuts import render
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.htm', context)

def about(request):
    return render(request, 'blog/about.htm', {'title': 'About'})

    # return HttpResponse('<h1> Blog - About page that we want you to see </h1>')

 
def order(request):
    return render(request, 'blog/order.htm')

class PostListView(ListView):
    model = Post
    template_name = 'blog/home.htm'   # <app>/<model>_<viewtype>.html
    context_object_name = 'posts'
    ordering = ['-date_posted']



class PostDetailView(DetailView):
    model = Post
   

class PostCreateView(CreateView):
    model = Post
    fields = ['title', 'content']

我的Blog / urls.py在这里:

from django.urls import path
from . import views
from .views import (
   PostListView,
   PostDetailView,
   PostCreateView
)

urlpatterns = [
 
   path('about/', views.about, name='blog-about'),
   path('order/', views.order, name='blog-order'),
   path('', PostListView.as_view(), name='blog-home' ), 
   path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail' ),
   path('post/new/', PostCreateView.as_view(), name='post-create' ),

]

我的blog / templates / blog / post_form.html在这里:-

{% extends "blog/base.htm" %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="context-section">
        <form method="POST">
            {% csrf_token %}     <!-- for security perpose  -->
            <fieldset class="form-group">
                <legend class="boder-bottom mb-4">Blog Post</legend>
                {{ form|crispy }}
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-info" type="submit">Post</button>
                
            </div>
        </form>
            
    </div>
{% endblock content %}

代码有什么问题,请帮忙!!! 我是Django的新手,我完全迷失在这里?

非常感谢您的建议!

感谢和惠康

0 个答案:

没有答案