Alongside my friend @ Chakit Arora π who created an article on "How to start a Flask Server in 7 minutes" I decided to try something similar but with Django π because why not π
Why Django?
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel. Itβs free and open source.
The framework runs on python so for all you Pythonians or Pythonista you are free to go to hop on the Django train π. Aside from the fact, the framework is super cool and awesome, well-known organizations use Django to create the popular web app we know today. Examples of these apps are: Disqus (largest project built with Django so far), Youtube, Instagram, Bitbucket e.t.c
If you want to know more about Django you can check out their official website here, enough of the fanatics and convincing you why you should pick Django over other Python frameworks π (just kidding tho)
Starting a server on Django
Starting a server is quite easy with Django, but there are few pre-requisites to follow so you get the best out of the framework. Things you will need β¬οΈ
- Django (Obviously π )
- Virtual Environment ( So you can run Django and other requirements )
- And I guess that should be all ( Easy right )
Let's get into it then
Setting up a virtual environment
If you have Python already installed on your system I guess you have pip installed automatically so you should be good... You can also confirm if you have the package by typing:
# On Your Terminal
pip --version
# If you don't see any error when this is typed out, it means you have pip installed and we can move on
# If you got an error... simple google how to install pip on your system ( Google is really the oracle you know πΌπΎ )
if you are running on cmd you can simply type "cls" to clear your terminal or "clear" if you are using a more advanced bash terminal.
# Once you have cleared your terminal or not ( it really depends on you tho ) run the snippet below
pip install virtualenv
And that's it! To install virtualenv, it's very easy. Now let's create our virtual environment
Like I mentioned before a virtual environment is like a dedicated space where you use to run and experiment with your project without interrupting the natural flow of your system ( Well in my own definition, you can google the actual meaning )
Creating a virtual environment
virtualenv myenv
# you can name your virtual environment in any convention you want
# Basically Virtualenv "anynameofyourchoice"
Once that is done your virtual environment would have been created, now you need to activate your environment... For this step, we have different methods of activating depending on your OS
# Windows
myenv\Scripts\activate
# myenv here is the name that was given to the virtual environment that was created above
# Mac or Linux
cd my_project_folder
# Changing directory into the folder they want to run their virtualenv
source bin/activate
# Activating the virtualenv
(myenv)/hostname/Desktop
# Once it is activated you should see something somewhat similar to this
( breathes in ..... breathes out )... Finally, let's get to step 1 of the pre-requisite
Installing Django
pip install Django
# Simple... All you have to do
Well at this point you can sit down and watch the download or check out the latest Tv series/ movies on Netflix ("Money Heist" just came out today π... For Nairobi ββ )
The installation should be done by now, so you have Django installed on your system ( hmm... not on your system tho but your virtual environment so yeah ). We have basically gone through the entire list yeah now let's get our server up and running ππ.
Starting a Server
To begin with, it is Django convention to start by creating a project
# creating a project named myblog
django-admin startproject myblog
# The folder will be created with some files included: urls.py, manage.py e.t.c
To run our server we would make use of the manage.py
# To run the server you have to cd (change directory into your project)
# And if you have done that initially, you are good to go
python manage.py runserver
# Once you run this Django will create and launch a server running on port 8080
Your server is running already... you can visit the localhost address provided on your terminal and the outcome should be like this β¬οΈ
And that is how you run a server on Django π ( easy ) see you later guys ππΎ