We will proceed on the premise of a Mac environment.
Check the Python version as follows.
$ python3 --version
Python 3.5.2
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 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.
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?
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.
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.
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.
The world's easiest way to explain how to make a LINE BOT (1) [Account preparation]
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