Step 1 :- Create Django Project For Deployment or use what you have created and some required local changes
django-admin startproject testproject
( before also check that django is already installed in you pc or not with command :- python -m django –version )
we will add allow all the host so put below code in our settings.py file
ALLOWED_HOSTS = ['*']
and then freeze your requirements file with below command :-
pip3 freeze > requirements.txt
then start your server (for just checking) :-
cd testproject
python manage.py runserver
2. Upload Project On Github
Create project on github , remote that in your Cmd or Bash and push the project code on Github.
git init git add .
git commit -m "first commit" git branch -M main git remote add origin https://github.com/rajparmarr2308/testproject.git git push -u origin main
3. Open PythonAnyWhere from this link and create new account
( make sure u give the username as the name which you wanted to add in your website beacuse after it will not change . i have given mytestproject as username)
go on consoles link and click on bash
4. Clone github repo and setup virtualenvironment
now we will clone out github repository in that Bash. put below command,
git clone https://github.com/rajparmarr208/testproject.git
now after cloning out project we will create virtualenvironment (make sure you put proprr python version i am using 3.9 here)
mkvirtualenv --python=/usr/bin/python3.9 testenv
Now your virtualenv is activated. if not then type
workon testenv
now we will install all our requiements (if u are not using requirements file then u need to manually install the django)
pip install -r requirements.txt
5 .Create a Web Application with Manual Config
Go to Web Tab and Click on it. You will see create a web application button, click on it
then click on Manual Configuration (make sure u not click onthe “Django” option)
Now our web app will get created then we will add some paths:-
Source code:
/home/mytestproject/testproject
Working directory:
/home/mytestproject/
Virtual env :-
/home/mytestproject/.virtualenvs/testenv
Static files
URL Directory
/static/ /home/mytestproject/testproject/static
/media/ /home/mytestproject/testproject/media
6.Setting up WSGI file
Now we will edit WSGI File to Point our Django Project to Server.
Click on WSGI configuration file link :
/var/www/mytestproject_pythonanywhere_com_wsgi.py
It will take you to your Hosted Django Project WSGI File. We need to add the following code. Just remove everything inside that file and paste the bellow code in it. Note you need to change bold code which your relevant paths.
import os import sys # keeping this path handy # '/home/mytestproject/testproject/testproject/settings.py' path = '/home/mytestproject/testproject' if path not in sys.path: sys.path.insert(0, path) os.environ['DJANGO_SETTINGS_MODULE'] = 'testproject.settings' from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
Save the file. And reload your apllication from web tab.
Our App Deployed Successfully to PythonAnyWhere. ( if u are using database then you can need to migrate them as well)
That’s it. Thank you!
thank-you
its works absolutely fine.
I agree with your point of view, your article has given me a lot of help and benefited me a lot. Thanks. Hope you continue to write such excellent articles.
Reading your article helped me a lot and I agree with you. But I still have some doubts, can you clarify for me? I’ll keep an eye out for your answers.