Easy-to-understand explanation of Python web application (Django) even for beginners (6) [MTV design pattern completion]

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

Cooperation between Model and Template

I will write about cooperation such as reflecting the created Model information on the Template side and operating the Model on the Tamplate side. After completing this chapter, you should have a rough idea of the skill sets required for web applications. As the next step, you can release the actual service once you can design it and deploy it to production.

Edit View

Edit the View that links the Model and Template.

blog/views.py


from django.shortcuts import render
from django.utils import timezone
from .models import Post

# Create your views here.
def post_list(request):
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    return render(request, 'blog/post_list.html', {'posts': posts})

I introduced the first import for Django shell, so it's okay. By the way, `.``` in .models``` represents current directory , or ``` current application `` .

.models is blog.Same as models.



 The important point is here

```py
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    return render(request, 'blog/post_list.html', {'posts': posts})

I'm passing the variable `` `posts``` that references the queryset to the Template via render. In this way, the variables declared in View can be referenced in Template. This is the key to the integration of Model and Template.

Edit Template

It's very simple, but let's take the posts passed to the Template on the View side and display it on the Template side.

blog/templates/blog/post_list.html


<html>
    <p>Hi there!</p>
    <p>It works!</p>
    {{ posts }}
</html>

As you can see, the part written in python should be enclosed in double brackets of ``` {{}}` ``.

By the way, of course you need to start the server, and don't forget the command to start the server?

Start the server


$ python3 manage.py runserver

It's okay to access the local server at http://127.0.0.1:8000/, right?

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

Did you get a familiar query set in Django shell like this?

Let's display this as a list.

blog/templates/blog/post_list.html


<html>
    <p>Hi there!</p>
    <p>It works!</p>
    <ul>
      {% for post in posts %}
        <li>{{post}}</li>
      {% endfor %}
    </ul>
</html>

It seems that the control syntax of for and ʻif should be enclosed in {%%} `. If the following is displayed, it is successful.

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

The title of the article is displayed. Since it's a big deal, let's edit the Template side so that not only the title but also the content of the article can be displayed.

blog/templates/blog/post_list.html


<html>
  <!-- title -->
  <div>
    <h1>
      <a href="/">
        Qiita:Django sample
      </a>
    </h1>
  </div>

  <!--contents of post-->
  {% for post in posts %}
    <div  style="margin-top:50px;">
      <h2>title:<a href="">{{ post.title }}</a></h2>
      <p>published: {{ post.published_date}}</p>
      <p>{{ post.text | linebreaks }}</p>
    </div>
  {% endfor %}
</html>

| linebreaks means to pass a filter that converts line breaks in the text into paragraphs. It's convenient.

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

This concludes the practice of linking Model and Template via View.

How was it going to finish so far? Perhaps you can see the parts that make up the website. Maybe you can study this much and only do this? I think some of you may have thought that. If you are such a person, don't worry. If you study designing with HTML and CSS, you can finish this web page beautifully at once.

It's not gorgeous in appearance, but it's a tough and necessary job to create one position called a server-side engineer just by linking this MVT.

We hope this article is a start for creating web applications with Django.

Advanced version

The world's easiest way to explain how to make a LINE BOT (1) [Account preparation]

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 (6) [MTV design pattern completion]
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 (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]
Easy-to-understand explanation of Python Web application (Django) even for beginners (5) [Introduction to DB operation with Django shell]
[Python] Web application design for machine learning
Beginners use Python for web scraping (1)
Beginners use Python for web scraping (4) ―― 1
Explanation of creating an application for displaying images and drawing with Python
[Python] Minutes of study meeting for beginners (7/15)
[For beginners] Try web scraping with Python
Easy understanding of Python for & arrays (for super beginners)
[Python] Beginners troubleshoot while studying Django web applications
Basic story of inheritance in Python (for beginners)
Beginners can use Python for web scraping (1) Improved version
(Python) Try to develop a web application using Django
Summary of pre-processing practices for Python beginners (Pandas dataframe)
Web application made with Python3.4 + Django (Part.1 Environment construction)
Explanation of NoReverseMatch error in "python django super introduction"
Beginners use Python for web scraping (4) --2 Scraping on Cloud Shell