我已经完成了 djangogirls 教程并且遇到了 OperationalError:没有这样的列:blog_post.user_id。
这在我的本地服务器上效果很好,但是在推送到 Pythonanywhere 后我得到了 OperationalError。
我正在使用 Django 1.9、Python 3.4.3
我已经完成了 python manage.py makemigrations,在 Bash 控制台上成功迁移,但我仍然在网站上收到错误消息。然后我尝试再次执行此操作,并且“未检测到更改”和“无需迁移”
这是我的 Models.py
from django.contrib.auth.models import User
from django.utils import timezone
from django.core.urlresolvers import reverse
from django.db import models
from django.db.models.signals import pre_save
from django.utils.text import slugify
from django.conf import settings
def upload_location(instance, filename):
return "%s/%s" %(instance.id, filename)
class Post(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
title = models.CharField(max_length=200)
slug = models.SlugField(unique=True)
image = models.ImageField(upload_to=upload_location,
null=True,
blank=True,
width_field="width_field",
height_field="height_field")
height_field = models.IntegerField(default=0)
width_field = models.IntegerField(default=0)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title
def get_absolute_url(self):
return reversal("posts:detail", kwargs={"slug": self.slug})
def pre_save_post_receiver (sender, instance, *args, **kwargs):
slug = slugify(instance.title)
exists = Post.objects.filter(slug=slug).exists()
if exists:
slug = "%s-%s" %(slug, instance.id)
instance.slug = slug
pre_save.connect(pre_save_post_receiver, sender=Post)
这是我的 Views.py
from django.contrib import messages
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.utils import timezone
from .models import Post
from django.shortcuts import render, get_object_or_404, redirect
from .forms import PostForm
# Create your views here.
def post_list(request):
posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
paginator = Paginator(posts, 5) # Show 25 contacts per page
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
posts = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
posts = paginator.page(paginator.num_pages)
return render(request, 'blog/post_list.html', {'posts':posts})
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'blog/post_detail.html', {'post': post})
def post_new(request):
if not request.user.is_staff or not request.user.is_superuser:
raise Http404
if request.method == "POST":
form = PostForm(request.POST, request.FILES or None)
if form.is_valid():
post = form.save(commit=False)
post.user = request.user
post.published_date = timezone.now()
post.save()
messages.success(request, "Awesome! Your new post was successfully created!")
return redirect('post_detail', pk=post.pk)
else:
form = PostForm()
return render(request, 'blog/post_edit.html', {'form': form})
def post_edit(request, pk):
if not request.user.is_staff or not request.user.is_superuser:
raise Http404
post = get_object_or_404(Post, pk=pk)
if request.method == "POST":
form = PostForm(request.POST, request.FILES or None, instance=post)
if form.is_valid():
post = form.save(commit=False)
post.user = request.user
post.published_date = timezone.now()
post.save()
messages.success(request, "Awesome! Your new post was successfully edited!")
return redirect('post_detail', pk=post.pk)
else:
form = PostForm(instance=post)
return render(request, 'blog/post_edit.html', {'form': form})
def post_delete(request, pk):
if not request.user.is_staff or not request.user.is_superuser:
raise Http404
post = get_object_or_404(Post, pk=pk)
post.delete()
messages.success(request, "That post was successfully deleted!")
return redirect("post_list")
打开网页时出现 blog_post 错误。页面卡住的行是我的 post_list.html 中的 {% for post in posts %}
这是我的 post_list.html
{% extends 'blog/base.html' %}
{% block content %}
<div class='col-sm-7 col-sm-offset-3'>
<div class="row">
{% for post in posts %}
<div class="col-sm-12">
<div class="thumbnail">
{% if post.image %}
<img src="{{post.image.url}}" class='img-responsive img-rounded' alt="...">
{% endif %}
<div class="caption">
<h3><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }} <small>{{ post.published_date|timesince }} ago</small></a></h3>
<p><b>Author: {{ post.user }}</b></p>
<p>{{ post.text|linebreaks|truncatechars:300 }}</p>
<hr/>
<p><a href="{% url 'post_detail' pk=post.pk %}" class="btn btn-primary" role="button">View</a> </p>
</div>
</div>
</div>
{% endfor %}
</div>
<div class="pagination">
<span class="step-links">
{% if posts.has_previous %}
<a href="?page={{ posts.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ posts.number }} of {{ posts.paginator.num_pages }}.
</span>
{% if posts.has_next %}
<a href="?page={{ posts.next_page_number }}">next</a>
{% endif %}
</span>
</div>
</div>
{% endblock %}
我删除了我的数据库并使用新的超级用户创建了一个新数据库。当我从 github 推送到 pythonanywhere 并运行迁移后重新加载页面时,我仍然收到此错误消息。请问,我该如何解决这个问题?