How to create a Django blog site

django01 django

Easy to create blog site even for beginners

Development Preparation

  1. Figure-01: “Build virtual environment (line 1)” in any folder
  2. Figure-01: Virtual environment execution (When executed, “(myvenv)” appears at the leftmost position)
  3. Figure-02: Creating requirements.txt (describing packages required for development)
  4. Figure-02: Package installation (pip3 install -r requirements.txt)
図-01
図-02

Create Project

図-03
図-04
図-05
図-06
  1. Figure-03: Project Creation (“mysite . is an arbitrary name. Project name space period, which can be created directly under the directory)
  2. Figure-04: mysite -> setting.py to change settings (language and time zone)
  3. Figure-05: Migrate (database setup)
  4. Figure-06: Web server startup
  5. Figure-07: Access the URL and confirm Django installation
図-07

Create Application

  1. Figure-08: Application creation (“app” is an arbitrary name)
  2. Figure-09: mysite -> settings.py -> ‘widget_tweaks’, ‘app’, added
図-08
Django-settings.py
図-09

Model

  1. Figure-10: app -> models.py “class name: Post” is optional. author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) *ForeignKey is linked to login linked to the user.

def str(self) allows you to define a name (string) to identify the data (records) in the model that will be displayed on the admin screen.

We have defined the title, text, and created_date fields in the Post model, and in def str(self) we return the title field

https://office54.net/python/django/model-str-self
図-10

Admin

図-11
  1. Figure-11: Reflecting the model just created on the admin page: app -> admin.py
  2. Figure-12: Migration: python3 manage.py makemigrations (required when changing models)
  3. Figure-12: migrate: python3 manage.py migrate
  4. Figure-13: Creating an administrative user: python3 manage.py createsuperuser
  5. Figure-14: Web server startup: python3 manage.py runserver: Add /admin to the URL and log in with the username and password you just set.
  6. Figure-15, 16: Register a few dummies in “Post” (for layout confirmation)
図-12
図-13
図-14
図-15
図-16

Routing (URL)

There are two types of routing, one for projects and one for applications.

1. For projects (for management sites and applications)

2, For applications (URL and view matching)

  1. Figure-17: mysite -> urls.py (routing for project)
  2. Figure-18: app -> urls.py (creating and routing urls.py for the application)
図-17
図-18

views.py

  1. Figure-20: app -> views.py Edit: Importing View and Post
  2. Figure-20: Edit the top page view (index)
  3. Figure-20: get function, the first function called when the view is called
  4. Figure-20: Retrieving data from the Post model
  5. Figure-20: return -> pass data to the template specified by the render function
図-20

templates: base.html

Template converts data passed from view to HTML

図-21
  1. Create “templates” folder in app
  2. Figure-21: Create “app” folder in templates
  3. Figure-21: Creating “base.html” in app -> templates -> app ->
  4. Copy and paste code from bootstrap

base.html often writes “headers, navigation, footers” that do not change dynamically

Django入門ブログ構築チュートリアル レッスン2

Bootstrapからコードをコピペ

bootstrap

templates: index.html

図-22
  1. Figure-22: Loading base.html in the first line
  2. Figure-22: Loading postings with {% %}
  3. Figure-22: Reading an object specified by {{ }}
図-23
  1. Figure-23: Creating the sidebar

CSS

図-24
  1. Figure-24: app -> static folder creation -> css folder creation -> style.css file creation
  2. Figure-24: main { flex: 1; } to always place the footer at the bottom
  3. Figure-25: Adding stylesheet to base.html
図-25
  1. Figure-26: Start the web server and click on the link
  2. Figure-27: Check the contents of the top page
図-26
図-27

Detail Page

図-28
  1. Figure-28: app -> urls.py with path for detail screen (post_detail)
  2. Figure-29: Add detail screen class to app -> views.py
  3. Figure-29: post_data = Post.objects.get(id=self.kwargs[‘pk’]) to get the data associated with the id (pk) of each post
  4. Figure-29: Calling the render function with return to pass data to the template
図-29
  1. Figure-30: app -> index.html Add link for more details
  2. Figure-31: app -> templates -> app -> post_detail.html created
図-30
図-31

Upload Images

  1. Figure-32: “Pillow~=2.2.1” added to requirements.txt
  2. Figure-33: Installing with pip3 install -r requirements.txt
  3. Figure-34: Add image upload destination in mysite -> settings.py
図-32
図-33
図-34
  1. Figure-35: mysite -> append to urls.py
  2. Figure-35: if settings.DEBUG: … is for development environment *not when deploying
図-35
  1. Figure-36: Append to class Post in app -> models.py
  2. After editing models.py, be sure to run “python3 manage.py makemigrations” and “python3 manage.py migrate
図-36
  1. Figure-37: Displaying images in index.html
  2. Figure-38: Displaying images in post_detail.html
  3. Both with {% if post.image %}, no error without image
  4. Figure-39: Editing style.css
図-37
図-38


Recomment

Deploy Django Blog to Vultr

タイトルとURLをコピーしました