程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

With a small hand raised, you can implement a micro blog system with Python Django

編輯:Python

Two 、Python Django Micro blogging

====================

Through simple operation , You can go to Django Implement a blog in application

2.1 Generate files with templates


Enter into In the directory created by the previous blog , Execute the following order .

django-admin startapp blog
# You can also use the following command
python manage.py startapp blog

After the command runs , The generated directory is as follows .

The relevant documents are as follows :

blog # root directory
__init__.py
admin.py # Background management
apps.py # Application settings related
models.py # Model , Database correlation
tests.py # Test related
views.py # View related
migrations # Database change record folder 

I'm going to modify my_website In folder settings.py file , add to blog application , The specific code is as follows , The point is the last line .

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog', # This is very important
]

stay Django in , If you want to apply the model 、 Static files 、 Template these contents , Need to be in INSTALLED_APPS Add the corresponding application directory , If not set , Applications don't load .

2.2 Create article model and database structure


A blog includes a title , Content , Release time, etc , These are all in Django Create in , The core thing to change is models.py file .

from django.db import models
# Create your models here.
class Blog(models.Model):
title = models.CharField(" title ", max_length=100)
content = models.TextField(" Content ")
creatr_time = models.DateField(" Release time ")

This class is the model class , Implement a model class , Need to inherit from models.Model class , This class is used for data and Python Conversion operations between objects .

Next Blog Class reflects to sqlite3 In the database , At this stage, please focus on imitation , The following will complete the corresponding knowledge framework for you .

stay manage.py Run the following command in the same folder :

python manage.py makemigrations blog

This command is used to check blog Changes to model files in , Because we created a Blog class , therefore models.py The document was modified , When executing the above command , Will output the following .

>python manage.py makemigrations blog
Migrations for 'blog':
blog\migrations\0001_initial.py
- Create model Blog

The message is also Create model Blog, Prompt creates a Blog class , Next models.py Content in sqlite3 created , The command used is python manage.py migrate blog, The operation results are as follows :

> python manage.py migrate blog
Operations to perform:
Apply all migrations: blog
Running migrations:
Applying blog.0001_initial... OK

2.3 Create a management background


Let's take a simple step to realize blog The management of the table ,Django It has its own user authentication system , Through the command python manage.py migrate That is to say .

> python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying sessions.0001_initial... OK

This command creates the database table structure of some columns , After the table structure appears , You also need to create a login account , The core command used is python manage.py createsuperuser.

> python manage.py createsuperuser
Username (leave blank to use 'administrator'): xiangpica
Email address: [email protected]
Password:
Password (again):
The password is too similar to the username.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.

The preparations are complete , Yes admin.py File modification , Be careful admin.py The location of the file and the calling relationship between the modules .

from django.contrib import admin
from blog.models import Blog
# Register your models here.
@admin.register(Blog)
class BlogAdmin(admin.ModelAdmin):
pass

Before the official operation , Also need to modify the web page to show the content in Chinese and English , open setting.py file , Modify the following .

# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-hans'

Preparatory work completed , The console runs the following command python manage.py runserver

> python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
March 24, 2021 - 22:22:33
Django version 3.1.7, using settings 'my_website.settings'
Starting development server at http://127.0.0.1:8000/

The default page opens , The content of the exhibition is as follows :

Modify the access address to :http://127.0.0.1:8000/admin, The login window appears , Use the account registered above to access .

Enter some information about the article and save it , Back to the list page , There is a small detail , The data in the orange box is a Blog object, Not the title of the article , Continue to modify the code .

modify modes.py file , The modification is as follows :

from django.db import models
# Create your models here.
class Blog(models.Model):
title = models.CharField(" title ", max_length=100)
content = models.TextField(" Content ")
creatr_time = models.DateField(" Release time ")
def __str__(self):
return self.title

A small blog does , You can edit and delete articles .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved