Continued application addition
Django's request processing flow is as follows. This time, only the routing view is used, and "Hello, World!" Described in the view function is displayed. Float is a request from the browser-> project1.urls-> posts.urls-> views.index.
Decide which file to call from the URL pattern that received the request from the browser and the pattern described in the routing file. There are two urls.py in the app and in the project, each defining the behavior distribution in the app and in the project.
Returns the behavior of the function called by routing. If it is necessary to call data from the database, describe the description to access the database. Also, describe the operation of processing the data into the required form and passing it to the template.
Write a function that returns Hello, World! When a request is received in views.py in the posts folder in the project1 folder created this time.
views.py
from django.shortcuts import render
from django.http import HttpResponse #Add this line
def index(request): #Add this line
return HttpResponse("Hello, World!") #Add this line
Next, create urls.py in the posts folder and define it in the application (posts).
app name/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Finally, edit urls.py in the project1 folder.
Project name/urls.py
from django.contrib import admin
from django.urls import path,include #add include
urlpatterns = [
path('admin/', admin.site.urls),
path('posts/', include('posts.urls')), #Add this line
]
Start the server, access "http://127.0.0.1:8000/ app name /" (posts in this case), and go to "Hello, World". If "!" Is displayed, it is successful. Template addition
Recommended Posts