A Complete Beginner’s Guide to Django
Anaconda Installation
Great so you’ve decided to install Django! Good for you mate!! So go ahead and install the Anaconda Python distribution.
Once you are done installing open your CMD or terminal. follow the following steps with me.
Creating the virtual environment
You might ask, why do I need a virtual environment. Well, the reason is you can easily manage multiple work-spaces or projects in one machine without any package dependency confliction.
So in your terminal follow these commands.
1 |
conda create -n myfirstDjangoProject python=3.6 anaconda |
great! the above command will create a new Anaconda virtual environment for you, and we named it as myfirstDjangoProject.
Now we need to activate the project. So use the following command.
1 |
source activate myfirstDjangoProject |
Wonderful! now let’s install Django using the following command.
1 |
conda install -c anaconda django |
This might take a few minutes, after that you should be ready to proceed with the rest of the steps.
Creating the Django Project
Go ahead and create a folder on your desktop. Now navigate to the folder you created using the terminal if you closed the terminal by mistake that’s okay, just open the terminal window inside the folder you created. Then go ahead and activate your virtual environment using the following command source activate myfirstDjangoProject.
Now, I’m going to create the project, and I’m going name it as firstSite.
1 |
django-admin startproject firstSite |
Now, cd into the firstSite, and list all the files, and you should see the following.
firstSite manage.py
Great! we are almost done. Now go ahead and type the final command.
1 |
python manage.py runserver 0.0.0.0:8000 |
In the above code, we are saying go ahead and run the Django server on port 8000, you can visit that URL at http://0.0.0.0:8000/ but you should get an error message saying “DisallowedHost at / Invalid HTTP_HOST header: ‘0.0.0.0:8000’. You may need to add ‘0.0.0.0’ to ALLOWED_HOSTS” well, let’s edit our settings.py, go into the firstSite–>firstSite folder and you should see the settings.py Python file.
Open it with your favourite text editor, and look for the following line. ALLOWED_HOSTS = [] and replace it with the following ALLOWED_HOSTS = ['0.0.0.0:8000','0.0.0.0']
Here what we are saying is we would like to run our Django web server up on port 8000 upon request. Make sure to save the file, and the run server again.
Here is the command python manage.py runserver 0.0.0.0:8000
Congraluation! You have successfully created your first Django Project!
Leave a Reply
You must be logged in to post a comment.