Published Date : 2019年3月6日1:04
A quick review of the previous article.
➀ Use the pick django -admin command to create a Django project template on the server.
This time, I will create a blog in Django.
This site is very easy to understand and I recommend it.
I make a project.
Then, please go to the folder that you downloaded and extracted locally.
If you go into the project folder, it looks like this.
Open a command prompt or Terminal in the same location where manage.py is located.
python manage.py runserverのコマンドを打ってください。
Type the command above.
This manage.py is the power behind the Django project.
We often use it.
Note 1 :
The editor uses this.
It's fast, easy to use and free of charge.
In addition to Python, there are code completion features (It's like predictive transformation.) and syntax features (It's easier to see.) for HTML editing.
Just search and download from the extension (Tags on the left side of the editor screen).
Note 2 :
If you don't have Python in your local environment, it won't work.
Take this opportunity to install Anaconda.
You can download an executable from the site that contains everything you need to install Anaconda.
Setup simply clicks on the EXE file and follows the instructions.
The rest of the tutorial is here.
Anaconda is heavy, so it's easier to understand if you only need Python.
python manage.py runserverこんな感じで表示されるので、赤枠の部分に注目してください。
Now, when you type this command, you will see something like this, so look at the red box.
This means that the server is running on port 8000 of the local host.
To try it out, open up your browser and type in the search box.
And when this screen comes up, it's a success.
If you want to change the display to Japanese time, open settings.py and rewrite the location of the red line around line 110 as follows.
Refresh Browser, (Press the Control key and the R key simultaneously) Then you can make sure that the display is in Japanese like this.
As its name implies, settings.py is a Python file that tells the server to set various settings.
This settingspy allows you to set the details. where to place the image, what database to use, what host name to use, etc. (For example, the last domain name)
Back in manage.py, he decides how to spend money on a home.
This settings.py looks like a money manager.
By the way, when I updated the browser, the setting changes are reflected immediately.
This is because the server keeps running and notifies you as soon as a file in the server changes.
You can reply to or read replies immediately while chatting, but with chat turned off, you can't read new replies until you turn it on.
Let's see what happens if we drop the server.
Go back to where you typed the command to start the server and type the command as shown in the red box.
Please press the control key and C on the keyboard at the same time.
"GET/HTTP/1.1" 200. It was like that, but now it's back.
By the way, GET From your browser, say "Please display this." to the server.
HTTPはかんたんにいうとクライアントがサーバー(コンテンツを持っていて、それを提供してくれる)に対して 「件名:提供のお願い。本文:このテキストファイルを下さい。」みたいな通信の決まりごとです。
HTTP is basically a set of rules about how you want your browser to display text.
In short, HTTP allows a client to send a message to a server (Have content and provide it) It is a communication rule like "Subject: Please provide. Body: Please give me this text file.".
1.1 is the version. (It's like Windows 7, 8, 10.)
The last 200 simply means that We was able to communicate with the server successfully.
ブラウザが客なら、サーバーはコンビニ店員。 商品は画像ファイルやHTMLファイル(文章や画像など) HTTP通信は「レジに商品を持っていく。会計が終わる。金額を渡す。商品を受け取る。」 といった商品を買う為のルールです。
If the browser is a customer, the server is a convenience store clerk. Products are image files or HTML files (Text and images, etc.) HTTP communication is "I take the product to the cash register. The check is done. I give you the amount. I receive the goods." It is a rule to buy such products.
I am sorry if the example is hard to understand.
Now, if you drop the server, try refreshing your open browser.(Press the control key and the R key simultaneously)
We can't communicate like this.
It's like going home to see my friend, but he's not home or sleeping, and he can't even get up.
Now let the browser say "Hello, browser."
A folder with the same name exists in the project folder.
This is where the settings.py.
Right-click and select New File to create a file called views.py.
Then copy and paste this code.
from django.http import HttpResponse def index(request): return HttpResponse('こんにちは、ブラウザ。')一行づつ説明していきます。
I'll explain it line by line.
from django.http import HttpResponseDjangoのhttp通信が使える機能から、HttpResponseをインポートしますよー。
Import HttpResponse from Django's http communication facility.
def index(request):関数(色々な命令、こうしろ、ああしろ、といった手順のかたまりをひとつにまとめたもの)
function (a package of instructions and procedures). def is a mean of define.
The function name is index.
As an aside, an index finger is an index finger.
In other words, an index is something "Refer to a standard item".
request is the variable containing the request to be sent to the server. You can pass it to the Index function to use.
I don't seem to use it clearly this time. This request is required for HTTP communication, so if the function does not receive this variable it will fail.
return HttpResponse('こんにちは、ブラウザ。')
Finally, return to the HttpResponse method
Insert the word "Hello, browser." and send it out.
Think of a method as almost the same as your function.
Http is the protocol to communicate, Response is the response.
I was told to make a request (GIVE IT TO ME.), so I took HTTP and (First, shake hands, bow,)
This is an image of a response (Hello, browser.).
Next, find urls.py on the same floor and open the file.
He's a good connecterer.
It looks like this when opened at first.
Import views.py as described above.
The red frame is the part to be added this time.
We will let urls.py know that views.py is in (Floor named mysite) where we are.
from mysite import viewsこれは、
path('index/',views.index)'index/'にアクセスがあったとき、つまり
This means that the index function in views.py will be invoked when 'index /' is accessed, that is, when (http:// 127.0.0.1: 8000/index) is typed into the browser.
python manage.py runserverと打って
Now, save the two files and type
python manage.py runserveragain into your browser as http:// 127.0.0.1/index/ and "hello browser" is displayed.
Next, I will make it more detailed, arrange the display, and make it look like a blog.