We'll show you how to build a Django environment and create a simple Hello Word app.
Execute the following command.
$ pip3 install Django
After the installation is completed normally, you can check the version with the following command.
$ python3
>>> import django
>>> print(django.get_version())
This completes the environment construction, isn't it? Now let's make a PJ.
Move to the directory where you want to create the project and execute the following command. Just run it and you should have a directory called mysite and a set of files.
$ django-admin startproject mysite
A utility for performing Django administration tasks. Most tutorials don't use django-admin after project generation, they use manage.py, a thin wrapper around django-admin.
There are two things that manage.py does:
When it's time to touch it for the first time, I think it often doesn't come to the fore. Is manege.py slower because of the extra processing? That's not the case, so I think it's best to follow the tutorial instructions that you refer to when you first touch it.
Now let's take a look at the generated files.
If the project generation command is successful, the project will be completed with the following file structure.
Execute the following command in the generated project directory to start the local server.
$ python3 manage.py runserver
After executing the above command, access the following to display the screen. Easy. http://127.0.0.1:8000/
If port 8000 (default) is used by another service, specify the port as shown below and execute.
$ python3 manage.py runserver 8080
After confirming the default TOP page, let's finally display the screen you made.
Execute the following command on mysite. The app should have been generated with the name firstapp.
$ python3 manage.py startapp firstapp
Under firstapp, there is an easy-to-understand view definition file called views.py. Please make the contents as follows.
views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("The first application I made with Django.")
After defining the view, it's time to set up the routing. Create a file called urls.py under the firstapp app and make the contents as follows.
firstapp/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
After setting the routing of the application, add it to the routing of the route project. Modify urls.py under mysite as follows.
mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('firstapp/', include('firstapp.urls')),
path('admin/', admin.site.urls),
]
After coding, start the server again and try accessing the following. http://localhost:8000/firstapp/
$ python3 manage.py runserver
I think the characters are displayed on the screen. In making a simple screen, you can do it just by understanding the following roles.
From the next time onward, I will introduce what happens if you create a DB to make it more like an application and perform the same procedure in Visual Studio.
Recommended Posts