Download Pycharm,django The specific learning process in the self-improvement school has.
① New Python project, enter commands in command line cmd
django-admin.py Startproject Learn_models
Go to the Learn_models folder and create a new app named LZHC
CD Learn_models
Python manage.py Startapp LZHC
Add LZHC to the Installed_apps in settings.py
Installed_apps = ( 'Django.contrib.admin', 'Django.contrib.auth', 'Django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'Django.contrib.staticfiles', 'LZHC',)
Synchronize all databases
Python manage.py makemigrations
Python manage.py Migrate
②models.py
from Import Models class article (models. Model): = models. Charfield (max_length=20) = models. Charfield (max_length=20) def _str_ (self): return self.title
③ Project urls.py
from Import include, url from Import = [ url (r'^admin/', include (Admin.site.urls)), URL ( R'^lzhc/', include ('lzhc.urls')),]
Create urls.py under the ④LZHC folder
fromDjango.conf.urlsImportURL from.ImportViewsurlpatterns=[url (r'^query/$', views.queryall,name='Query'), url (r'^delete/$', views.delbyid,name='Detele'), url (r'^beginadd/$', views.addbyid,name='BeginAdd'), url (r'^add/$', views.add,name='Add'), url (r'^update/$', views.update,name='Update'), url (r'^beginupdate/$', views.updatebyid,name='beginupdate')]
⑤ write the method of adding and deleting and changing. Modify views.py
fromDjango.shortcutsImportRender fromDjango.shortcutsImportRender_to_response from. ModelsImportarticle fromDjango.httpImportHttpResponse fromDjango.httpImportHttpresponseredirectdefQueryall (Request): b=Article.objects.all ()returnRender_to_response ('queryall.html',{'Data': b})defDelbyid (Request): ID= Request. get['ID']; BB= Article.objects.get (id=ID) bb.delete ()returnHttpresponseredirect ("Http://127.0.0.1:8000/lzhc/query")defAddbyid (Request): ID= Request. post['ID'] Title= Request. post['title'] Author= Request. post['author'] St=article ()ifLen (ID) >0:st.id=ID; St.title=title St.author=author St.save ()returnHttpresponseredirect ("Http://127.0.0.1:8000/lzhc/query")defAdd (Request):returnRender_to_response ('add.html')defUpdate (Request): I=request. get['ID']; b=article.objects.get (id=i)returnRender_to_response ('update.html',{'Data': b})defUpdatebyid (Request): ID= Request. post['ID'] Title= Request. post['title'] Author= Request. post['author'] St=article () st.id=ID st.title=title St.author=author St.save ()returnHttpresponseredirect ("Http://127.0.0.1:8000/lzhc/query")
⑥admin.py
from Import Admin from Import Articleadmin.site.register (article)
⑦ Create a new templates folder under LZHC, and store the HTML file under the folder
Add.html
<! DOCTYPE Html>"en"> <meta charset="UTF-8"> <title> add data, submit form form </title>"http://127.0.0.1:8000/lzhc/beginadd/"Method="Post"align="Center"> <input name="ID"Type="Hidden"Value=""><br/>Please enter a title:<input name="title"Type="text"Value=""><br/>Please enter<input name="author"Type="text"Value=""><br/> <input type="Submit"Value="Submit"></form></body>Queryall.html
<! DOCTYPE Html>"en"> <meta charset="UTF-8"> <title> Data display platform </title>"1"align="Center"><tr> <td> numbering </td> <td> title </td> <td> author </td> <td> action </td>< /tr>{% forDinchData%}<tr><td>{{d.id}}</td><td>{{d.title}}</td><td>{{d.author}}</td><td > <a href="Http://127.0.0.1:8000/lzhc/delete?id={{d.id}}"> Delete </a> <a href="Http://127.0.0.1:8000/lzhc/add"> Add </a> <a href="Http://127.0.0.1:8000/lzhc/update?id={{d.id}}"> Modify </a></td> </tr>{% ENDFOR%}</table></body>Update.html
<! DOCTYPE Html>"en"> <meta charset="UTF-8"> <title> Modify personal Information </title>"http://127.0.0.1:8000/lzhc/beginupdate/"Method="Post"> <input type="Hidden"Name="ID"Value="{{data.id}}">Title:<input name="title"Type="text"Value="{{Data.title}}"><br/> <input name="author"Type="text"Value="{{Data.author}}"><br/> <input type="Submit"Value="Save"/></form></body>⑧ start the server. On the current face record, execute the command:
Python manage.py runserver
⑨ on the browser, enter the URL:
http://127.0.0.1:8000/lzhc/add/
Database additions and deletions change