The directions at djangogirls for creating databases on pythonanywhere did not work for me:
Creating the database on PythonAnywhere
Here's another thing that's different between your own computer and the server: it uses a different database. So the user accounts and posts can be different on the server and on your computer.
We can initialise the database on the server just like we did the one on your own computer, with migrate and createsuperuser:
(myvenv) $ python manage.py migrate Operations to perform: [...] Applying sessions.0001_initial... OK (myvenv) $ python manage.py createsuperuser
This is what I got:
(djgirls)05:05 ~/my-first-blog (master)$ python manage.py migrate
Operations to perform:
Apply all migrations: auth, contenttypes, sessions, admin, blog
Running migrations:
No migrations to apply.
(djgirls)05:06 ~/my-first-blog (master)$
Note how the last line says no migrations were applied.
According to the help directions at pythonanywere, you have to go to your pythonanywhere dashboard
, then click on the databases
tab, then select the type of database you want to create, then create a password. After you create your password, you will see a database called:
<your_user_name>$default
You can use that database, so you don't need to create a new database.
Then for python3 you need to use pip to install mysqlclient
(noted at the end of the help directions). If you look at dashboard/consoles tab
, there is a link you can click on to open a bash shell (or you can use a previously opened shell):
08:14 ~ $ ls
README.txt my-first-blog
08:14 ~ $ cd my-first-blog/
08:15 ~/my-first-blog (master)$ ls
blog db.sqlite3 djgirls manage.py mysite static
08:15 ~/my-first-blog (master)$ source djgirls/bin/activate (make sure your venv is activated)
(djgirls)08:15 ~/my-first-blog (master)$ pip install mysqlclient
The pythonanywhere help directions use virtualenv
, which is a third party python module for creating virtual environments. It is different from venv
, which is builtin to python. The djangogirls tutorial uses venv
, but switches to using virtualenv
on pythonanywhere.
Finally, you need to make some changes to settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '<your_username>$<your_database_name>',
'USER': '<your_username>',
'PASSWORD': '<your_mysql_password>',
'HOST': '<your_mysql_hostname>',
}
}
The fill in for <your_mysql_hostname>
can be found in the databases
tab on your dashboard.
After doing all that, the migration worked:
(djgirls)08:15 ~/my-first-blog (master)$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, blog, auth, contenttypes, sessions
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying blog.0001_initial... OK
Applying sessions.0001_initial... OK
(djgirls)08:37 ~/my-first-blog (master)$