This time, I will write the procedure to display "Hello world" on the browser with Djnago. If you're new to Django, read the article below and you'll be on your way. Procedure to create application with Django with Pycharm ~ Preparation ~ Django ~ settings.py edition ~
The directories and files of the entire project are as follows.
Pycharm project
|
|___django project directory
|
|___django application directory__templates directory
|
|___manage.py
|
|___Other files
Let's go!
First, go to "urls.py" in your django application directory. Probably something like this is written.
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
Please add this as below.
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('app/', include('todo.urls')),
]
#Here "'app/'In many cases, enter the application name, so enter whatever you like.
By setting "include", if you enter "app /" at the end of the URL, it will jump to "urls.py" of the application. The reason there are two is that separating "urls.py" for the project and the application makes the code easier to read.
Next, write to "urls.py" of the application.
In the first place, I think there are people who say that "urls.py" is a nanja. In a word, it is "an instruction sheet that describes the material of what you want to display on the browser". Imagine a "memo of what to buy" that your mother gave you when you asked for shopping.
It is not prepared by default, so if you have not made it, let's make it.
#Move to application directory
cd application directory name
#urls.Creating a py file
touch urls.py
This is OK.
I just made it and the contents are empty, so let's write it immediately.
from django.urls import path
from .views import HomeView #views.Loading the "HomeView" class from py.
urlpatterns = [
path('home/', HomeView.as_view())
]
#「home/Is what you enter in the URl part.
#「HomeView.as_view()"" Is "home" in the YRL part/By typing "views".Call "HomeView" of py.
Even if it is called views.py, I think it feels like a mess, so I will explain it in the next chapter.
views.py is a place to collect the necessary materials as instructed by urls.py. "View.py" is a child who buys ingredients according to the memo given by his mother.
Let's add it immediately. By default it should look like this:
from django.shortcuts import render
# Create your views here.
First of all. "# Create your views here." Delete this part and add as follows.
from django.shortcuts import render
from django.views.generic import TemplateView #Import of template display specialized
class HomeView(TemplateView): #Urls a while ago."HomeView" class loaded into py
template_name = 'home.html' #Specifying the HTML file to use
Let's edit the HTML file from here. First, change to the templates directory.
cd templates
Create an HTML file in the templates directory.
touch home.html #〇〇.Please create with html
When you create it, the contents are empty, so let's write it. First, press "!" And the Tab key at the same time. Then you will see something like the one below.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>
If this does not come out, please copy it. After that, add + change as below.
<!DOCTYPE html>
<html lang="ja"> #In Japanese
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Hello World</h1># Addendum
</body>
</html>
Now you're ready to go!
Let's do it!
python manage.py runserver
http://127.0.0.1:8000/」
If you click it, you will be taken to the browser. It would be perfect if you could display "Hello World" there!
This time, I introduced the procedure to display "Hello World". Let's experience small things first and grow steadily! I will continue to write articles as output, so please take a look if you like.