The interaction between SQL and DB in Django--Non-ORM

Source: Internet
Author: User

In your views.py,you can code like this:

1 ImportMySQLdb2 defIndex (Request):3conn=MySQLdb.connect (4host='127.0.0.1',5port=3306,6User='Root',7Passwd='Root',8db='Pythonweb',9charset='UTF8',#must be consistent with the code when the database is establishedTen  One         ) ACursor=conn.cursor ()#Defining Cursors -Cursor.execute ("SELECT * from Firstapp_article") -results=Cursor.fetchmany () the  -articles=[] -      forResultinchResults: - Articles.append ( +             { -                 #this is associated with a table that is defined in the database +                 #result[0] as ID A                 'title': result[1], at                 'content': result[2], -                 ' views': result[3], -                 'likes': result[4], -                 'Createtime': result[5], -                 'Editors_choice': result[6], -                 'cover': result[7], in                 'Url_image': result[8], -             } to  +         ) -context={} thecontext['Articles']=Articles *     returnRender (Request,'index.html', context)

The interaction between SQL and DB in Django--Non-ORM

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.