[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 3 ~

Series list (will be updated when all articles are completed)

Create a view

Django provides a generic view. There are CreateView, UpdateView, DeleteView, ListView, DetailView, etc.

This time, we will use ListView to create a list page.

/crud/blog/view.py


from django.views.generic import ListView
from .models import Post


class PostListView(ListView):
    #Model specification
    model = Post
    #Describe the path under html specified templates
    template_name = 'blog/home.html'
    #The name of the record group in the Post class
    context_object_name = 'posts'
    #Order: Descending date (latest is up)
    ordering = ['-date_posted']

Creates the html file specified in view.py. Extract one record of posts (record group in Post class) with for and display the title, content, author, and posting date.

/crud/blog/templates/bolg/home.html


{% for post in posts %}
{{ post.title }}<br>
{{ post.content }}<br>
{{ post.author }}<br>
{{ post.date_posted }}<br>
{% endfor %}

Create URLConf

URLConf is responsible for mapping URL patterns to views in Django. Write "Return this view if this URL is specified".

Please note that there are two files in urls.py. The first is /crud/config/urls.py. The entire project is set as the setting range. Load urls.py of each app by using include. The second is /crud/blog/urls.py. The setting range is the blog application.

Describe the setting to read urls.py of the blog application.

/crud/config/urls.py


from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('blog.urls')),
    path('admin/', admin.site.urls),
]

"Http://127.0.0.1:8000/ returns a PostListView".

/crud/blog/urls.py


from django.urls import path
from .views import PostListView

urlpatterns = [
    path('', PostListView.as_view(), name='blog-home'),
]

Show view

Now that you're ready, let's view the view.

python manage.py runserver

Go to "http://127.0.0.1:8000/". image.png

Did it look like the one above?

That's all for today.

Recommended Posts

[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 3 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 4 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 5 ~
Create a Todo app with the Django REST framework
Create a python GUI using tkinter
Create a Python general-purpose decorator framework
Install Python framework django using pip
Create a CRUD API using FastAPI
Try using the Python web framework Django (2) --Look at setting.py
Implementation of CRUD using REST API with Python + Django Rest framework + igGrid
Create a graph using the Sympy module
[Python] Create a Batch environment using AWS-CDK
[Ev3dev] Create a program that captures the LCD (screen) using python
Create a REST API to operate dynamodb with the Django REST Framework
Create a shogi game record management app using Django 3 ~ Django default management site settings ~
Try using the Python web framework Django (1)-From installation to server startup
Create a record with attachments in KINTONE using the Python requests module
Try using the Python web framework Tornado Part 1
Create a web map using Python and GDAL
Run a Python file from html using Django
Create a Mac app using py2app and Python3! !!
Try using the Python web framework Tornado Part 2
Create a GUI on the terminal using curses
Create a Django schedule
Django python web framework
Create a Python module
Create a Python environment
Create a Python image in Django without a dummy image file and test the image upload
Create a simple reception system with the Python serverless framework Chalice and Twilio
Create a Django project and application in a Python virtual environment and start the server
Try to create a Todo management site using WebSocket with Django (Swamp Dragon)
Create a data collection bot in Python using Selenium
Create a Todo app with Django REST Framework + Angular
Cut a part of the string using a Python slice
(Python) Try to develop a web application using Django
[Python] Create a ValueObject with a complete constructor using dataclasses
Build a Python virtual environment using venv (Django + MySQL ①)
[Python] Mask the image into a circle using Pillow
A little bit from Python using the Jenkins API
Create a dictionary by searching the table using sqlalchemy
[Python] Let's change the URL of the Django administrator site
Create a Wox plugin (Python)
Create a function in Python
Create a dictionary in Python
[S3] CRUD with S3 using Python [Python]
Create ToDo List [Python Django]
Create a homepage with django
Create a python numpy array
Create a Django login screen
Create a directory with python
Create a shogi game record management app using Django 4 ~ Create View ~
Create a local scope in Python without polluting the namespace
Create a real-time auto-reply bot using the Twitter Streaming API
How to generate a query using the IN operator in Django
Create a Twitter BOT with the GoogleAppEngine SDK for Python
Create a simple CRUD app using Django's generic class view
[Python] Create a screen for HTTP status code 403/404/500 with Django
I tried to create a RESTful API by connecting the explosive Python framework FastAPI to MySQL.
Create a DI Container in Python