Django Book Management System 3

Source: Internet
Author: User
Tags form post contact form

Add the author's additions and deletions to search

One, add data table

Add a class in the models file below the App01 file

class Author (models. Model):    = models. Autofield (primary_key=True)    = models. Charfield (max_length=32)    Books= models. Manytomanyfield (to='book')

Perform

1. Python manage.py makemigrations--and find a little laptop to record models.py's changes.

2. Python manage.py Migrate---translate the above change records into SQL statements, go to database execution

Second, add the route

  

URL (r ' ^author_list/$ ', views.author_list),
URL (r ' ^add_author/$ ', Views.add_author),
URL (r ' ^del_author/$ ', Views.del_author),
URL (r ' ^edit_author/$ ', Views.edit_author),

#上传功能
URL (r ' ^upload/$ ', views.upload),

  

Third, add the corresponding function

1. Functions of the author's display list

def author_list (Request):     # 2. Get the ORM object    for the author Author_obj_list = Author.objects.all ()    # Returns a author page table     return Render (Request,'author_list.html', {'author_obj_list ': author_obj_list})   # Show All the authors and books

2. Add the Author function

defAdd_author (Request):#2. Show all the books to the user    #get all the booksBook_obj_list =Book.objects.all ()#Post When a user submits a request    ifRequest.method = ='POST': Add_author_name= Request. Post.get ('author_name')#get the author's nameAdd_book_ids = Request. Post.getlist ('Book_ids')#get the book written by the author (be sure to use getlist or you can only get the last book)        #A = Author.objects.create (name=add_author_name)        #A.books.set (add_book_ids)Author.objects.create (Name=add_author_name). Books.set (Add_book_ids)#using ORM to write data into a database table        returnredirect'/author_list/')    #1. First time to return a page for a user    returnRender (Request,'add_author.html',{'book_obj_list': Book_obj_list})

3. Delete Author functions

def Del_author (Request):     # get the ID    of the deleted object del_author_id = Request. Get.get ('ID')    # Delete data    from SQL table Author.objects.get (id=del_author_id). Delete ()    #  jump back to author_lsit face page    return Redirect ('/author_list/')

4. Editing author functions

defEdit_author (Request):#1. Get the ID you want to edit firstedit_id = Request. Get.get ('ID')#get the ID you want to edit for the first timeAuthor_obj = Author.objects.get (id=edit_id)#get an Orm object to edit the author of the table    ifRequest.method = ='POST': Edit_author_name= Request. Post.get ('author_name') Edit_book_ids= Request. Post.getlist ('Book_obj_ids')#Get the dataAuthor_obj.name= Edit_author_name#Modify AuthorAuthor_obj.save ()#Change your watch to save it.Author_obj.books.set (edit_book_ids)#Modify the third contact form        returnredirect'/author_list/')    #returns an HTML page for the first time    #need the corresponding author, titleBook_obj_list =Book.objects.all ()returnRender (Request,'edit_author.html',{'book_obj_list': Book_obj_list,'Author_obj': Author_obj})

5. Upload function

defUpload (Request):ifRequest.method = ='POST':#Submit Form PostFile_obj = Request. Files.get ('name')#get File Object        Print(file_obj) file_name=File_obj.namePrint(file_name)ifos.path.exists (Os.path.join (settings. Base_dir,file_name)):#Parse OS.path.exists path exists as true, does not exist not false, file name stitching project path            #add up is the project path under the other side of the existence and my file name fileName,suffix = File_name.split ('.') name+='1'file_name= name +'.'+suffix with open (file_name,'WB') as F: forChunkinchfile_obj.chunks (): F.write (chunk)#give the user an interface for the first time    returnRender (Request,'upload.html')

Django Book Management System 3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.