After you have finished developing and testing your Django app locally, use the command prompt or git bash to commit your code to Github. Push your Django project on Github. Browse on pythonanywhere.com and create a free account. Log in to your account. The dashboard will be as shown below.
Click on the Files tab and click on the Open bash console here. This will open a bash console on the current directory.
Run the following command to clone your project from Github.
git clone <your-github-web-url>. In my case am using
git clone https://github.com/jkmchinoxxx/pythonanywhere.git
After the cloning process has finished, you can navigate back to the files page to confirm whether are the files from Github have been successfully cloned.
Click the console tab on your dashboard and start a new console by clicking the bash button.
Type the following command into your bash to make a virtual environment.
mkvirtualenv –python=/usr/bin/python3.7 your-site-virtualenv .
Remember to replace the python version with the version you selected and also replace {your-site} with your preferred name. Press enter to execute the command. If correctly executed, you should have something similar to the following:
After successfully creating your virtual environment, it’s time to install Django. Run pip install django==2.1.0 replace the numbers with the Django version you when developing your application.
Create a new web app by clicking on the web tab and clicking on the Add a new web app button. The free account does not support custom domains and thus your web application will go live on your-username.pythonanywhere.com. Click next to continue. The page will be similar to the one shown below.
Click on the manual configuration (including virtualenvs) and you will be directed to another page. On the new page select the python version used to develop your site. In my case, I will select python 3.7.3. Selecting the manual enables you to include a virtual environment for your project manually.
Click next to finish creating the web app. This will take some moments before creating the web app.
Edit the path that your web application source code. Scroll down until you see a similar section like the one shown below.
Click on the statement highlighted in red and enter the path to your web application source code. The path should be /home/your-username/your-web-app-name/.
Edit the path to your virtual environment (the virtualenv you created). The path should be /home/your-username/.virtualenvs/your-virtualenv-name.
Save the changes by reloading your site by clicking the green button located under the reload section. After editing both the source code and the virtualenv path and then reloading my site, it is as shown below:
Click on the link containing the WSGI file to edit it.
While still on the files page, edit the static and media files of your Django application. This can be done under the static files section. In my case I will edit the media path by using the URL as /media/ and the path as /home/my-username/my-site-name/media. To edit the static files of the admin dashboard, edit the URL to be
/static/admin/
and the path to be
/home/your-username/.virtualenvs/your-virtualenv-name/lib/python3.7/site packages/Django/contrib/admin/static/admin.
Reload your site to save the changes.
Now the last thing is to configure our database. In this case, I will handle how to configure the MySQL database. On your dashboard, click on the databases tab. Enter the name of your database and click create. This will create a new database. On the same page, the database host address and the username are provided. Edit your settings with the new database credentials. The database name is yourusername$databasename. Also, remember to set a password for your database. In your settings.py file, edit the allowed hosts to be as shown below:
Open your database on the console by clicking on the start a console on under your databases.
We are going to import our local MySQL database via the console. Export your local database and navigate to the files page of your Pythonanywhere dashboard. Upload the exported database file. Open a MySQL console and run USE your-username$databasename. Then run SOURCE your-export-db-file.sql. You should see the following on a successful import.
You can now visit your site by clicking on the link provided by pythonanywhere as your domain name. in my case it is http://jkm2407.pythonanywhere.com/