- Published on
Getting Started with Django 3
Django - The Web Framework for perfectionists with deadlines
- Django Project
Prerequisites
- Basics of Python
- Basics of REST API
Goals
- Learn what is Django
- Setup basic development environment for starter project
- Create a new project
What is django?
Django is a opinionated free and open source powerful Python Web Framework that encourages rapid development and clean, pragmatic design, while offering a relatively shallow learning curve. You can easily build simple web applications in a short time without the need to reinvent the wheel and you can focus on the application logic. Django is also a robust and scalable framework that can be used to create large-scale web applications with complex requirements and integrations. This makes it popular to both beginners and expert programmers.
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Why Django?
- Fast Development - You can launch web application in matter of hours. Most of the complexity is already implemented by the framework itself. There is no need to reinvent the wheel.
- Fully Loaded - Django is known for being Batteries included for having a lot of libraries and methods already implemented by the framework. You can just focus on your application logic or application code.
- Fundamentally Secure - Django framework is fundamentally secure, they take security very seriously. This helps developers avoid many common security mistakes,like - SQL injection, cross-site scripting, cross-site request forgery, clickjacking etc.,
- User Authentication - One of the important things that most of the websites mess up is the User Authentication workflow. Django's Authentication module is very secure and it manages user accounts and passwords in a very secure way. It also support OAuth,
- Highly Scalable - Most application today suffer when a lot of users start using it. The reason is, the applications were not built to scale. Django has an incredible ability to scale well. Some of the largest websites in the world use Django framework - Instagram, Dropbox, YouTube, Spotify, Disqus, Mozilla etc.,
- Django is Mature - Started in 2003, Django isn’t some new framework that hasn’t accounted yet for all common edge cases. Rather Django’s maintainers have handled those edge cases at least once. Instead, the maintainers are worried that Django is now old enough to be interested in dating and has a US driving learner’s permit.
- Django is Python - Python is an immensely popular programming language and is by far #1 in the field of data science. Python is easy to learn yet powerful in execution. It has a gigantic global community of users and hundreds of thousands of libraries to draw on, including Pandas, Numpy, Keras, and of course, Django.
- Django is Proven - No tool remains popular for as long as Django unless it proves itself. Because it is so proven, Django is relied on by startups building dreams, science and fintech efforts presenting their data, and of course, tech giants like Instagram.
- Documentation and community support - django framework has extensive documentation, one of the best documentations if you want to rank them. Also, django community is so wide-spread and popular. Stackoverflow and djangoproject community have had hundred thousands of questions with solutions. Make the best use of it.
- Serialization of Data - Serializers in django is one of the most superior in the web frameworks. Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into
JSON
,XML
,protobuf
or other content types. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data.
Expanding the usefulness of the serializers is something that we would like to address. However, it's not a trivial problem, and it will take some serious design work.
— Russell Keith-Magee, Django users group
Step 1: Installation
Install latest version of Python. Please follow the python installation guide.
You can verify python installation and its version by typing python in the terminal.
$ python
Python 3.8.2
[GCC 4.x] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Learn how to Symlink python3.8 to
python3
command.
Also, please check if python --version
is python 3.x. If it is python 2.x, then, in the commands below, please use python3
instead of python2
.
Now you are ready to go and install Django
python -m pip install Django
Verify Django Installation
$ python -m django --version
$ python
>>> import django
>>> print(django.get_version())
3.0
You may have another version of Django installed. You should see that version.
Step 2: Create a new django project
Create a new django project using the following command in your command line interface-
$ django-admin startproject mywebsite
Now open the project mywebsite in your favourite code editor. If you are not sure of which code editor to use, here are some code editors you can give a try - VS Code or pycharm or sublime.
The contents of the project should look something like this
mywebsite/
manage.py
mywebsite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
- The outer
mywebsite/
root directory is a container for your project. The name of this folder doesn't matter to django. You can rename it the way you want. manage.py
: A command-line utility that lets you interact with this Django project in various ways.manage.py
is automatically created with every new Django project. It is also responsible for setting the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.- The inner
mywebsite/
directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g.mywebsite.urls
). mywebsite/__init__.py
: An empty file that tells Python that this directory should be considered a Python package. If you’re a Python beginner, read more about packages in the official Python docs.mywebsite/settings.py
: A Django settings file contains all the configuration of your Django installation. A settings file is just a Python module with module-level variables.mywebsite/urls.py
: The URL declarations for this Django project.mywebsite/asgi.py
: An entry-point for ASGI-compatible web servers to serve your project.mywebsite/wsgi.py
: An entry-point for WSGI-compatible web servers to serve your project.
Step 3: Running the Project
$ python manage.py runserver
You will see the following lines in your terminal
Performing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
April 01, 2020 - 15:50:53
Django version 3.0, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Open Application in Web Browser
Now open the django application in the web browser http://127.0.0.1:8000/ . You should see a django startup screen! Congratulations! You have made it.
The preceding screenshot indicates that Django is running. If you take a look at your console, you will see the GET request performed by your browser:
[09/Apr/2020 07:07:47] "GET / HTTP/1.1" 200 16351
Each HTTP request is logged in the console by the development server. Any error that occurs while running the development server will also appear in the console. You can run the Django development server on a custom host and port or tell Django to load a specific settings file, as follows.
python manage.py runserver 127.0.0.1:8001 \--settings=mysite.settings
Remember that this server is not suitable for production use, it is only intended for development. In order to deploy Django in a production environment, you should run it as a WSGI application using a web server, such as Apache, Gunicorn, or uWSGI, or as an ASGI application using a server like Uvicorn or Daphne. You can find more information on how to deploy Django with different web servers at https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/.
You can create multiple applications within the same project. In Django, a project is considered a Django installation with some settings. An application is a group of models(M), views(V), templates(T), and URLs. Django follows the MVT architecture. You can think of a project as your website, which contains several applications, such as a API server, blog, wiki, social network or forum, that can also be used by other projects.
Want to know more about django related posts? Explore django posts