This time, we'll use Django to create a blog app that allows you to post articles.
I will introduce it in series, but first I will create it locally and then configure it with Docker, deploy it on AWS, and make a series of automatic test and deployment with CircleCI.
Django is a web application framework that you can implement in Python.
A framework is software that is a collection of functions used when developing an application. By introducing the framework, you can proceed with the web application efficiently.
This time we will create a blog application, but with Django you can easily create high-quality web applications such as content management systems and wikis, social networks, news sites, etc. using databases with a small amount of code. A simple web application can be created in minutes. Of course, you can extend the functionality to create complex web applications.
Django is also used in well-known web apps such as Instagram, and has become a notable framework equivalent to Ruby on Rails in Ruby.
First, create a working directory. The name here can be anything, but for now I'll leave it as a blog.
mkdir blog
cd blog
If you do not pollute the base environment, you can eliminate the influence of other modules, so let's build a virtual environment using pipenv.
pip install pipenv
#Depending on the environment, you may not be able to use pip directly, so python3-Install with m pip install pipenv
After installation, execute the following command in the working folder.
pipenv shell
After execution, when you can enter the virtual environment, the character string according to the directory name will be displayed at the beginning of the command line.
(blog)bash-3.2$
You can also run pipenv install django directly to install Django, Considering using Docker later, create a file called requirements.txt directly under the working directory. I will describe the required modules.
Right now I only need Django, so I'll write it along with the version information as follows:
requirements.txt
Django==3.1.0
Install the module based on requirements.txt by executing the following command. Only Django should be installed here.
pipenv install -r requirements.txt
Run directly under the blog directory
django-admin startproject mysite
I think that the file structure directly under the parent project looks like this.
├── Pipfile
├── Pipfile.lock
├── mysite
│ ├── manage.py
│ └── mysite
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── requirements.txt
It has a function to start the development server. (If you've done Ruby on Rails, rails -s should be easy to understand.)
To do this, it is more convenient to go to the directory where the manage.py file is located, so go to the mysite / mysite directory before running it.
cd mysite
python3 manage.py runserver
If it can be executed normally, the following output will be output.
October 16, 2020 - 21:30:23
Django version 3.1, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
In addition, this part is a message that is output because the integration process to the database called migrate has not been completed, but for now it is okay to not worry about it.
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
Now that Django's development server has started, check it from your browser. Type 127.0.0.1:8000 in the address bar of a browser such as Chrome and press Enter.
This is the beginning of everything. If you see this screen, the first step is complete! Congratulations.
By the way, when you accessed the test server earlier, it was written in English. These settings can be set in mysite / settings.py, and by default the language display and time zone are adapted to the English-speaking world.
mysite/settings.py(before)
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
I'll change this as follows.
mysite/settings.py(after)
LANGUAGE_CODE = 'ja'
TIME_ZONE = 'Asia/Tokyo'
If you access the test server again, you will see that it is written in Japanese. (Although it cannot be confirmed from this screen, the time zone is also set to Tokyo)
I created a project with the start-project command earlier, but next I will create an application. It's easy to get confused, but the underlying project and the individual apps are different.
This is the explanation on the official page.
What is the difference between a project and an app? An app is a web application that does something, such as a weblog system, a database of public records, or a small voting app. A project is a collection of specific website configurations and apps. A project can contain multiple apps. The app can exist in multiple projects.
Now let's create a blog app. Execute the following command directly under the mysite project (where the manage.py file is located).
python3 manage.py startapp blog
The current directory structure looks like this.
.
├── db.sqlite3
├── manage.py
├── mysite
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── blog
├── __init__.py
├── admin.py
├── apps.py
├── migrations
│ └── __init__.py
├── models.py
├── tests.py
└── views.py
You can see that various files are created under the blog.
Now we need to tell the project that this app was created.
There is a column called "INSTALLED_APPS" in mysite / setting.py, so let's tell you the existence of the blog app in it.
mysite/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog.apps.BlogConfig', #Add here
]
First, "change each template.
The template is the part that creates the look and is the equivalent of an html file.
Create a templates folder directly under the mysite project, a blog folder under it, and index.html under it.
.
├── blog
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── db.sqlite3
├── manage.py
├── mysite
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── templates
└── blog
└── index.html ← Create here
The contents of index.html are appropriate for the time being.
index.html
<h1>Hello, from Django!</h1>
You also need to tell the project where you created the templates folder. Just like when you set INSTALLED_APPS, put the following in settings.py.
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], #Fix here
'APP_DIRS': True,
Next, modify views.py. Call index.html, the template you just created.
blog/views.py
from django.views.generic import TemplateView
class IndexView(TemplateView):
template_name = 'blog/index.html'
Next, create a routing setting specifically for the blog app. Routing settings are set in a file called "urls.py".
First of all, set the routing in urls.py, which controls the routing of the entire project, and urls.py in the app.
First, edit from urls.py directly under mysite.
mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('blog/', include('blog.urls')),
path('admin/', admin.site.urls),
]
You will load the urls for the blog app that you will create later in the urlpatterns.
In, urls.py is created only directly under mysite, but you can also create "urls.py" directly under blog. You can use an editor, or you can execute the following command in the blog directory of the app.
/blog
touch urls.py
The file structure under blog is like this.
.
├── __init__.py
├── admin.py
├── apps.py
├── migrations
│ └── __init__.py
├── models.py
├── tests.py
├── urls.py
└── views.py
By changing the contents of the created urls.py in this way, you can set the routing of the function (= index.html call process) created in views.py earlier.
blog/urls.py
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
]
By the way, by setting name ='index', you can call this url by reverse lookup using the name "blog: index".
At this point, check if index.html can be called.
Run runserver in the directory where manage.py is located.
python3 manage.py runserver
If you pass successfully, access ** 127.0.0.1:8000/blog ** with your browser. This is because when the address with blog is accessed in mysite / urls.py earlier, the contents described in the blog application will work.
If you access it and the contents of index.html are displayed, it is successful.
Next time, we will create models and prepare to actually register the article.
→ Continued: Django tutorial (blog application creation) ② --model creation, management site preparation
Recommended Posts