No. | title | |
---|---|---|
1 | To myself as a Django beginner (1)-Project app- | |
2 | To myself as a Django beginner (2)-What is MTV- | |
3 | To myself as a Django beginner (3)-Hello world!- | ☆ |
4 | To myself as a Django beginner (4)-Memo app creation- |
Last time touched on how MTV works. From this time on, I will finally write the code!
This time, I'm going to use Django to do "Hello world!", A road that cannot be avoided in programming learning. The goal this time is to understand the connection of ʻurls.py --
views.py` through "Hello World!".
First of all, open views.py
in app1.
myapp/
manage.py
myapp/
__init__.py
asgi.py
settings.py
urls.py
wsgi.py
app1/
migrations/
__init__.py
__init__.py
admin.py
apps.py
models.py
tests.py
views.py <-Here it is!
I think that the contents of views.py
are as follows by default.
from django.shortcuts import render
# Create your views here.
Import what you need to display "Hello World!" Here and define ** View **.
from django.shortcuts import render
from django.http import HttpResponse # Add
# Create your views here.
def hello (request): #View definition return HttpResponse ('Hello world!')
HttpResponse is read and literally, and the inside of () is returned to the screen. The preparation of ** View ** is complete. Then set the URL.
The project has ʻurls.py` by default, but the app doesn't. So I will make it myself.
myapp/
manage.py
myapp/
__init__.py
asgi.py
settings.py
urls.py
wsgi.py
app1/
migrations/
__init__.py
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
urls.py <-add
Import views
and set to associate with URL.
app1/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.hello)
]
Defining a URL in this way and allocating processing according to it is called ** routing **.
Then connect myapp / urls.py
to ʻapp1 / urls.py.
myapp/urls.py`
from django.contrib import admin
Added from django.urls import path, include # include
urlpatterns = [
path('admin/', admin.site.urls),
Connect to path ('', include ('app1.urls')), # app1 / urls.py ]
At this point, preparations for "Hello World!" Are complete!
Now let's start the server and access http://127.0.0.1:8000/
!
I wonder if everyone is doing "Hello world!"
As you may have noticed, I haven't used ** Model ** and ** Template ** this time. This is because ** Model ** and ** Template ** are not always necessary when the response ("Hello World!") Is completed in views.py
like this time. .. This time, I did not use ** Model ** and ** Template ** to verify "Hello World!", But when actually creating a web application, ** Model - Template - View ** is almost always needed. If you're worried about ** MTV **, go back to Last article each time.
--Defining a URL and allocating processing according to a request is called ** routing **.
--There is no ʻurls.py in the app by default, so create it yourself and include it in your project's ʻurls.py
.
Next time will create a simple memo app as a culmination of the past!
Recommended Posts