image
Introduction to Django (Part 2)
#Python #Programming #Beginner #Django

We'll start off by creating our django project in our specified location which you had created in in our first part. Incase you missed that go back and start this series; Introduction to Django(Part 1)
To create a project in django we do by running

django-admin startproject (project_name)

Note the project name should not be in parenthesis, it should be some like 

django-admin startproject MyWebsite

After that press enter and a folder with the project name will be created, inside the folder there will be another folder with the same name as the project name and a file called manage.py
So we don't have to touch the manage.py file, we won't be editing a thing in it. This file is the one responsible for managing all our tasks, from making our migrations to running our server. 
In the subfolder created we will be dealing with settings.py to do some settings to our projects, urls.py to set the URL paths, wsgi.py will be used when deploying, so we won't need to touch that.
Now after we have our project we can now go ahead and create our app..
In django the project file is like our entire website file, for instance we wanted to do a school management site, the school management site will be our project, then within the project we will have apps, like maybe the finance app that handles finances, administration app that handles admissions of students etc, so that's just some basic point to note so as you understand better the difference between an app and a project
And before we create our app, we can test to see if our servers are working correctly and we can do that by running

python manage.py runserver

Running this will result into an error because we are not in the correct folder which has manage.py file so we have to move into the folder created by running

cd project_name

For our case it will 

cd MyWebsite

After that we can now the the 

python manage.py runserver

This will start the server with a default port of 8000 but if you have another application running on the 8000 port, you can specify the port you want your django project to run on i.e 

python manage.py runserver 8081

This will start the server at port 8081.
The server will start running though we will get some notification telling us we have some unapplied migrations, why is this?
After creating a django project, django comes with a boilerplate already, like the admin panel and the migrations are needed so that it can create the relevant tables for the boiler plate.
Note the server will still be running, so you can clink on the link provided in your terminal which is always your licalhost address, which will open a django landing page with a rocket ready to take off, if you see this, it means everything is working well.
Now we need to stop our server by pressing 

Ctrl + C

Then we can now run our migrations by 

python manage.py migrate

This will create the relevant tables needed and a new file file called db.sqlite3 will be created, this is our database since we will be using the sqlite database that comes by default in django.
After this is done, we can now create our superuser so that we can try logging in into the admin panel and to do this we run

python manage.py createsuperuser

Some qqueations will be displayed on the terminal like your name, email address, password etc.
Fill in the details and note when typing the password, you won't get any feedback, so just type the password and when done click Enter, confirm the password and click Enter again. Upon successful creation, we can now start the server once again and head to our browser.
In the browser url you need to add "/admin", it will now be something like 

127.0.0.1:8081/admin

Assuming we used port 8081, now click enter, and you'll be prompted by a dhango admin login screen, enter your name and password and login.
Whoooolah, you are in. It was easy right? 
That's the fun thing about django, it generates the admin panel automatically for you.

Hope we are making some progress. We'll continue in our next article from here.

See you then...