Django framework for beginner

Tram Ho

Like the previous post, I went to learn the basics of python’s basic components. If you have not seen it, see here . For this part, I will go to a framework for the website called Django. Let’s go.

First indispensable is a basic introduction to Django

Django is a popular web framework written entirely in the Python language. It is a framework with all the libraries, modules to support web-developers. Django uses the MVC model and is developed by the Django Software Foundation ( DSF an independent non-profit organization). Django’s main goal is to simplify the creation of complex websites that use databases. Django focuses on the “reusable” and “self-run” features of the components, the rapid development, and no reworking of what was already done. Some of the popular websites built from Django are Pinterest, Instagram, Mozilla, and Bitbucket.

According to the homepage:

Django makes it easier to build better Web apps more quickly and with less code

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 possibilities shared on the homepage are:

  • Ridiculously fast: Django was designed to help developers take applications from concept to completion as quickly as possible.
  • Reassuringly secure: Django takes security seriously and helps developers avoid many common security mistakes.
  • Exceedingly scalable: Some of the busiest sites on the Web leverage Django’s ability to quickly and flexibly scale.

How to install the django framework

The prerequisite is to install python first (This is obvious, you kidding me =)))

Next, we will install Django Framwork:

-Global setting:

Here, I just choose a new version to use (python3). after installation it looks like this (I install it as django-3.1.1):

-Installed with Virtualenv

This is the most flexible way to install Django. Vitualenv is a tool that allows us to primer a virtual Python environment, where we assume it looks like the real environment, where you can install any python package without affecting the real environment on the machine. This allows us to add packages on each project without the fear of disturbing other projects.

Setting:

This will install a standalone version of Pyhton, pip into a separate directory structure called newenv. To install packages into this virtual environment, we must be active:

If you want to exit this environment use the following cmd:

Like this configuration is successful:

Then install the framework and other necessary packages (if needed):

Take care not to name the project django or test

The structure of the directory

After installing django successfully we will have a new directory helloworld . We have the project directory structure:

The functions of project files:

  • __init__.py is an empty file specifying that this folder path will be treated as a Python package.
  • settings.py is the file that contains the project’s settings. The file contains basic settings such as DEBUG, ALLOWED_HOSTS, INSTALLED_APP, DATABASES, …
  • urls.py is the file that declares the URL of the project.
  • wsgi.py is the file used to deploy the project to the server.
  • manage.py is the file to create the app, migrate, …

Now let’s create first app

Run the following command in the same directory as the manage.py file.

In this command, I will give the port at port 8080 (because the port is identical to the other project, you may not need it)

And this is the result:

If you pay attention to the command, there will be 18 warnings:

Currently in the project, I have not set up the necessary things, the goal is just to learn the structure, I may not pay attention to these waring, because later on when doing larger projects, it will be installed. But if you feel uncomfortable, you can always set up:

After that there will be no more warning:

Create your first app

To create the first app we run the following command:

In the helloWorld directory will add a directory named myfirstapp, which has the following structure:

Files:

  • admin.py is a configuration file for the app
  • apps.py is a configuration file for the app
  • migrations
  • models.py is the place to define the database models
  • tests.py where to write tests
  • views.py is where the request / responce logic is processed

Add myfirstapp to the helloworld file:

Then we will create a file that contains information about the url of myfirstapp:

And add the following:

Then configure the url of the helloWorld / urls.py file to this file:

We will proceed to edit the display view, edit the file myfirstapp / view.py – is the place to handle logic request / responce

Then we run the server again to see the results:

And here is the result:


Above is what I am learning about the django framework for web development with django python. Thank you for your interest.

Share the news now

Source : Viblo