Python Django micro-blog

With a few simple steps, you can implement a blog application in Django

2.1 Generating Files using templates

Go to the directory created in the previous blog post and execute the following command.

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

After the command is executed, the following directories are generated.

The relevant documents are described as follows:

Blog # root directory __init__.py admin.py # background management apps.py # Application Settings related models.py # model, Tests.py # tests-related views.py # views-related migrations # database change log folderCopy the code

Next modify the settings.py file in the my_website folder to add the blog application as follows, focusing on the last line.

INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django. Contrib. Sessions',' the django. Contrib. The messages' and 'django. Contrib. Staticfiles',' blog '# is very important here]Copy the code

In Django, if you want to apply models, static files, or templates, you need to add the corresponding application directory to your INSTALLED_APPS directory. If you don’t set this directory, your application won’t be loaded.

2.2 Create article model and database structure

A blog post includes the title, content, and release date, all of which are created in Django. The core changes are to the 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 ")Copy the code

This class is a Model class, and to implement a Model class, you need to inherit from the Models. Model class, which is used to convert data from the database to Python objects.

The following Blog class will be reflected into the SQlite3 database, this stage please imitate the main, the subsequent will be complete for you to supplement the corresponding knowledge framework.

Run the following command in the manage.py folder:

python manage.py makemigrations blog

Copy the code

This command is used to check for changes to the model file in the blog. Since we created a blog class earlier, the models.py file was modified. When executing the command above, the following output is displayed.

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

Copy the code

Python manage.py Migrate Blog Create a Blog class in sqlite3 using python manage.py Migrate Blog.

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

Copy the code

2.3 Creating a Management Background

To manage blog tables, run the python manage.py Migrate command using Django’s built-in user authentication system.

> 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

Copy the code

This command creates columns of database table structures. Once the table structures appear, you also need to create login accounts. The core command 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.

Copy the code

After the preparation work is completed, modify the admin.py file, paying attention to the location of the admin.py file and the calling relationship between modules.

from django.contrib import admin
from blog.models import Blog
# Register your models here.

@admin.register(Blog)
class BlogAdmin(admin.ModelAdmin):
    pass

Copy the code

Before the official operation, we still need to modify the contents of the webpage in Both English and Chinese. Open the file setting.py and modify the following contents.

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

Copy the code

With preparations complete, the console runs the following command python manage.py runserver: 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/Copy the code

The default page is displayed as follows:

Modify the access address is: http://127.0.0.1:8000/admin, a login window, use the above registered account can access.

After entering some information about the article and saving it, you return to the list page and a small detail appears. The data in the orange box is a Blog object, not the title of the article. Continue to modify the code.

Modify the modes. Py file 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 ("内容") creatr_time = models.datefield ("内容") def __str__(self): return self.titleCopy the code

A small blog complete, you can edit and delete articles.