Easy-to-understand explanation of Python web application (Django) even for beginners (4) [Routing settings / Introduction to MTV design patterns]

Composition of commentary articles

No. title
1 Easy-to-understand explanation of Python Web application (Django) even for beginners (1) [Environment construction]
2 Easy-to-understand explanation of Python Web application (Django) even for beginners (2) [Project creation]
3 Easy-to-understand explanation of Python Web application (Django) even for beginners (3) [Application creation / DB setting]
4 Easy-to-understand explanation of Python web application (Django) even for beginners (4) [Routing settings / Introduction to MTV design patterns]
5 Easy-to-understand explanation of Python Web application (Django) even for beginners (5) [Introduction to DB operation with Django shell]
6 Easy-to-understand explanation of Python web application (Django) even for beginners (6) [MTV design pattern completion]

Development environment

We will proceed on the premise of a Mac environment.

Check the Python version as follows.

$ python3 --version
Python 3.5.2

Creating a web page

Routing settings

mysite/urls.Management screen with py(admin)And set the routing (URL) for the blog.




#### **`mysite/urls.py`**
```py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'', include('blog.urls')),
]

adminAs for, django admin has set it automatically, but I haven't created anything for blog yet, so I will set it.

blog/urls.Create an empty file called py and edit it as follows.




#### **`blog/urls.py`**
```py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.post_list),
]

