That's why I decided to study Django in Python. Django seems to be a Python web framework. What is a framework? That's what it looks like, but the official website says "Django makes it easier to build better Web apps more quickly and with less code." It seems that Youtube, Dropbox, and Instagram are also made with Django in famous places. Wow ...
Note) This article is "I tried a tutorial on the official Django project".
First of all, from the environment construction to the rabbit and the corner
I think you can start Django if you keep these three points in check. In my case on Windows 10 1909
I prepared it in. Below are the links that I referred to during setup.
After installing Django, I built a virtual environment. As you can see, the step is quite slow. Please forgive me.
Generally, there are many articles about pyenv
or venv
when building a virtual environment, so when I make it with ʻAnaconda this time It was built with a little confusion. Since the GUI of ʻAnaconda
is fairly easy to understand, it is possible to build a virtual environment without typing any honest commands.
So it may be easy for beginners to get along with.
The following is the setting screen for ʻAnaconda` Navigator.
Create a new environment The name should be easy to understand and you can decide it freely. (I named it Django, but maybe there's a namespace conflict)
Once the virtual environment is created, start the virtual environment from ʻAnaconda` Prompt, enter the working directory, and create a project.
cmd
(base)C:\User>conda activate Django
(Django)C:\User>django-admin startproject mysite
This will create a mysite
directory in which the project and starter kit-like code will be created automatically.
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
Since the tutorial has exactly the same explanation, I will omit it in detail, but the basic movement is controlled by manage.py
, and ʻurls.py` is responsible for routing within the site.
Detailed article ⇒ Explanation of the mechanism of code for django-django (framework) in an easy-to-understand manner [Thoroughly chew]
First, let's run the web server!
cmd
(Django)C:\User\mysite>python manage.py runserver
You have successfully set up a server. As those who understand it will understand, the one set up here is a so-called trial server on the development server. Actually, ʻApache` etc. is put in and operated.
So let's check the development server we set up. If you enter 127.0.0.1:8000 in your browser It's okay if you see a screen with a rocket like this flying. 127.0.0.1:8000 represents your own port 8000 because 127.0.0.1 is the loopback address, and this page shows that django is running on port 8000 in your computer. I will.
In the tutorial, we will start creating a voting application here. First, create a template for your application.
cmd
(Django)C:\User\mysite>python startapp polls
This will create a directory named polls, which already contains the basic scripts for building your application.
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
This script called views.py
controls the appearance of the page.
Now, let's play with this views.py
a little to change the appearance.
From here, we will edit using vscode.
In vscode, I have included Japanese localization and Python extensions as plugins, and Pylance, which I introduced because it was a hot topic recently.
When you open the vscode screen, it looks like this.
Add the following script to views.py
.
from django.http import HttpResponse
def index(request):
#I put in the appropriate words
return HttpResponse("Hello!")
The content of HttpResponse
of return
is different from the tutorial, but since this is a display message, I put in whatever I like.
Then set the routing in ʻurls.py` to display this function.
in the polls directory. Be careful because there is also ʻurls.py
in the directory hierarchy of mysite. Here, it is written in the deeper ʻurls.py`.polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
I'm calling views.py
to put the path
to views.index
in the list of ʻurl patterns. This alone cannot call
views.indexyet. You need to show the routing to polls in
mysite / urls.py`.
mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
admin is originally included.
Added path
to polls.
ʻInclude is a function needed to pass the action to the lower ʻurls.py
.
The exception is ʻadmin.site.urls. You can now look up
views.index on
127.0.0.1:8000/polls`.
Reference about URL dispatch ⇒ Study of DJango (1)
Let's check it immediately.
It's pretty lonely, but it feels good because the response is as per the code.
Set the database to be used in mysite / setting.py
.
Thankfully Python comes standard with a small database called SQLite
.
These places also show the philosophy of Python's battery attachment.
cmd
(Django) C:\User\mysite>python manage.py migrate
This completes the database table creation. I'm surprised that it's too ridiculous, but is this a good thing about Django?
When using a database other than SQLite
, a separate setting in setting.py
is required.
Create the core model of the polls application.
polls/models.py
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
As you can see from the code, we have defined two subclasses of the class models.Model
, Question
and Choice
.
In addition, the attribute is set to Field
.
I haven't caught up with Field
yet. I will devote myself.
models.ForeignKey
is the one who goes to get the external key. I'm sorry.
After creating the model, the next step is to enable the model in setting.py
so that it can be referenced.
mysite/setting.py
INSTALLED_APPS = [
'polls.apps.PollsConfig', #Add this line
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
By the way, what is polls.apps.PollsConfig
?
It was this ↓
polls/apps.py
from django.apps import AppConfig
#I'm calling this.
class PollsConfig(AppConfig):
name = 'polls'
And when you finish setting to setting.py
,
cmd
(Django) C:\User\mysite>python manage.py makemigrations polls
I was able to save the model changes by doing makemigrations
migrate
.
It's getting longer, so I'd like to cut it once here. Thank you very much. If you notice a mistake, correct it accordingly.
Recommended Posts