Database Design
A user table (auth_user that table is used, by inheritance Custom Table Abstr.)
phone telephone
avatar Avatar
create_time Created
# foreign key
blog site one individual table
Personal Sites table
site_name site name
site_title aphorisms
site_theme style
Tag table
name tag name
# foreign key
blog many individual sites each site containing multiple tags
Category Table
name category name
# foreign key
blog many individual sites each site containing multiple classification
Article table
title Article Title desc page content article content create_time article creation time # foreign keys under many personal blog site can site a number of articles tag labels-many articles have more than a label, a label may correspond to multiple articles category many classification classification following a number of articles # ordinary field # database design optimization (******) will not have to set these points Chan stepped points table query data, and reduce database queries # open a transaction, when you step on the operating point thumbs table, the following data will change the general field comments comment_num point number like ordinary field up_num points stepped down_num ordinary field
The thumbs point step on table
Like many user a user table corresponds to only one user, the user can point a different Like
article Like many articles only a table corresponding to an article, an article can have a plurality Like
is_up 0 /. 1
Comments Table
user-to-many user table is a review written by a user, a user can write more reviews
article to many article table
comment Review
create_time Created
parent-to-many table Comments (autocorrelation) parent comment id value if there is comments that you are the child if there is no value that you are a parent commentary
Registration functional use of forms components to verify the login information is correct, and render the page
1. File input box set id = 'myfile' input box and hidden files, set for = 'myfile' label in the label, then click on the picture two words have the effect of pop-up file input box.
<label for = "myfile" > Avatar set a default avatar image <img src = " /static/img/default.jpg " alt = "" height = " 80 " style = " margin-left: 20px " the above mentioned id = " img " > </ label> <the iNPUT of the type = " file " the above mentioned id = "myfile" style = " Run the display: none " > # file input box
Registration function general process:
1. Create a view views forms object, the template is transmitted to the layer, rendering the input box
2. Select the registration page picture reading Filereader objects ajax.
3. Write ajax data submitted in the template layer, layer view views the received data, the received data parity component forms, forms assembly to pass directly to the check request.POST
4. backstage passes
Log function
Home features
2019-9-27
Logout function
auth.logout(request)
change Password
Copying a page mode in block
it must be a form submission form input tag, it can not be a button
admin Admin
To create a super-user login background
Admin.py find the file in an application and then register the dictation you want to watch the operation, use the super administrator account to log in the background data management
from django.contrib import admin from app01 import models admin.site.register(models.UserInfo) admin.site.register(models.Blog) admin.site.register(models.Tag) admin.site.register(models.Category) admin.site.register(models.Article2Tag) admin.site.register(models.Article) admin.site.register(models.UpAndDown) admin.site.register(models.Comment)
User picture show
Main page display
Profile structures
Personal sites and users site_name table settings the same name
step:
1. Enter the personal site to verify whether there is
Filter query results returned, queryset objects ( single Queryset objects )
Print Results: <QuerySet [<UserInfo: jason >]>
Print type: <class 'django.db.models.query.QuerySet'>
Query filter >> first ()
Print Results: jason This is a target print type: < class ' app01.models.UserInfo ' >
Queryset plurality of objects (for transfer to the need to remove the background loop)
<QuerySet [<Article: .Net Core Add Swagger support>, <Article: Docker Compose and Docker Stack differences>, <Article: novice to learn FFmpeg - complete API calls to read and output video>, <Article: Learn about Java SPI principle>, <Article: Django + MySQL Dashboard web client database visualization>, <Article: mybatis the collection association and optimize the use of multi-parameter passing>, <Article: 200 lines of code to achieve Mini ASP.NET Core>]>
Sidebar rendering
Sidebar feature
2019-9-28
First, the article details page set up
Copy blog content and style park, pay attention to the content of the code to be displayed in the front page | Safe , front-end code to cancel the escape
Second, the thumbs point stepping function
1. First check whether the user is logged
request.user.is_authenticated()
2. check whether the user thumbs point of this article is to step on to write their own
3. check whether the current user has to this point been thumbs up or step on
4. Modify the data recorded in the article table and thumbs point to step on the table should be recorded
Query uses F
models.Article.objects.filter(pk=article_id).update(up_num=F('up_num')+1)
In the table data query or create time, if the field is a foreign key, to pass an object field, if the field is a common field, then pass a specific value. (******) this is the best
= models.Article.objects.filter article_obj ( PK = the article_id , blog = blog ) .first ()
in the Article table, pk is a common field pass the article_id specifically refers to, blog is a foreign key, all objects transmit a blog
models.UpAndDown.objects.create ( Article This article was = article_obj , the request.user = User, is_up = is_up)
or (if no object value can take specific values pass)
models.UpAndDown.objects.create ( the article_id = the article_id , the request.user = User, is_up = is_up)
a first field according to write models, according to the second database field is written to write
5. Cancel Tags escaping at the rear end
from django.utils.safestring import mark_safe mark_safe('请先<a href="/login/">登录</a>')
Analyzing distal ajax:
1. Click judgment is thumbs up or stepped on point
Is there a class attribute according to the clicked label, get a Boolean value string format. The point is to determine the point of praise or stepped on.
Article id and pass a Boolean value to the backend
The callback function receives the result returned by the backend, to determine in accordance with code, just like in qualifying or point Point of the modified pages step
Third, review
Cancel float
class="clearfix"
1. comment box and make a list of comments styles
2.ajax judgment is the root comment or sub-comments, different content interception return to the back-end, for articles and comments id passed to the backend content
3. POST receiving back-end data, do data synchronization, data to be saved to a comment form and article comments field table, here to write affairs.
4. The receiver front-end code returned by the backend, the determination, the user reviews the list of comments added to the content, need to define the list style.
5. Click to reply, child generates comment.