Develop a Django blog website project based on python

Simple blogging platform based on Python and Django framework. The platform provides a user-friendly interface that enables users to easily create and manage blog posts, comments and tags

Pre-environment: The environment that needs to be prepared is above python3

Create a virtual environment (for compatibility with different Django versions)

  1. Create a folder dedicated to Django (website)
  2. Install Django
    pip3 install django
  3. Open cmd to create a virtual environment in the website (djangoenv is the name of the virtual environment project)
    cd desktop/website
    python -m venv djangoenv
  4. Continue typing  djangoenv\Scripts\activateto activate the virtual environment.
    This is an operation that must be performed every time you use a virtual environment

 

 The above virtual environment is created

Create a Django project

`django-admin startproject one(项目名)

 The purpose of the initialization file
Manage.py is a website management tool that can be called on the command line. The init.py
in the one folder tells Python that this folder is a module settings.py. Some configuration urls.py in the one project declares each URL in the website Interface between usgi.py web server and django project



Create a new Django App 

`cd one`
python manage.py startapp blog(app项目名)

 Then add the App to settings.py INSTALLED_APPS includes some Django-built-in applications by default:

  • django.contrib.admin -- Admin site
  • django.contrib.auth--authentication authorization system
  • django.contrib.contenttypes -- Content Types Framework
  • django.contrib.sessions -- session framework
  • django.contrib.messages -- message framework
  • django.contrib.staticfiles -- framework for managing static files

Some applications that are enabled by default require at least one data table, so create some tables before using them

python manage.py migrate

This command is used to check the INSTALLED_APPS setting and create the required data tables for each application (acting on the database file)

The general operation process of Django

Responses to different paths (usually modified in urls.py--blog)

Create a new file under the blog folder: urls.py is used to process URL parsing, and add a path to urls.py under the one folder
urls.py--one path('',include('blog.urls')),

  • urls.py--one to addfrom django.urls import path,include
  • urls.py -- add in blogfrom django.urls import path

The function include() allows referencing other URLconfs. It will truncate the portion of the URL that matches this entry and send the remaining string to the URLconf for further processing. include() should always be used when including other URL patterns, admin .site.urls is the only exception.

The function path() has four parameters, two must parameters: route and view ,

Two optional parameters:  kwargs and name generally only fill in two mandatory parameters:

route is a criterion for matching URLs. When Django responds to a request, it will start from the first item in urlpatterns and match the items in the list in order until a matching item is found. These criteria will not match GET and POST parameters. or domain name.

https://www.example.com/myapp/ For example, URLconf will try to match myapp/ when processing a request  . When processing a request  https://www.example.com/myapp/?page=3 , it will only try to match myapp/

path() parameter: view
When Django finds a matching criterion, it will call this specific view function and pass in an HttpRequest object as the first parameter, and the "captured" parameters are passed in as keyword parameters .
The route parameter can have some specific format to handle the route

  • <slug:blog_link>--slug可以识别字符和数字
  • <int:blog_id>--int用于识别数字,表示这个网址是个数字存到blog_id这个变量里
  • <str:blog_title>--str用于识别字符串

There is a default path as shown abovehttp://127.0.0.1:8000/admin/

write model

A model is the only and accurate source of information about your data. It contains the important fields and behaviors of the data you are storing. In general, each model maps a database table

  • Each model is a python class and these classes inherit from django.db.models.Model
  • Each attribute of the model class is equivalent to a database field
  • In summary, Django is giving you an automatically generated API for accessing the database

Various models available in Django

The three association models are to prevent the problem of excessive duplication (to facilitate adding data)

Associated Author Model

 

 Use ForeignKey() to associate another model

Create a Post model in the models.py file, and the Category and Tag models associated with it

  • ForeignKey()--generally used for one-to-many models
  • ManyToManyField()--generally used for many-to-many models
  • The associated author model has an out-of-the-box direct import
    from django.contrib.auth.models import User

 Then, if you want to directly operate these three data in the background, you need to register in admin.py

 

  • from .models import Post,Category,Tag--Import Post, Category, Tag from models.py

  • admin.site.register -- to register

Finally, whenever we make changes to the models, we need to perform two operations 

python manage.py makemigrations
python manage.py migrate

The first command is used to detect your modification of the model file, and the modified part as a migration (this is a form of storage for changes) The second command
creates a data table of the newly defined model in the database

Guess you like

Origin blog.csdn.net/weixin_56043516/article/details/130488249