Tutorial goal: Create a Django project, a simple view and return a simple response
Who this tutorial is for?
In this tutorial series, we are going to make a step by step tutorial about Django fundamentals. This tutorial is a complete beginner’s guide to start learning Django. We've tried to make it as simple as possible. For this purpose, the concepts are divided into 10 parts. If you are a beginner the development world may seem like an endless road, but it all depends on how to learn and practice the basics.
Django is a Python-based web framework that is specifically engineered to help developers build and construct powerful web applications quickly and efficiently. The core of the Django framework works with Python versions from 2.3 to 2.7. The framework was named after famous guitarist Django Reinhardt.
A framework is a toolbox of components. The reason for using a framework is to prevent written duplicate code for similar tasks. A framework is a library combined with a preset way to organize your code. It's reusable code bundled in a reusable (but often inflexible) architecture.
Perhaps the best part about Django is its outstanding documentation. Once you learned the basics, You can use the advanced topics because Django has very thorough and readable docs available online.
Django takes much of the tedious work and repetition out of the process, solving questions such as project structure, database object-relational mapping, templating, form validation, sessions, authentication, security, cookie management, internationalization, basic administration, and an interface to access data from scripts.
Create a Django project
Creating a Django project in doprax is quite easy. In doprax you can create your first project by clicking on the create a new project button in the dashboard after sign in your account.
Create a new Django project step 1
Click on next to determine how much resources you want to allocate to your project. You can always adjust the hardware resources of your project. Then automatically a whole Django project will be created for you.
Create Django project step 2
Here is your first Django project on the dashboard. You can also see your project details like the type of your project and database and operating system which will be discussed later and no need to worry about them right now.
The new project is added to the project list
Open project in editor
let's open our project in the editor. By clicking on open in editor button you can edit your project in the doprax editor. Here is a directory of our project in editor.
When you first create a Django project there are some basic settings that you need to do. But in doprax, all the initial settings and configurations are automatically done to your project. All we need to is run our project to see an online working Django website.
What is editor?
Doprax editor is an online IDE(integrated development environment) for developers to start coding and make it online. The only requirement is a web browser. There are several sections in the editor. Main menu, toolbar, sidebar, and text editor.
On top of the editor, there is a menu and a toolbar. Also, there is a link to the project URL. Each project is automatically assigned a subdomain with https. The ► button will run the project. Click on it and the project will start building and then it will be launched. when the run process is done, the website will be accessible via the link on the right side of the toolbar. The stop button will stop the execution of the project.
In this menu, you can see 2 options: projects and deploy. By clicking on “projects” a drop-down menu would be open and you can see 3 options:
- run project
- stop project
- domain setting
- computer setting
You can also on this menu have access to run and stop the project, domain setting, and computer programming. if you want to read more please click here Editor. The sidebar shows a directory pf your projects, services and...etc. In the directory section in the sidebar of the editor, a built-in text editor will be opened on the right side of the screen. After you finish editing each file don't forget to click on the save button to save the changes that you have made. Here is a thorough screenshot of the editor.
Click on the run (►) button. The log at the button of the screen shows you logs From the current process for example when you are building your project or when you're running project creates a log message.
log & building
The Django building usually takes 1 minute and the log shows a successfully built message as you can see in the picture. Click on URL at the top of the page and here you go! the welcome page of Django.
Django welcome page
Structure of a Django project
On the Django framework, any website that is to be developed is called a "project." Also, a project consists of a set of "Applications". An application is also a collection of code files that depend on MVT architecture. To make this clear, let me give you an example. Suppose you want to make a website. In this case, the website will be the project and like the main part of you the website like "blog, payment" will form the app's applications. Such a structure allows developers to use developed applications between different projects Since each application is independent of other applications.
Each app should do one thing, so whenever you're developing your apps and can't explain what it does in one sentence, you need to break it up more.
Django follows the model - template-view (MTV) architectural pattern. The MVT (Model View Template) is a software design pattern. It is a collection of three important components:
Django MVT model
In general, a Django project consists of the following files.
mysite __init__.py manage.py settings.py urls.py
The explanation of the above files is as follows:
__init__.py: A file required by Python, This file tells python to view this directory(my project folder) as python packages. Also, the above file is an empty file, and usually, you will not add anything inside it.
manage.py: One of the advantages of the command line is that it allows you to interact with this project in different ways. In doprax by clicking on "open in shell" button in the dashboard, you can access the running development environment. It allows you to do things like creating users for your website and access to the database. You should never edit or add anything to this file. just kind of ignore it.
settings.py: This is a file that contains all of the Settings/configurations for your entire website. Take a look at it to see the available settings, along with the default values.
urls.py: To put it simple url.py is like a table of your content for the Django website. This module is a pure Python code. Basically this file looks at whatever user requests and performs some functionality.
create a simple view
Django View is one of the essential participants of the Model View Template patterns as we discussed previously. This section acts as a bridge between the MODEL and TEMPLATE, in which all the logic of the project is located. Inside your Django project folder, there is a filename views.py. They are just Python functions. These functions take a Web request and return a Web response and also take the required information from the models and send it to the template. A view is the part of the program where the "logic" of the program is placed.
Each Django view performs a specific function and has an associated template. In other words, views are responsible for interpreting user requests, transforming or fetching data associated with their requests, and passing along the proper response to the template which communicates the outcome. When you visiting a page on the internet or click on some links, the browser sends a request. It contains things including headers, parameters, HTTP methods, and sometimes data.
Write our first Hello World!
Let's create a simple hello world view to show you how views in Django work. First, we need to define our function with the
Def is a python registered keyword that always takes requests and returns a response. There's nothing special about choosing names for your functions, but it's a good idea to name it something meaningful to make your code more legible for other developers.
In every website, each of the requests coming to the server has a response. For example, if a user clicks on "About" link in doprax.com. that's a request the user sends to the server and for that request, there is a view(function) that sends a proper response to the browser relatable. This response can be HTML contents of a Web page or a redirect. It also can be a 404 error, or an XML document, or an image. Depending on the request you send different responses will send back to you. Considering this issue, a view's only purpose is to parse requests to serve a proper response.
The example above was a very simple example of how views work. Most of the time, views do more complex things.
from django.shortcuts import render from django.http import HttpResponse def hello_world(request): return HttpResponse("Hello World!")
URLs in Django
URL stands for Uniform Resource Locator. As we mentioned before URL is like the table of content of your website. Every page on the Internet needs its own address. It means whenever you create a view, it needs to be mapped to a URL or address. Each view is called by a URL written in the urls.py file inside my app folder. Each URL consists of two main parts. The first part is the URL pattern and the second part is the name of a view function or class that is called to execute. For example when a user clicks "my-websitenhjg.com/hello" in the browser. Our first part of the URL is "hello" and the second part refers to functions(hello_world) that send the proper response associated with "my-websitenhjg.com/hello" address.
from django.contrib import admin from django.urls import path from . import views from django.contrib.staticfiles.urls import staticfiles_urlpatterns urlpatterns = [ path('', views.default_index), path('hello',views.hello_world), ] urlpatterns += staticfiles_urlpatterns()
After creating our URL, let's run the project to see the result. It's official you created your online Django website. You can add more functions and URLs to this project as you go further.
This is the end of part 1. Thank you for reading.