Welcome back! Django is a very powerful framework that allows you to create web applications (pretty much full fledged web development) using Python. This is a very powerful addition to any engineers skillset, so let’s setup Django for your Python environment and setup your first project.
Make sure you have Python installed, in case you don’t please read this article I made to learn how to do install it, secondly, I would probably recommend that you have a basic understanding of Python before getting into web development for this language.
First off, let’s install Django! we do this by pip installing, if you don’t have pip, please read this article I created on installing pip. Once installed, run this line in your terminal:
pip install django
Once installed, let’s verify Django installed properly by running this in your terminal / command prompt:
python -m django — version
You should see this output:
Awesome, you have just setup your Django environment! You are pretty much ready to start building out your Django project!
Creating your first project
As soon as you have Django setup, we can create our first sample project! Let’s make our way back to the command prompt / terminal and run this command:
django-admin startproject "name of project you want to use"
In my terminal it looked like this:
This will essentially create a new folder layout in a new directory on your machine. I had to search for it on mine, but whatever you named your project, just reverse search that in your files window and find that folder, mine looked like this:
And once it’s opened , it looked like this:
The manage.py file is the file we run to access the website on our local machine, but within the “django_project” file we will have 4 seperate Python files:
The __init__.py file is a blank Python file, the settings.py file is a Python file that indicates different connections for your website + tons of other parameters you can mess around with (databases, etc.), this is also where your secret key for your website is stored at, our urls.py file also has the mapping for our urls, so if we plan to make a ton of other pages for our website, this is where we would modify our urls, finally, our wsgi.py file will go ahead and allow us to modify how our web server and Django are able to communicate.
Running Our Website
Now this is the most common thing your probably going to be doing for your website, in order to run your specific website, we will have to know the location of the manage.py file we talked about earlier, so navigate to that specific file and copy the location of that file. Next up, we want to make our way over to the terminal / command prompt and run this line of code:
python "location of the manage.py file" runserver
This is what it looks like in my Terminal:
Awesome! Now the way we can access this specific website is literally by going over to our localhost, if you don’t know what localhost is, it is pretty much our local computer which is hosting the website. Luckily, our Django environment gives us the exact address to put inside of our web browser:
So go ahead and copy that specific ip address and paste it inside your web browser, you should see something that looks like this (it may change in the future):
Congrats! You’ve just created your first Django project, keep in mind that this project only runs as long as the Terminal / Command Prompt is running, so if you close the terminal / command prompt the whole entire project shuts down. Hopefully this is just the beginning of your Django journey, but by now you should have a basic understanding of how to setup a project using Python and Django!