Python's web framework:
Django
One. Django
Django is an excellent next-generation web framework
Django's processing flow
1.
python:https://www.python.org/downloads/
django:https://www.djangoproject.com/download/
Note: Before installing Django, you must install Python first.
2. Installation
Install under Windows: (Use Pycharm here)
>> PIP3 Install Django
To add an environment variable:
Add these directories to the system environment variable:
C:/python33/lib/site-packages/django; C:/python33/scripts. Once added, you can use the Django django-admin.py command to create a new project.
Check to see if the installation was successful:
3. Create Project
>django-admin.py Startproject MySite
Note: Since previously created here using Mysite2
This will automatically generate several directories:
In Django, the basic project contains four files:
Mysite2
__init__.py: Tell Python that this is a package
settings.py: Project-related configuration files
urls.py: Global URL configuration (routing system)
Wsgi.py:WSGI, WSGI socket is used by default
Manage.py:django Program startup file
4. Create an App
You can create multiple apps under one project
> Python manage.py startapp CMDB #创建名为cmdb的app
This will see the CMDB that has been created:
Auto-generated application file under CMDB:
__init__.py: Tell Python is a package
admin.py: Default Background management
apps.py: Configuration file for current app
models.py: Data-related (data model)
tests.py: Unit Test
Views: View functions, processing business requests.
5. Writing code
So far the project has been created and the App,urls and views.py files currently have no real code and need to add code after that.
Custom page:
urls.py: Add URL path, default to Admin
1 fromDjango.conf.urlsImportURL2 fromDjango.contribImportAdmin3 fromCmdbImport views4Urlpatterns = [5 #URL (r ' ^admin/', admin.site.urls),6URL (r'^index', Views.index)#Specify the default page, to the index function of the views under the CMDB7]
URLs
1 fromDjango.shortcutsImportRender2 fromDjango.shortcutsImportHttpResponse3 #You must import the HttpResponse module user to see it in the browser. Request to get the response value. 4 #Create your views here.5 #processing user requests, the default Django is automatically encapsulated and must be added to the request6 7 defIndex (Request):8 #...9 returnHttpResponse ('Hello,word')
views
4. Start the Django program
> Python manage.py runserver 127.0.0.1:8000
Prompted us to visit Http://127.0.0.1:8000/after launch, but after the interview error 404 page does not exist, because our custom page is index, plus index can:
Python's web framework Django