Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Setup and First Application with Django Web Framework

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. In this tutorial we will see how to configure it on Windows/Linux.

What is 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. With Django, you can take Web applications from concept to launch in a matter of hours. Django 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.

Installing Django on Windows

To install Django on your Windows machine you will first have to install Python on your system. This step is required only for Windows machine because Linux and Mac have Python pre-installed.

Head over to “ https://python.org/download/ “ and download the latest version of the Python. Make sure to download the MSI Installer. Once downloaded run the MSI installer and follow the on screen steps. It is similar to installing a simple software on the python. Once completed you can check the version of the Python installed by opening “Windows Powershell” and then typing the command :

python --version

Figure 1. Checking the Python version

Next step is to install Python Setup Tools on your system, this is required to install Python Packages on your system.

Head over to "https://pypi.python.org/pypi/setuptools"and install the latest version of Setup Tools for your system.

Next step is to install PIP on your system. PIP is a Python package that let’s you install Python packages through Python Package Index ( PyPI ).

Open the Windows Powershell or Command Prompt and type the following command :

easy_install pip

This command will only work if you have successfully installed Setup Tools as guided in the above step. Alternatively you can also download the PIP setup and install using windows installer. You will get the setup at “ http://www.pip-installer.org/en/latest/installing.html “.

After this you will have to install Django on your machine. This can be done by using PIP.

In the command prompt or Windows Powershell type the following command :

pip install django

This will download the latest version of Django on your system. You can verify the correct installation by typing :

django-admin --version

This will give you the version of Django installed on your system. In the next section we will learn how to install Django on Linux and we will use Linux Operating System for our Django Projects.

Installing Django on Linux

To install Django on our system, we require two scripts installed i.e “easy_install” and the “virtualenv”. First we need to get “easy_install” and then we can install “virtualenv” and create our own virtual environment.

“easy_install“ script will be used to install Python modules from PyPI (Python Package Index). PyPI is a repository which contains all different Python modules that are used for better functioning of Python. “easy_install” script is a part of Python Setup Tools, so we will first have to install Python Setup Tools in our root folder.

Fire up the terminal and write this line to install Python Setup Tools =>

sudo apt-get install python-setuptools

Figure 2. Installing Python Setup Tools

After pressing enter you will be required to enter your password and then installation will automatically start. If you are getting an error after entering your system password then you have to follow this step. In your terminal type “sudo apt-get update” and press enter. This will automatically update the “apt-get” to the newest version. After this you can follow the above step again and you will get no error while installing Python Setup Tools. After the installation has been completed you will see this type of message on the terminal.

Figure 3. Successful Installation of Python Setup Tools

Now that we have successfully installed Python Setup Tools on our system, we can use the “easy_install” script ( which is a part of Python Setup Tools ) to install virtual environment ( virtualenv ). Virtual environment is used to sandbox your Python applications i.e you can install different modules and dependencies in your virtual environment and it will not affect your root folder. Basically virtualenv will create a sudo root folder for you Python Application.

In the terminal use this line to install “virtualenv” =>

sudo easy_install virtualenv

Figure 4. Installing virtualenv

It will now install virtualenv and will take 2-3 minutes depending on your Internet speed. After the installation has completed you will see this in your terminal.

Figure 5. Successful Installation of virtualenv

Now we have installed both “easy_install” and “virtualenv” scripts. Now it’s time to create a new folder which will act as our virtual environment.

We will create a folder using virtualenv and then install Django in the created folder.

virtualenv --no-site-packages django

Figure 6. Creating a folder using virtualenv

“--no-site-packages” is a switch i.e this is used for bare installation. It means that it will install only those modules that are not present in the root of the system. It will only install those modules that are required by the application so that it can save space and speed up the application.

Now we have to activate the environment so that from now onwards all the scripts that we are installing will go into our virtual environment rather than the root of the system.

source django/bin/activate

Figure 7. Activating the virtual environment

In the above screenshot of the terminal you can see that (django) osboxes@samarth indicates that we are inside a virtual environment “django” that we created a bit earlier. Now if we install any script or module then it will be installed in our virtual environment rather than the root of our system. Now we will install Django in our environment.

easy_install Django

Figure 8. Installing Django

In the above snapshot you can easily see that it will automatically install the latest version of Django and in this case it is saying Best Match : “Django 1.8.4”. It will install this version in our virtual environment.

Now we will enter our django directory that we created using virtaulenv.

“cd django”

After entering this directory we will all the different files inside the ‘bin’ folder using this command

“ls -l bin/”

This is how terminal will look.

Figure 9. Having a look inside the bin folder

Here in the above figure you can see the “activate” file which we have used to activate our virtual environment. You will see all the different files that are installed in the environment inside the ‘bin’ folder.

Now we will create a new Django project and see how it will look in the browser. We will use the command

“django-admin.py startproject django_test”

Here ‘django_test’ is the name of our project.

Now if we again look at our django folder then we will notice that our project has been created.

Figure 10. Creating our first Django Project

Now we will move into our project folder by using

“cd django_test”

If we look inside our “django_test” folder then we can see that there are total 2 files and a single folder. The “django_test” folder will contain all the settings and urls of our main project. “db.sqlite3” is our database file which will hold all the data of our project in a safe and organised manner. Whenever we want to look at our database we can simply fire up SQLite Database Browser and it will show all the tables in our database. Next we have a Python executable file “manage.py” which manages different functions related to our project. Some functions are “migrating data to the database”, “syncing the database”, “starting the server”, “starting the project”, “starting the app” and many more. If you want to look at all the functions “manage.py” can perform then on your terminal type the following :

python manage.py help

This will display all the functions which this file can perform.

Now we have to start the server so that we can see our first application in the web browser.

“python manage.py runserver”

This command will start the server and we will be able to view our application in the web browser.

Figure 11. Starting the server

Don’t worry about the red lines that you see in the terminal, we will fix those while setting up a database for our application. You can use the link provided in the terminal “http://127.0.0.1:8000/” to view your application in the web browser.

Figure 12. Our first Django application in the browser

Huray!!!! we have successfully installed Django in our system and we have created our first Django project. Now you might be wondering how we can exit our virtual environment and enter our default (root) folder. You will simply have to type “deactivate” in your terminal and you will be redirected back to your root folder.

Conclusion

We have seen how to install Django on Windows Machine and we have also installed “easy_install” , “virtualenv” and “Django” in our system. We also have created our first Django project and viewed it in the browser. Now you are ready to create your own projects using Python Django. For Mac users all steps are similar to the Linux installation.

Links

Official Django Website => “ https://www.djangoproject.com/

Official Python Website => “ https://www.python.org/



CODING is my Passion first, hobby second and job third! I am Full Stack Developer from last two years and I love building scalable web applications and modern websites with new technologies.I started with my web development with ...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login