Python study note 08: Install django and pythondjango
Install django in linux:
Sudo pip install django
Install django in windows:
Pip install django
Verify that django is installed:
Python-m django -- version
Switch the directory to E: \ SourceCode and create a new project named mysite:
Django-admin startproject mysite
Switch to the mysite directory and run the mysite project:
Python manage. py runserver
In the mysite project, create a polls application:
Python manage. py startapp polls
Start writing the first page and open polls/views. py:
from django.shortcuts import renderfrom django.http import HttpResponse# Create your views here.def index(request): return HttpResponse("Hello, world. You're at the polls index.")
At this time, django cannot access the view of the polls application. Because the routing information is missing, configure the routing information as follows:
Add and enable polls/urls. py:
from django.conf.urls import urlfrom . import viewsurlpatterns = [ url(r'^$', views.index, name='index'),]
Open mysite/urls. py:
from django.conf.urls import include, urlfrom django.contrib import adminurlpatterns = [ url(r'^polls/', include('polls.urls')), url(r'^admin/', admin.site.urls),]
There are several ways to add a route, which can be used according to different situations.
Function views ):
1. First import the view module, from. import views
2. Insert a new route in urlpatterns, url (R' ^ $ ', views. index, name = 'index ')
Class-based views ):
1. First import the view module, from other_app.views import Home
2. Insert a new route in urlpatterns, url (R' ^ $ ', Home. as_view (), name = 'home ')
Third (Including another URLconf ):
1. First import the include () function, from django. conf. urls import include, url
2. Insert a new route in urlpatterns, url (r '^ polls/', include ('polls. urls '))
Initialize the database of the mysite project first:
Python manage. py migrate
After the configuration is complete, run the mysite project:
Python manage. py runserver