Django: The web framework for perfectionists with deadlines

Beginners guide to Django Framework

Yakul Khajuria
crossML Blog
6 min readNov 8, 2021

--

Django is a Python web framework. It is free and open-source. It has a very large and active community and has very brilliant documentation.

“Django is a framework of python and we know that python is integrated with data science, machine learning and artificial intelligence so we can also use these technologies in our projects”

Django workflow

  • Urls: In Django, there is a separate urls.py file to handle URLs and against each URL there is a function defined in views.py to handle that request came from URL
  • View: It handles the HTTP request and returns the HTTP response. it also uses the data from the database required to fulfill the request against a particular URL
  • Models: In models.py we define the structure of tables and their relationships. we don’t need to write a query to create the table we only describe the column name and their reaction
  • Templates: A template is a text file defining the structure or layout of a file A view can dynamically create an HTML page using an HTML template, populating it with data from a model. A template can be used to define the structure of any type of file; it doesn’t have to be HTML!
  • Admin: admin section is provided by Django we can also override the admin section

Django functionalities

  • Rapid development

We will not need to create separate files for database design and connect them with the server. So we won’t need an extra file burden. This saves time, money, and effort. Hence we can work more on another functionality of the project rather than basic requirements in a project that is most common in most of the projects

  • Offers High Security

Django is super secure. To prove the feature, you can always take examples of lots of websites that are worldwide and possess huge traffic.

Django prevents most common security mistakes:

  • XSS Protection: Django template system by default escapes variables unless they are explicitly marked as safe.
  • CSRF Protection: This generates a unique token that authenticates the user on each request.
  • SQL injection Protection: Django uses ORM hence there is no risk of SQL injection.

Additional security features:

  • Safe password hash: Django encrypt/hash your password by PBKDF2, bcrypt algorithms
  • Clickjacking Protection: Django also detect the content when it is requested from an unauthorized iframe

Thoroughly Tested

When we learn a new technology we check the future scope of that technology. Since Django has been in the industry for more than the last ten years and is still very very popular technology than other frameworks.

Many big companies prefer Django in their projects like Instagram, Spotify, and many more. So we can say that it handles the website traffic very smoothly.

Other functionalities

Django provides many other built-in methods as it provides you user authentication system.

  • It also provides you with model forms so you don’t need to create separate HTML forms.
  • It is cross-platform.
  • It works with most major databases and allows using a database that is more suitable in a particular project, or even multiple databases at the same time.

Creating the first Website using Django:

Commonly when a web application receives a request from a client(web browser) that web application checks what is needed to that request in the URLs and data will be in Post or get or any method that reads or writes information from the database and performs the required operation to fulfill the user request. The application then returns a response to the web browser, often dynamically creating an HTML page for the browser to display by inserting the retrieved data into placeholders in an HTML template.

Create a project

  • The outer mysite/ root directory is a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
  • manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py.
  • mysite/__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.
  • mysite/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
  • mysite/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL dispatcher.
  • mysite/asgi.py: An entry-point for ASGI-compatible web servers to serve your project.
  • mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project.

Create app

Django provides built-in tables of groups and users you just have to migrate them to use them. Our model is all set up. Now to run our migrations, which will set up our database.

  • python manage.py makemigrations
  • python manage.py migrate

Makemigrations: Makemigrations is responsible for creating new migrations based on the changes you have made to your models.

Migrate: Migrate is responsible for applying and removing migrations.

Run the local server: Now run a local server, then open a web browser to http://127.0.0.1:8000.

That how your website look

Accessing the Django Admin Site

Django created and configured the default admin site for you. All you need to do now is create an admin user(superuser) to log into the admin site. To create an admin user, run the following command from inside your virtual environment.

First, make sure the development server is running, then open a web browser to http://127.0.0.1:8000/admin/. You should see the admin’s login screen.

Conclusion

Django is crowd-tested. It has a big, supportive community accessed through numerous forums, channels, and dedicated websites. Django started off with great documentation, the best of any other open-source framework. And it’s still maintained on a high level, updated along with the new functions and fixes, so you can easily adapt to changes.

You can trust that any issues with the framework will be solved as soon as they arise. The software is constantly updated and new packages are released to make working with Django more convenient than it already is.

--

--