1. Create Project
Django-admin Startproject MySite
After entering the above command, generate the MySite directory and the corresponding file in the current directory:
[[email protected] Django]#Tree.. └──mysite Project container, Django does not care, can be arbitrarily named ├──manage.py a command-line tool that allows you to interact with the Django project in a variety of ways, using python manage.py Help View how to use The Python package for the └──mysite project is actually stored in a location that needs to be used for import, such as an ├──__init__.py empty file (import mysite.urls ) . Tell python this directory is a Python package ├──settings.py the configuration ├──urls.py Project URL declaration for this Django project , which is the Django routing setting └──wsgi.py An entry point for a WSGI compatible server 2 directories, 5 files
2. Open the Web Server service
[[email protected] mysite] # python manage.py runserver 0.0.0.0:8000performing system checks ... System Check identified no issues (0, 2016-14:16:551.8.2, using Settings ' mysite.settings ' Starting development Server at http://0.0.0.0:8000/Quit the server with CONTROL-C.
Browser access:
3. Create an App
Now that you've created a project, you're ready to start writing apps (apps). Each app in Django is a package (that is, a directory) that can be generated by commands
Python manage.py Startapp Polls
[Email protected] mysite]# tree/polls/
./polls/
├──admin.py
├──__init__.py
├──migrations
│└──__init__.py
├──models.py
├──tests.py
└──views.py
Writing views polls/views.py
# Cat./polls/views.py From django.http Import httpresponsedef Index (Request): return HttpResponse ("Hello, World.") You ' re at the polls index. ")
To write the URL configuration polls/urls.py:
# Cat./polls/urls.py = [ url (r ' ^$ ', Views.index, name=' index '),]
Set the forwarding in the URL configuration file under MySite:
#Cat mysite/urls.py"""mysite URL configurationthe ' urlpatterns ' list routes URLs to views. For more information see:https://docs.djangoproject.com/en/1.8/topics/http/urls/examples:function views 1. ADD an import:from My_app import views 2. Add a URL to Urlpatterns:url (R ' ^$ ', views.home, Name= ' home ") class-based views 1. ADD an import:from other_app.views import Home 2. Add a URL to Urlpatterns:url (R ' ^$ ', Home.as_view (), name= ' Home ') including another URLconf 1. ADD an import:from blog import URLs as Blog_urls 2. Add a URL to Urlpatterns:url (R ' ^blog/', include (Blog_urls))"""From django.conf.urls import include, Urlfrom django.contrib import adminurlpatterns=[url (r ' ^admin/', include (Admin.site.urls)), URL (r ' ^polls / ', include (' Polls.urls ')),]
To open the Web Server service
# python manage.py runserver 0.0.0.0:8000performing system checks ... System Check identified no issues (0, 2016-14:28:091.8.2, using Settings ' Mysite.sett Ings ' starting development Server at http://0.0.0.0:8000/Quit the server with CONTROL-C.
Above access http://0.0.0.0:8000/polls, will jump to polls.urls parsing route, i.e. URL (r ' ^$ ', Views.index, name=' index ' )
Page Access Testing
2. Write the first Django app