python - 部署到heroku后Django应用程序不工作
问题描述
该应用程序是一个博客帖子应用程序,在本地运行良好,但在我将其部署到 Heroku 后,该应用程序无法运行。我做了一个多星期试图通过视图和模型来修复它,但没有成功。
视图.py
from django.shortcuts import render,get_object_or_404, get_list_or_404
from posts.models import Post, Category
# Create your views here.
import datetime
def index(request):
now = datetime.datetime.now()
big_slider_list = Post.objects.filter(category__name='Big_Slider')
news_box_list = Post.objects.filter(category__name='People')
cote_divoire_list = Post.objects.filter(category__name="Côte d'Ivoire").exclude(position=1)
block1_big = Post.objects.filter(position=1)
culture_list = Post.objects.filter(category__name="Culture").exclude(position=2)
block2_big = Post.objects.filter(position=2)
sport_list = Post.objects.filter(category__name="Sports").exclude(position=3)
block3_big = Post.objects.filter(position=3)
infrast_et_devel_list = Post.objects.filter(category__name="Infrastructures Et Dévelopements")
context = {
'now': now,
'big_slider_list': big_slider_list[:10],
'news_box_list': news_box_list[:4],
'cote_divoire_list': cote_divoire_list[:4],
'block1_big': block1_big[0],
'culture_list': culture_list[:3],
'block2_big': block2_big[0],
'sport_list': sport_list[:3],
'block3_big': block3_big[0],
'infrast_et_devel_list': infrast_et_devel_list[:3],
}
return render(request, 'home/index.html', context)
模型.py:
from django.db import models
from django.contrib.auth import get_user_model
from django.urls import reverse
# Create your models here.
User = get_user_model()
STATUS = (
(0, "Draft"),
(1, "Publish")
)
POSITIONS = [
(0, ' '),
(1, 'BIG_BLOCK_1'),
(2, 'BIG_BLOCK_2'),
(3, 'BIG_BLOCK_3'),
(4, 'BIG_BLOCK_4'),
]
class Author(models.Model):
name = models.CharField(max_length=50)
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_picture = models.ImageField(null=True, blank=True)
email = models.EmailField(unique=True)
active = models.BooleanField(default=False)
created_on = models.DateTimeField(auto_now_add=True)
last_logged_in = models.DateTimeField(auto_now=True)
def __str__(self):
return self.user.username
class Category(models.Model):
country = models.CharField(max_length=200, blank=True)
name = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, unique=True)
author = models.ForeignKey(Author, on_delete=models.CASCADE,)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('posts:post_by_category', args=[self.slug])
class Tag(models.Model):
name = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, unique=True)
author = models.ForeignKey(Author, on_delete=models.CASCADE,)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('posts:post_by_tag', args=[self.slug])
class Post(models.Model):
status = models.IntegerField(choices=STATUS, default=0)
position = models.IntegerField(choices=POSITIONS, default=0)
title = models.CharField(max_length=300)
short_title = models.CharField(max_length=200)
display_image = models.ImageField(null=True, blank=True)
post_image = models.ImageField(null=True, blank=True)
content = models.TextField(max_length=8000)
slug = models.SlugField(max_length=300, unique=True)
updated_on = models.DateTimeField(auto_now=True, auto_now_add=False)
created_on = models.DateTimeField(auto_now=False, auto_now_add=True)
author = models.ForeignKey(Author, on_delete=models.CASCADE,)
category = models.ForeignKey(Category, on_delete=models.CASCADE,)
tags = models.ManyToManyField(Tag,)
comment_count = models.IntegerField(default=0)
class Meta:
ordering = ['-created_on']
verbose_name = "All Post"
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('posts:post_detail', kwargs={'slug': self.slug})
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
name = models.CharField(max_length=80)
email = models.EmailField()
body = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
active = models.BooleanField(default=False)
class Meta:
ordering = ['created_on']
def __str__(self):
return 'Comment {} by {}'.format(self.body, self.name)
解决方案
It's something to do with the migrations. Your local DB has all the migrations applied, while your Database on Heroku doesn't have the post
field. Maybe you added/edited the field later so your two DBs are out of sync. Since this seems to be a new project without data it would be easier for you to heroku reset
your heroku database and push
the local db to heroku. Here are the instructions: https://devcenter.heroku.com/articles/heroku-postgres-import-export
推荐阅读
- sql - 我如何操作一对多配置单元表中相关表的结果的结果
- function - 查询功能逻辑,列求和分组
- c# - 我可以轻松地为单元测试制作 DataTable 数据吗?
- python - ValueError:输入数组应具有与目标数组相同数量的样本。找到 0 个输入样本和 121 个目标样本
- javascript - HTML - javascript有没有办法强制POST来自服务器而不是用户的浏览器
- node.js - ReferenceError:BigInt 未定义 Asar Electron Nodejs
- css - CSS跨浏览器问题:替代网格模板列值
- php - 如何正确检查客户端是否与 PHP 套接字断开连接?
- python - 如何*不*在 Windows 中使用 python/anaconda 拥挤您的用户文件夹?
- kernel - 有没有办法使用内核驱动程序来挂钩 winhttp