首页 > 解决方案 > django CreateView forms not loading in html

问题描述

I have found a lots of similar problems on this topics. None of them has proper solution. CreateView form from models not loading.

models.py

from django.db import models
from django.contrib.auth.models import User
# Create your models here.


class Blog(models.Model):
    author: models.ForeignKey(
        User, on_delete=models.CASCADE, related_name='post_author')
    blog_title: models.CharField(max_length=264, verbose_name="Title")
    slug: models.SlugField(max_length=264, unique=True)
    blog_content: models.TextField(verbose_name="What is on your mind?")
    blog_image: models.ImageField(
        upload_to='blog_images', verbose_name='images')
    publish_date: models.DateField(auto_now_add=True)
    updated_date: models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.blog_title


views.py

from django.shortcuts import render, HttpResponseRedirect
from django.views.generic import CreateView, DeleteView, UpdateView, ListView, DetailView, View, TemplateView
from App_Blog.models import Blog, Comment, Likes
from django.urls import reverse, reverse_lazy
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin

# Create your views here.


def blog_list(request):
    return render(request, 'App_Blog/blog_list.html', context={})


class CreateBlog(LoginRequiredMixin, CreateView):
    model = Blog
    template_name = 'App_Blog/create_blog.html'
    fields = '__all__'

urls.py

from django.urls import path
from App_Blog import views

app_name = 'App_Blog'

urlpatterns = [
    path('', views.blog_list, name='blog_list'),
    path('write/', views.CreateBlog.as_view(), name='create_blog'),
]

I am trying to load the form in this html page: create_blog.html

{% extends 'base.html' %}
{% load crispy_forms_tags %}

{% block title_block %}
Write a Blog
{% endblock title_block %}


{% block body_block %}
<h2>Start Writing:</h2>
<form method="POST" enctype="multipart/form-data">
    {{ form | crispy }}
    {% csrf_token %}
    <button type="button" class="btn btn-success btn-sm">Publish</button>
</form>
{% endblock body_block %}

After reloading the I get only base.html and button except form. enter image description here

标签: pythonhtmldjangodjango-modelsdjango-forms

解决方案


You have not defined your model fields correctly, you need to use = and not : to define the fields

class Blog(models.Model):
    author = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name='post_author')
    blog_title = models.CharField(max_length=264, verbose_name="Title")
    slug = models.SlugField(max_length=264, unique=True)
    blog_content = models.TextField(verbose_name="What is on your mind?")
    blog_image = models.ImageField(
        upload_to='blog_images', verbose_name='images')
    publish_date = models.DateField(auto_now_add=True)
    updated_date = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.blog_title

推荐阅读