1

私は djangogirls チュートリアルを実行しましたが、OperationalError: no such column: 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")

Web ページを開くと、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 にプッシュして移行を実行した後にページをリロードすると、このエラー メッセージが引き続き表示されます。どうすればこれを修正できますか?

4

0 に答える 0