My dear reader, how are you? السلام عليكم
Take small steps every day and you will get there one day!
In this post, we will continue our ‘web-journey‘ and move forward from motivation to implementation. I will explain the basic default structure of a Django project with steps to its installation. If you would like to read a brief motivation on Django for web application development, I would suggest you follow the following tutorials, first.
- Read about MVC architectural patterns in “Introduction to Design Patterns for Web Applications” (DirectMe)
- Read about quick Django introduction in “Insights to Backend Web Development Frameworks and Databases” (DirectMe)
- Read about details on why and when to use Django in “Inside Out of Django for Web Application Development” (DirectMe)
Reading the posts above would help you to understand that Django uses a Model View Controller (MVC) based architectural pattern and as a tool, it is termed as a Model View Template (MVT) framework. As a recap from the MVC architectural pattern, a model only represents application data. A view serves to present the model data to the user and knows how to access the data. A controller usually exists between model and view to listen to user actions such as button clicks and etc. In Django, a view works as a controller in itself and therefore depends on the model. Finally, templates contain the HTML, CSS and JS files that are served to the user to interact. I would say that in Django, you consider the templates as a view of MVC.
Lets us get started with Django installation. I would explain the installation process on Linux Ubuntu. If you are a Windows 10 user, you may install a ubuntu application from the Microsoft app store. Once done, the following are the 3 steps to install and run Django on your machine and create a default application.
Step 1) Create and activate a virtual environment using virtualenv.
Step 2) Install Django inside the virtual environment and create a Django project.
Step 3) Create a Django application.
Let me explain the steps in detail now.
Step 1) creating a virtual environment: First natural question to ask yourself is what is a virtual env and how would it help you in working with Django. A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them. Therefore, installing Django inside an isolated environment would allow you to use different package versions for different projects, which is far more practical than installing Python packages system-wide. Another advantage of using an isolated environment is that you won’t need any administration privileges to install Python packages. We will use the tools called virtualenv (DirectMe) to create it. Run the following command in your shell to install virtualenv:
$ sudo apt-get install pip3 $ pip3 install virtualenv $ virtualenv my_env # create a virtual environment. Say, my_env $ source my_env/bin/activate # activate your virtial env $ (my_env)arsalan:~$ # once activated, shell prompt will include the name of the active virtual environment enclosed in parentheses # Incase you would like to return back, use deactivate command
Step 2) create a Django project. Before you create a Django project, install Python3 in your virtual environment.
$ pip3 install python3 # check if it is installed successfully $ python3 --version # install Django $ pip3 install django # check Django version $ django --version # create a django project and name it mysite $ django-admin startproject mysite # check the project structure using tree mysite, it should be as follows. mysite/ manage.py mysite/ __init__.py settings.py urls.py wsgi.py # go to mysite/ directory and check if development server is working properly $ cd mysite $ python3 manage.py runserver # check if the development server is running at localhost on port 8000. Enter the following URL in your browser: http://127.0.0.1:8000
Let us quickly understand the files in default Django project individually:
- manage.py This is a command-line utility used to interact with your project. It is a thin wrapper around the django-admin.py tool. You don’t need to edit this file.
- mysite/ This is your project directory, which consists of the following files:
- __init__.py An empty file that tells Python to treat the mysite directory as a Python module.
- settings.py This indicates settings and configuration for your project and contains initial default settings.
- urls.py This is the place where your URL patterns live. Each URL defined here is mapped to a view.
wsgi.py This is the configuration to run your project as a Web Server Gateway Interface (WSGI) application.
The generated settings.py file contains the project settings. It includes a configured SQLite 3 database by default. You can configure Django to work with other DB types as well such as MySQL, Oracle, PostgreSQL, MongoDB and more. A list named INSTALLED_APPS contains Django applications that are added to your project by default. we will list our application in here later once we create them. Further explanations can be found in official Django documentation (DirectMe).
Step 3) create a Django application: In order to create your application, make sure you are in the same directory as manage.py and type the following command:
$ python3 manage.py startapp your-app-name
# This should create a directory with your application name with following structure.
your-app-name/
__init__.py
admin.py
migrations/
__init__.py
models.py
tests.py
views.py
A quick explanation of these files are as follows:
- admin.py This is where you register models to include them in the Django administration site—using the Django admin site is optional.
- apps.py This includes the main configuration of the blog application. migrations: This directory will contain database migrations of your application. Migrations allow Django to track your model changes and synchronize the database accordingly.
- models.py Data models of your application—all Django applications need to have a models.py file, but this file can be left empty.
- tests.py This is where you can add tests for your application.
- views.py The logic of your application goes here; each view receives an HTTP request, processes it, and returns a response.
References
[1] Official Django Documentation, https://docs.djangoproject.com/en/2.2/
[2] Melé, A. (2015). Django By Example. Packt Publishing Ltd.
I hope you find this post useful. If you find any errors or feel any need for improvement, let me know in your comments below.
Have you worked with Django or followed this tutorial? Let me know about your experience.
See you next week! Happy learning.