1. Install the virtual environment

sudo pip install virtualenv
Copy the code

2. Install the virtual environment development package

sudo pip install virtualenvwrapper
Copy the code

3. Edit the ~/. Bashrc file and append it at the end

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
Copy the code

4. Use source.bashrc to take effect

source.bashrc
Copy the code

5. Create a Python3 virtual environment

Mkvirtualenv -p python3 Name of the virtual environmentCopy the code

6. Go to the created virtual environment

Workon Indicates the name of a virtual environmentCopy the code

7. Check how many virtual environments exist on the local computer

Workon + space + two TAB keysCopy the code

8. Launch the current virtual environment

Deactivate the name of the virtual environmentCopy the code

9. Delete a virtual environment

Rmvirtualenv Virtual environment nameCopy the code

10. Name/module of the virtual environment installation package

PIP install package nameCopy the code

11. Check which packages are installed

pip list
pip freeze
Copy the code

12. Install the Django environment

PIP install django = = 1.8.2Copy the code

13. To create a Django project, you must create it after entering the virtual environment

Django-admin startProject Specifies the project nameCopy the code

14. Create a Django application

Python manage.py startApp Application nameCopy the code

15 Modify the INSTALLED APPS configuration item in settings.py to append the newly registered application.

'App just created'Copy the code

16. Start the project

python manage.py runserver
Copy the code

17. Design model classes using Djangos built-in ORM framework

18. Generate migration files. The first migration file appears in the migrations directory with the name 0001_initial.py

python manage.py makemigrations
Copy the code

19. Perform the migration to generate a table. The default name of the table is lowercase application name

python manage.py migrate
Copy the code

20. Use the model class to operate the data table, static shell command:

python manage.py shell
Copy the code

21. Operation Steps:

Export the designed classes from the model
from booktest.models import BookInfo,HeroInfo

1Write a single piece of data to the booktest_bookinfo table. B = BookInfo()Define an object of class BookInfo
	b.btitle ='Heavenly Dragon eight Bu'  Define and assign attributes to object B
	b.bpub_date = date(1990.10.11) 
	b.save()  Save the data to the database

2Select test_bookinfo from test_bookinfoidfor1The data. b = BookInfo.objects.get(id=1) 

3) change the publication date of b's book based on the previous step. b.bpub_date = date(1989.10.21)
	b.save()  Update table data

4) Next, delete the data of the book corresponding to B. b.delete()Select * from table where id = 1

5Insert a single piece of data into the booktest_heroInfo table. h = HeroInfo()Generate a HeroInfo object
	h.hname = 'the guo jing'  # Assign to the object
	h.hgender = FalseH comment = 'bookinfo.objects.get (id=2) Create an object with id==2 to store all books in BookInfo with id==2
	h.hbook = b2  # assign the relational property to the book object to which the hero belongs.
	h.save() 

6) Queries all contents of the book table. BookInfo.objects.allHeroinfo.objects (); heroinfo.objects;all(a)7) query outidfor2Information about all the heroes in the book. b = BookInfo.objects.get(id=2)
	b.heroinfo_set.all(a)Select * from b books
Copy the code

22. Modify the time zone and localization in settings.py

LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
Copy the code

23. Create an administrator

python manage.py createsuperuser
Copy the code

24. Register the model classes in the admin file of your application and tell the Django framework to generate the corresponding admin interface based on the registered model classes

From booktest.models import LOLcity from booktest.models import LOLhero 2. # Register your models here.admin.site. Register (LOLhero) admin.site. Register (LOLcity)Copy the code

25. Run the server: enter http://127.0.0.1:8000/admin/ into the admin interface

26. Optimize the display by modifying the __str__ magic method in each function of the template to return what it wants to return

Def __str__(self): # return selfCopy the code

27. Optimize the background management display, display more management information, and include management class assignment declaration in the registration information

Class LOLcityAdmin(admin.modeladmin): List_display = ["id", "cityname", "citycreate"] # class LOLheroAdmin(admin.modeladmin): list_display = ["id", "hname", "hgender", "hcomment", "hcity"] # Register your models here. admin.site.register(LOLhero, LOLheroAdmin) admin.site.register(LOLcity, LOLcityAdmin)Copy the code

View section:

28. Create a new url in your app and add information about the urls in your system file

From Django.conf. urls import url # Strictly matches the beginning and end of urlpatterns = []Copy the code

29. Modify the urls in the system file to add the path to the newly created urls

Urlpatterns = [URL (r'^admin/', include(admin.site.urls))), #include Contains urls in your app (r'^', include('booktest.urls'))]Copy the code

30. When entering the URL, the system will automatically match the regular path in the system urls first, once the match will enter the path, then enter the application urls match, so first set up an index path, remember to strictly match the beginning and end

Urlpatterns = [url(r'^index$', views.index), Create /index and view index.Copy the code

31. After the match is successful, execute the view item in views.index

from django.shortcuts import render from django.http import HttpResponse from booktest.models import LOLcity # Create your views here. def index(request): Return render(request, 'booktest/index.html', {"content": render(request, 'booktest/index.html') "Hello world", "list": list(range(10))})Copy the code