在 django CreateView 中从 fbv 迁移到 cbv 时出现问题,我的模型未在我的模型中提交
Having problem to migrate from fbv to cbv in django CreateView which my model is not submitted in my models
我正在尝试从 FBV 迁移到 CBV,我遇到了一个我完全不知道如何解决的问题,这是在输入提交后将新新闻插入我的 model
,它不会那样做,也不会显示任何内容。甚至 错误 。它只是 return 到 create
页
news/views
from django.shortcuts import render , get_object_or_404
# Create your views here.
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView
from .models import PreNews
class NewsListView(ListView):
model = PreNews
template_name = 'news/main_news.html'
queryset = PreNews.objects.order_by('-date')
class NewsDetailView(DetailView):
model = PreNews
template_name = 'news/sub_news.html'
#slug_url_kwarg = 'slug'
def get_object(self):
slug_ = self.kwargs.get('slug')
return get_object_or_404(PreNews,slug=slug_)
class NewsCreateView(CreateView):
model = PreNews
template_name = 'create/news_create.html'
fields = '__all__'
succes_url = '/news/'
news/models
from django.db import models
from pages.models import LoginFormModel
from django.urls import reverse
# Create your models here.
class PreNews(models.Model):
hardware = 'chw'
software = 'csw'
politics_iran = 'pir'
politics_international = 'pin'
news_choice = [
(hardware,'computer hardware news'),
(software,'computer software news'),
(politics_iran,'iran politics'),
(politics_international,'international politics'),
]
author = models.ForeignKey('pages.LoginFormModel', on_delete = models.CASCADE ,)
title = models.TextField(max_length=100,null=False)
tags = models.TextField(default='sobhan esfandyari,')
choice = models.CharField(max_length=3 , choices=news_choice , default=software)
slug = models.SlugField(unique=True,blank=False,null=False)
date = models.DateTimeField(auto_now_add=True)
main_pic = models.ImageField(upload_to='images/',null=False)
brief = models.TextField(max_length=255,null=False)
article = models.TextField(null=False)
def __str__(self):
return (self.title," ---- ",self.date.year ,self.date.month ,self.date.day ," ---- " ,self.date.hour ,":", self.date.minute," ---- ",self.author)
def get_absolute_url(self):
return reverse('sub_news_url', kwargs = { 'slug' : self.slug ,'year' : self.date.year , 'month' : self.date.month , 'day' : self.date.day })
news/urls
from django.urls import path
from .views import NewsListView , NewsDetailView , NewsCreateView
#app_name = 'news' ##
urlpatterns = [
path('', NewsListView.as_view(), name='news'),
path('create/', NewsCreateView.as_view(), name='news_create'),
path('<int:year>/<int:month>/<int:day>/<slug:slug>/', NewsDetailView.as_view(), name='sub_news_url'),
]
模板
<!DOCTYPE html>
<html lang=en-US>
<head>
<title>
Sobhan Esfandyari Personal Website | DAWSHESSI
</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width , initial-scale=1.0">
<meta name="description" content="Contact Sobhan Esfandyari">
<meta name="author" content="sobhanessi,Sobhan Esfandyari,Sobhan Esfandiari">
<meta name="keywords" content="dawshessi, sobhanessi, sobhanesfandiari, Sobhan Esfandiari,Sobhan Esfandyari,sobhan esfandiari email,sobhanessi contact,sobhan esfandyari email,contact dawshessi,contact sobhanessi">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<![endif]-->
{% load static %}
<link href="{% static 'pages/newscreate.css' %}" rel="stylesheet" type="text/css" />
<link href="{% static 'pages/general.css' %}" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body id="indexbody">
{% include 'navbar.html' %}
<br><br>
<form style="width:auto%;" method='POST' action='' > {% csrf_token %}
<div class="container">
<div class="input-group" style="width:100%;">
<span class="input-group-addon" style="width:20%;">Author</span>
{{ form.author }}
</div><br>
<div class="input-group" style="width:100%;">
<span class="input-group-addon" style="width:20%;">title</span>
{{ form.title }}
</div>
<br>
<div class="input-group" style="width:100%;">
<span class="input-group-addon" style="width:20%;">tags</span>
{{ form.tags }}
</div>
<br>
<div class="input-group" style="width:100%;">
<span class="input-group-addon" style="width:20%;">choice</span>
{{ form.choice }}
</div><br>
<div class="input-group" style="width:100%;">
<span class="input-group-addon" style="width:20%;">slug</span>
{{ form.slug }}
</div>
<br>
<div class="input-group" style="width:100%;">
<span class="input-group-addon" style="width:20%;">main pic</span>
{{ form.main_pic }}
</div><br>
<div class="input-group" style="width:100%;">
<span class="input-group-addon" style="width:20%;">brief</span>
{{ form.brief }}
</div>
<br>
<div class="input-group" style="width:100%;">
<span class="input-group-addon" style="width:20%;">article</span>
{{ form.article }}
</div>
<br>
<input type="submit" class="btn btn-primary" value="submit">
</div>
</form>
{% include 'footer.html' %}
</body>
</html>
我想在我的 models
中提交新的 news
并重定向到主要新闻页面,甚至是我刚刚创建的新闻。
问题是 ImageField
,模板中需要 enctype="multipart/form-data"
我正在尝试从 FBV 迁移到 CBV,我遇到了一个我完全不知道如何解决的问题,这是在输入提交后将新新闻插入我的 model
,它不会那样做,也不会显示任何内容。甚至 错误 。它只是 return 到 create
页
news/views
from django.shortcuts import render , get_object_or_404
# Create your views here.
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView
from .models import PreNews
class NewsListView(ListView):
model = PreNews
template_name = 'news/main_news.html'
queryset = PreNews.objects.order_by('-date')
class NewsDetailView(DetailView):
model = PreNews
template_name = 'news/sub_news.html'
#slug_url_kwarg = 'slug'
def get_object(self):
slug_ = self.kwargs.get('slug')
return get_object_or_404(PreNews,slug=slug_)
class NewsCreateView(CreateView):
model = PreNews
template_name = 'create/news_create.html'
fields = '__all__'
succes_url = '/news/'
news/models
from django.db import models
from pages.models import LoginFormModel
from django.urls import reverse
# Create your models here.
class PreNews(models.Model):
hardware = 'chw'
software = 'csw'
politics_iran = 'pir'
politics_international = 'pin'
news_choice = [
(hardware,'computer hardware news'),
(software,'computer software news'),
(politics_iran,'iran politics'),
(politics_international,'international politics'),
]
author = models.ForeignKey('pages.LoginFormModel', on_delete = models.CASCADE ,)
title = models.TextField(max_length=100,null=False)
tags = models.TextField(default='sobhan esfandyari,')
choice = models.CharField(max_length=3 , choices=news_choice , default=software)
slug = models.SlugField(unique=True,blank=False,null=False)
date = models.DateTimeField(auto_now_add=True)
main_pic = models.ImageField(upload_to='images/',null=False)
brief = models.TextField(max_length=255,null=False)
article = models.TextField(null=False)
def __str__(self):
return (self.title," ---- ",self.date.year ,self.date.month ,self.date.day ," ---- " ,self.date.hour ,":", self.date.minute," ---- ",self.author)
def get_absolute_url(self):
return reverse('sub_news_url', kwargs = { 'slug' : self.slug ,'year' : self.date.year , 'month' : self.date.month , 'day' : self.date.day })
news/urls
from django.urls import path
from .views import NewsListView , NewsDetailView , NewsCreateView
#app_name = 'news' ##
urlpatterns = [
path('', NewsListView.as_view(), name='news'),
path('create/', NewsCreateView.as_view(), name='news_create'),
path('<int:year>/<int:month>/<int:day>/<slug:slug>/', NewsDetailView.as_view(), name='sub_news_url'),
]
模板
<!DOCTYPE html>
<html lang=en-US>
<head>
<title>
Sobhan Esfandyari Personal Website | DAWSHESSI
</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width , initial-scale=1.0">
<meta name="description" content="Contact Sobhan Esfandyari">
<meta name="author" content="sobhanessi,Sobhan Esfandyari,Sobhan Esfandiari">
<meta name="keywords" content="dawshessi, sobhanessi, sobhanesfandiari, Sobhan Esfandiari,Sobhan Esfandyari,sobhan esfandiari email,sobhanessi contact,sobhan esfandyari email,contact dawshessi,contact sobhanessi">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<![endif]-->
{% load static %}
<link href="{% static 'pages/newscreate.css' %}" rel="stylesheet" type="text/css" />
<link href="{% static 'pages/general.css' %}" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body id="indexbody">
{% include 'navbar.html' %}
<br><br>
<form style="width:auto%;" method='POST' action='' > {% csrf_token %}
<div class="container">
<div class="input-group" style="width:100%;">
<span class="input-group-addon" style="width:20%;">Author</span>
{{ form.author }}
</div><br>
<div class="input-group" style="width:100%;">
<span class="input-group-addon" style="width:20%;">title</span>
{{ form.title }}
</div>
<br>
<div class="input-group" style="width:100%;">
<span class="input-group-addon" style="width:20%;">tags</span>
{{ form.tags }}
</div>
<br>
<div class="input-group" style="width:100%;">
<span class="input-group-addon" style="width:20%;">choice</span>
{{ form.choice }}
</div><br>
<div class="input-group" style="width:100%;">
<span class="input-group-addon" style="width:20%;">slug</span>
{{ form.slug }}
</div>
<br>
<div class="input-group" style="width:100%;">
<span class="input-group-addon" style="width:20%;">main pic</span>
{{ form.main_pic }}
</div><br>
<div class="input-group" style="width:100%;">
<span class="input-group-addon" style="width:20%;">brief</span>
{{ form.brief }}
</div>
<br>
<div class="input-group" style="width:100%;">
<span class="input-group-addon" style="width:20%;">article</span>
{{ form.article }}
</div>
<br>
<input type="submit" class="btn btn-primary" value="submit">
</div>
</form>
{% include 'footer.html' %}
</body>
</html>
我想在我的 models
中提交新的 news
并重定向到主要新闻页面,甚至是我刚刚创建的新闻。
问题是 ImageField
,模板中需要 enctype="multipart/form-data"