问题
First, I was able to fix the ImportError. I figured out that it was because the Django version of pythonanywhere is not updated, So I upgraded Django on pythonanywhere from 1.x.x to 2.0.9.
The error came out like this:
ImportError at / cannot import name 'path'
django version: 1.x.x
python version: 3.6.6
and, unfortunately, my app gave me another error:
OperationalError at / no such column: blog_post.published_date Request Method: GET Request URL: http://.pythonanywhere.com/ Django Version: 2.0.9 Exception Type: OperationalError Exception Value:
no such column: blog_post.published_date Exception Location: /home//my-first-blog/myenv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py in execute, line 303 Python Executable: /usr/local/bin/uwsgi Python Version: 3.6.6
I thought this error occurred because of some database, so I tried migrate
or makemigrations
on pythonanywhere, but I could not fix it still.
So, is there anyone who knows how to fix this database?
Here is my model.py
:
from django.conf import settings
from django.db import models
from django.utils import timezone
class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
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
here is the output of python manage.py showmigrations
:
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
[X] 0009_alter_user_last_name_max_length
blog
[X] 0001_initial
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
sessions
[X] 0001_initial
回答1:
The problem as I see has to be with the database and django migrations.
The Post
object inside the blog has the attribute that django's trying to find. The migrations haven't been correctly applied to the database.
Now considering the history of migrations, I do not know what's going wrong unless I can look around your database which I'm assuming is an sqlite.
One way to resolve this if you're having a newly constructed database is to get rid of the database and do the following:
- Delete all the migrations from
app/migrations
directory python manage.py makemigrations
python manage.py migrate
Also, try to avoid sqlite
as much as possible. The same migrations that ran on an sqlite
db might be erroneous on Postgres or MySQL database which are more production grade databases.
NOTE: Please understand that this would lead to a complete data loss. Hence, try this only if you can afford to compromise on the existing/test data.
回答2:
Don't forget to refresh your production server after every migration if you want the changes to take effect
来源:https://stackoverflow.com/questions/53863318/django-operationalerror-no-such-column-on-pythonanywhere