My blog development (024)

Posted May 24, 20203 min read

image.png
image.png
image.png
Simulation(copy a copy first, operate on the copy):

Create User(AbstracUser) class in blog/user/models.py, create attribute nickname, and then create its metaclass
image.png
Then add fields in mysite/settings:
AUTH \ _USER \ _MODEL = 'user.User'
image.png
Because we have changed the User, we need to modify the User used in it:
blog/models.py:
image.png
Then make the same modification in comment/models.py:
image.png
Similarly:likes/models.py
image.png
For modifications in user/forms.py:
image.png
user/views.py also
image.png
Migrating the database:(Note that this is the migration in the original file copy)
At this point, there will be a database migration conflict:
image.png
Treatment:Delete the migration table in blog/migrations and all other migrations, and then re-migrate the application:

Create a cache table:python manage.py createcachetable

Create super administrator:python manage.py createsuperuser

Running:There will be no blog posts at this time, because we deleted them

Create an admin.py management background in user/to manage users:

Write code:
image.png
Modify user/models.py to change the nickname English name to Chinese name:
image.png
Effect:
image.png
Because the data showing the user's identity status is not fully displayed after submission, add list \ _display:to admin.py
image.png
Effect:
image.png
to sum up:
image.png
Reference to djangoproject website
A new model expansion method is used here:simple modification and less
image.png

1) Create a custom model:create a new models.py file in the user
image.png
Then migrate the application

Add an admin file to user to display user information,
image.png
In the background user management, you want to see the user name and other related information settings:

Copy the following code from djangoproject.com to user/admin.py:
image.png
Then make changes:
image.png
Run restart:
image.png
Because the nickname is still Nickname, I want to change it:

Modify user/models.py:
image.png
Then display the nickname in the list:modify the admin.py file
image.png
Display nickname in Chinese:
image.png
Effect:
image.png
image.png


Function increase:add a background management button in the drop-down box to jump directly to the background management interface
image.png
Modify base.html in templates:
image.png
Modify login.html and register.html to make it automatically jump to the home page after registration, and automatically jump to the home page after login

However, background management should not allow everyone to log in:it must be a user or a super administrator:
image.png