For urlpatterns,[Django Girls Tutorial](https://djangogirlsjapan.gitbooks.io/workshop_tutorialjp/content/django_urls/)More excerpts.



 > This means that you have assigned a URL with the pattern ^ $ to a view called post_list.
 What does ^ $ mean?
 It's the magic of regular expressions :) Let's break it down:
>
 * In the regular expression, ^ means "start of string". Start pattern matching from here.
 * $ Means "end of string" and ends pattern matching here.
>
 Yes, an empty string matches the pattern of these two symbols side by side. That said, Django's URL name resolution does pattern matching except for'http://127.0.0.1:8000/', so this pattern means'http://127.0.0.1:8000/'itself. I will. In other words, you are specifying to return `views.post_list` to users who access the URL'http://127.0.0.1:8000/'.

 In short, it currently declares that ```urlpattern``` assigns `` `post_list``` to a URL (root directory) with no strings.
```post_list```I haven't created the view yet, so of course I can't access it.

# Django design patterns

 Now, let's create a display screen that corresponds to the URL you set, but first you need to know how the screens and databases that are displayed in Django are managed.

 Before we talk about Django, let's take a look at MVC, a typical design pattern.
 If you understand MVC, it will be versatile in the future, and you can easily understand the MTV design pattern adopted by Django.

## MVC design pattern used in Ruby on Rails etc.

 In web applications, it is often managed as an MVC design pattern.
 MVC is an abbreviation for Model View Controller, and each role is as follows.
 Isn't Ruby on Rails a classic example of this MVC design pattern?

 ![](https://blog.chattyhive.com/wp-content/uploads/2014/01/mvc_detailed-full.png)

 Based on the information accessed by the user, the Controller acts as an intermediary, pulling out the necessary information from the Model and View, and returning them to the user together.
 Roughly speaking, View is HTML, Model is DB, and Controller is the administrator.
 Such a classic MVC design pattern is very easy to understand and handle once you get used to it.

## Django is an MTV design pattern

 It seems that Django manages with a design pattern called MTV design pattern instead of MVC.
 * Is this area like a religious war?

 MTV is an abbreviation for Model Template View.

 ![](http://blog.easylearning.guru/wordpress/wp-content/uploads/2015/08/MVC-Vs-MTV-300x178.png)

 I think there are various reasons, but I personally think that the MVC design pattern is fine.
 Basically, MVC and MTV seem to have a one-to-one correspondence, so be sure to convert from MVC to MTV in your head.

## MTV Reference Articles
 * [I tried Python + Django. About MTV Model](http://mimumimu.net/blog/2011/11/21/python-django-%E3%81%97%E3%81%A6%E3%81%BF%E3%81%9F% E3% 80% 82-mtv-model-% E3% 81% AB% E3% 81% A4% E3% 81% 84% E3% 81% A6 /)
* [Implementing MTV model in Python Django](https://www.google.co.jp/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&ved=0ahUKEwiQ--nhgaDQAhVLfLwKHW23BWEQjhwIBQ&url=http%3A%2F%2Fblog.easylearning.guru%2Fimplementing-mtv-model-in-python-django%2F&psig=AFQjCNFX3N_Iwfe29CaOZ8djG0oa6V4_2A&ust=1478929512330040)


# View settings

 View controls the part such as which Template is read from the set URL and what kind of information is passed to Template.
 * In MVC design patterns such as Ruby on Rails, this is the part called Controller.


#### **`blog/views.I will make py.`**
```I will make py.



#### **`blog/views.py`**
```py

from django.shortcuts import render

# Create your views here.
def post_list(request):
    return render(request, 'blog/post_list.html', {})

Since the URL above was set to access the method `post_list```, define def post_list``` and take `` request``` as an argument in it. I am.

In the render method, blog/post_list.The contents of the request received as an argument are output using a template file called html.


 It is easy to manage if the method name and template file name match.
 * Ruby on Rails basically assumes that the method name and template file name match, and does not bother to write `` `render```.

# Creating Template

 Now let's create a template file.
 First, create a directory for Template.

```bash
$ mkdir blog/templates
$ mkdir blog/templates/blog

In this, create `` `post_list.html```.

blog/templates/blog/post_list.html


<html>
    <p>Hi there!</p>
    <p>It works!</p>
</html>

After saving this, if you access http://127.0.0.1:8000/, the template will be loaded and work as follows.

スクリーンショット 2016-11-11 3.35.33.png

As for how to write HTML, there are many other references, so I will leave the explanation there.

Now you can display the web page for the original template with the URL.

Next commentary article

Explanation of Python Web application (Django) in an easy-to-understand manner even for beginners (5) [Introduction to DB operation with Django shell]

References

bonus

We are waiting for you to follow us!

Service introduction Please feel free to contact us if you are interested in "Kikagaku", a one-on-one tutoring service for machine learning that allows you to learn "math-> programming-> web applications" all at once.

Recommended Posts

Easy-to-understand explanation of Python web application (Django) even for beginners (4) [Routing settings / Introduction to MTV design patterns]
Easy-to-understand explanation of Python Web application (Django) even for beginners (5) [Introduction to DB operation with Django shell]
Easy-to-understand explanation of Python Web application (Django) even for beginners (2) [Project creation]
Easy-to-understand explanation of Python Web application (Django) even for beginners (1) [Environment construction]
Easy-to-understand explanation of Python Web application (Django) even for beginners (3) [Application creation / DB setting]
[Python] Web application design for machine learning
Updated "Introduction to Python Web Application Homebrew for Slow 3rd Year Web Engineers"
Updated "Introduction to Python Web Application Homebrew for Slow 3rd Year Web Engineers"
Updated "Introduction to Python Web Application Homebrew for Slow 3rd Year Web Engineers"
Updated "Introduction to Python Web Application Homebrew for Slow 3rd Year Web Engineers"
Updated "Introduction to Python Web Application Homebrew for Slow 3rd Year Web Engineers"
Updated "Introduction to Python Web Application Homebrew for Slow 3rd Year Web Engineers"
[Introduction to Udemy Python 3 + Application] 26. Copy of dictionary
[Introduction to Udemy Python3 + Application] 43. for else statement
(Python) Try to develop a web application using Django
[Introduction to Udemy Python3 + Application] 53. Dictionary of keyword arguments
[Introduction to Udemy Python3 + Application] 52. Tupleization of positional arguments
Explanation of NoReverseMatch error in "python django super introduction"
[Introduction to Udemy Python3 + Application] 42. for statement, break statement, and continue statement
■ Kaggle Practice for Beginners --Introduction of Python --by Google Colaboratory
[Explanation for beginners] Introduction to convolution processing (explained in TensorFlow)
[Python] Introduction to graph creation using coronavirus data [For beginners]
Introduction to Python Django (2) Win
Design Patterns in Python: Introduction
~ Tips for beginners to Python ③ ~
Introduction to Python For, While
Summary of Chapter 2 of Introduction to Design Patterns Learned in Java Language
Chapter 4 Summary of Introduction to Design Patterns Learned in Java Language
Summary of Chapter 3 of Introduction to Design Patterns Learned in Java Language
[Introduction to Udemy Python3 + Application] 69. Import of absolute path and relative path
[Introduction to Udemy Python3 + Application] 12. Indexing and slicing of character strings
Python techniques for those who want to get rid of beginners
A super introduction to Django by Python beginners! Part 2 I tried using the convenient functions of the template
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments
[Introduction to Udemy Python 3 + Application] 57. Decorator
[Introduction to Udemy Python3 + Application] 59. Generator
Beginners use Python for web scraping (1)
[Introduction to Udemy Python 3 + Application] Summary
Beginners use Python for web scraping (4) ―― 1
An introduction to Python for non-engineers
Introduction to Python Django (2) Mac Edition
[Introduction to Udemy Python3 + Application] 47. Process the dictionary with a for statement
[For beginners] Super introduction to neural networks that even cats can understand
Take the free "Introduction to Python for Machine Learning" online until 4/27 application
Python beginners publish web applications using machine learning [Part 2] Introduction to explosive Python !!
[Introduction to Python] Thorough explanation of the character string type used in Python!
An introduction to self-made Python web applications for a sluggish third-year web engineer
Explanation of creating an application for displaying images and drawing with Python
Introduction to Graph Database Neo4j in Python for Beginners (for Mac OS X)
A super introduction to Django by Python beginners! Part 5 I made a super simple diary application with a class-based general-purpose view