Load news comment function

1. analysis

Business process flow :

  • Judge the news on the front end id Is it empty , Integer or not 、 Does it not exist

Request method GET

url Definition '/news/<int:news_id>'

Request parameters :url Path parameter

Parameters type Does the front end have to transmit describe
news_id Integers yes Journalism id

News details page is directly realized by template rendering , Directly render the comment information of this news on the news details page .

Background code : Take the comments out of the database , Follow the news details page and render it directly ,

Required fields : Comment on id, Comment content , Commentator , Comment on time ,

Parent comments : Ditto field

Add self association to the database

# More than a table , author , Self correlation ( Parent comment )
# Just look up the fields : Comment content , The author's name , Creation time ,
# The author of the father's review , The content of father's comments , Parent comment creation time
# Query criteria : Not logically deleted , Designated news ,

Back end code implementation

# Import test data tb_comments_20181222.sql
# Baidu cloud disk 2018 year 12 month 22 Japan Answer class \datas Medium tb_comments_20181222.sql Import the file into the virtual machine first
# Make sure tb_users There is id by 1,2,3 Three users of , Otherwise, importing test data will report an error
mysql -u user name -p -D Database name < tb_comments_20181222.sql

stay news/models.p Of documents Comments The following instance methods are defined in the model , For serializing :

def to_dict_data(self):
comment_dict = {
'news_id': self.news.id,
'content_id': self.id,
'content': self.content,
'author': self.author.username,
'update_time': self.update_time.strftime('%Y year %m month %d Japan %H:%M'),
'parent': self.parent.to_dict_data() if self.parent else None,
} return comment_dict stay news-data.html In file


Tanzhou classroom 25 class :Ph201805201 django project Lesson 24 Article home page Multi level review database design , Background code complete ( Class notes ) More articles about

  1. Tanzhou classroom 25 class :Ph201805201 django project Lesson 22 Article home page The news list page scrolls , Carousel map background implementation ( Class notes )

    newly build static/js/news/index.js file , Mainly used to send requests to the background , // newly build static/js/news/index.js file $(function () { // News list function l ...

  2. Tanzhou classroom 25 class :Ph201805201 django project Lesson 25 The article multi-level comments front and back stage implementation ( Class notes )

    Add news comment function 1. analysis Business process flow : Judge the news on the front end id Is it empty , Integer or not . Does it not exist Judge whether the content of the comment is empty Judge if there is a parent comment , The father commented on id Whether it's related to news id matching Determine whether the user is logged in Save news reviews ...

  3. Tanzhou classroom 25 class :Ph201805201 django project Lesson 19 Article home page database model , The front and back functions are realized ( Class notes )

    - Database model design : article : News sheet : Field : picture , title , Abstract , type , author , Creation time The label table Comment table , Shuffling figure : Foreign keys , Foreign key table pointing to the article stay utls Create under directory  models.py  Put the words commonly used in other models ...

  4. Tanzhou classroom 25 class :Ph201805201 django project Lesson 29 docker example , File download before the background to achieve ( Class notes )

    docker example :wq! Save and exit Put one in html file Not enough permissions , Add sudo Check the local warehouse's image function docker -- name, Followed by a runname , -p Physical machine ports are mapped to container ports , ...

  5. Tanzhou classroom 25 class :Ph201805201 django project Lesson 28 Journalism elasticsearch Search before and after the stage can achieve ( Class notes )

    Back end function implementation file , class , Field , Don't change the name , stay apps/news/search_indexes.py Create the following classes :( The name is fixed to search_indexes.py) # -*-# -*- coding ...

  6. Tanzhou classroom 25 class :Ph201805201 django project Lesson 26 docker brief introduction ( Class notes )

    Official documents : https://docs.docker.com/install/linux/docker-ce/ubuntu/#set-up-the-repository 1, Update sudo apt-ge ...

  7. Tanzhou classroom 25 class :Ph201805201 django project Lesson 15 The user registration function is realized in the background ( Class notes )

    The front desk : Judge user input , Confirm the password , cell-phone number , Send a request to the background after everything passes , Request mode :post stay suers In the view under application : 1, Create a class , 2, establish GET Method , Manifesto page 3, establish   POST Method ...

  8. Tanzhou classroom 25 class :Ph201805201 django project Lesson 20 Database analysis design ( Class notes )

    https://www.dbdesigner.net/

  9. Tanzhou classroom 25 class :Ph201805201 django project Lesson 18 The front desk annotation ( Class notes )

    In static file js/user To be deceived , Of auth.js In file $(function () { let $username = $('#user_name'); // choice id by user_name The web page of ...

Random recommendation

  1. C# The program runs with administrator privileges

    original text :C# The program runs with administrator privileges C# The program runs with administrator privileges stay Vista and Windows 7 And newer versions of the operating system , Added UAC( User account control ) Security mechanism , If UAC To be opened , Users even as administrators ...

  2. IT company 100 topic -35- Find the largest two-dimensional matrix in a matrix ( Elements and maximum )

    Problem description : Find the largest two-dimensional matrix in a matrix ( Elements and maximum ). Such as : 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 What's the biggest of all : 4 5 9 10   analysis : 2*2 The largest sum of subarrays . Ergodic summation , when ...

  3. html5 in manifest Characteristic test

    Test environment and tools   chromium  18.0.1025.151 ( Develop the compiled version 130497 Linux) Ubuntu 11.04 One . Test content 1.A page manifest The cache js file ,B The page is not ...

  4. How to uninstall eclipse Medium ADT

    uninstall ADT Methods , The method is as follows : 1. choice  Help > Install New Software: 2. stay "Details" The palette , Click on "What is alre ...

  5. spring3.2.0 And mybatis3.2.7 Integration error --Failed to read candidate component class--nested exception is java.lang.IllegalArgumentException

    The error message is as follows : org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate com ...

  6. windows Under the system , Small data volume Oracle User physical backup

    Environmental Science :windows Server 2003 oracle 10g, Inter system backup The target system creates shared files , The original system mounts the shared directory Write batch scripts , Call... Regularly with task plan Rem * Because the real-time requirement of the system is not very high , data ...

  7. Pop up plug in layer Use

    layer It's a popular one in recent years web Shell assembly , She has a full range of solutions , Dedicated to serving developers at all levels , Your page will easily have a rich and friendly operation experience . Official address of plug-in :http://layer.layui.co ...

  8. day18_python_1124

    01 Chicken soup Lotus law Mangosteen's law Cicada's law Lotus law : Tomorrow will grow twice as much as today , The pond is full in thirty days , The first 29 The day is half . Mangosteen's law : When bamboo grows , In the first three years, it was only on the surface 3 centimeter , From the fourth year , In a day 30cm It's starting at the speed of ...

  9. [EXP]Microsoft Windows 10 - XmlDocument Insecure Sharing Privilege Escalation

    Windows: XmlDocument Insecure Sharing Elevation of Privilege Platform: Windows (almost certainly ear ...

  10. typescript Interface extension 、 Inheritance of interfaces

    // Interface extension : Interface can inherit interface // interface Animal{ // eat():void; // } // interface Person extends Animal{ // work ...