We will proceed on the premise of a Mac environment.
Check the Python version as follows.
$ python3 --version
Python 3.5.2
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')),
]
admin
As 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.
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.
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