I learned Django in an online course called Udemy. This article is an output page that also serves as a memorandum. I will try to write it in an easy-to-understand manner from a beginner's point of view. The following two courses were used as reference. Both were very good courses that were difficult to attach.
Safe for beginners in programming, Python / Django introductory course [Python 3 x Django 2.0] Learn while making Django
Quite simply, it's like a toolbox that brings together what Python genius programmers need so that even novice programmers like themselves can easily build a website. To put it coolly, it's called a web framework.
In addition to Django, there are web frameworks made with JavaScript, Ruby, etc., and it seems that they are better as of 2020 in terms of share rate. However, in my case, I use Python as a hobby, and the cost of learning basic knowledge is low, so I chose Django.
First, create a virtual environment. If you create this, it will be an independent environment, so by adding the library later, you will not have the foolish situation that "it worked before, but it worked". Machine learning libraries often fall into this situation, so let's do it properly. You can easily switch back.
Start PowerShell or a command prompt. The virtual environment is created with "virtualenv". If it is not installed, install it with the following command.
> py -m pip install virtualenv
Once installed, let's build a virtual environment. Build with an environment name named venv.
> py -m virtualenv venv
Let's log in to the created virtual environment. After moving to the venv folder, type the following command to enter the virtual environment.
> \Scripts\activate
After entering the virtual environment, install Django.
> pip install django
Once Django is installed, create a project.
A project is a toolbox created by the genius Python programmers mentioned earlier. It's packed with everything you need to build a web server.
Thank you very much, but until you get used to this folder structure, it's quite esoteric and confusing. I will write in detail which file you are editing as carefully as possible.
Let's create a project named "first".
> django-admin startproject first
A folder called first will be created, so go to the first folder. After moving, create a web application.
> py manage.py startapp myapp
This will create a new folder called myapp. I think it has the following tree structure. Under the first project folder, there is another first folder, You might think, "Huh?", But if this happens, it's a success. Next, there is settings.py directly under first \ first. Let's open it.
first\fisrt\settings.py
INSTALLED_APPS = [
"...",
"first", # <-Postscript
]
Now I can tell Django that there is an app called first. Scroll down further and modify LANGUAGE_CODE and TIME_ZONE as follows:
first\fisrt\settings.py
LANGUAGE_CODE = 'ja'
TIME_ZONE = 'Asia/Tokyo'
LANGUAGE_CODE means Japanese and TIME_ZONE means Japanese time. This completes the settings in settings.py. Overwrite and save and close.
Then edit urls.py. As the name suggests, it is a file for name resolution of the URL entered from the browser. Open first \ first \ urls.py ↓
After opening the file, write as follows.
first\first\urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('myapp/', include('myapp.urls')) # <-Postscript
]
This will tell you to go to urls.py directly under myapp if you see myapp in the URL. By doing this, even if the number of apps in the same project increases in the future, it will be easier to understand because the URLs will not be scattered in the url patterns.
Next, open the myapp folder and create a new urls.py.
first\myapp\urls.py
from django.urls import path
from . import views
app_name = 'myapp'
urlpatterns = [
path('', views.index, name='index'),
path('test/', views.test_index, name='testindex')
]
Import views.py with from .import views
.
`ʻapp_name ='myapp'means name resolution for the URL of myapp. In urlpatterns, describe the processing for the URL that follows myapp.
path ('', views.index, name ='index') is the processing when the URL
myapp / comes.
path ('test /', views.test_index, name ='testindex') is the processing when the URL
myapp / test`` comes.
I haven't edited views.py yet, so I think the image is hard to understand, but
When the URL myapp /
comes in, the function ```indexin views.py is executed. When the URL
myapp / testcomes, the function of
test_index`` is executed.
This function name is arbitrary. This completes the editing of urls.py. Next, let's edit views.py.
Let's create the index and test_index functions specified in urls.py earlier. views.py is stored below. Open views.py and write as follows.
first\myapp\views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("hello world")
def test_index(request):
return HttpResponse("test")
def index
is a function that just returns "hello world" when views.index receives a request. def test_index
is a function that just returns "test" when views.test_index receives a request.
This completes editing views.py. Let's finally start the development server.
The development server is started with PowerShell. Please move to directly under the project folder you created first. Make sure that manage.py is directly below it. Then execute the following command.
py manage.py runserber
When started, the following result will be displayed. If you get an error, there may be something wrong with the procedure so far.
http://127.0.0.1:8000/myapp and <a href="http://127.0.0.1:" in your internet browser Make sure you can successfully access 8000 / myapp / test "> http://127.0.0.1:8000/myapp/test .
I think that hello world is displayed safely.
The next article is below. https://qiita.com/sw1394/items/903397960d7164ff31ac
Recommended Posts