python - 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
解决方案
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
推荐阅读
- spring-boot - 在 Spring Boot 中使用嵌入式 Tomcat 时如何配置 HTTP 连接器属性
- python - 无法安装一些 requirements.txt python 依赖项
- flutter - Flutter 中的路由
- python - 如何在云端部署深度学习模型?
- android-source - Android 模拟器在成功构建 aosp 后无法启动
- c# - 在 Unity 场景中激活暂停菜单窗口时如何锁定我的相机视图?
- node.js - Angular Universal 服务器快速部署到生产错误
- python - 如何使用 BeautifulSoup 从属于多个类的网站中抓取元素
- c# - 如何使用 EPPlus 以逗号分隔小数以生成 Excel
- proxy - 在 Windows 上使用 Mitm 代理