python學習筆記08:安裝django

來源:互聯網
上載者:User

標籤:資料庫   res   source   初始化   應用   rtc   python學習   style   配置路由   

linux環境安裝django:

sudo pip install django

windows環境安裝django:

pip install django

驗證django是否安裝:

python -m django --version

切換目錄到E:\SourceCode,建立新項目,項目名稱為:mysite:

django-admin startproject mysite

切換到mysite目錄,運行mysite項目:

python manage.py runserver

在mysite項目中,建立polls應用:

python manage.py startapp polls

開始編寫第一個頁面,開啟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.")

此時django還不能訪問到polls應用的view,因為缺少路由資訊,下面要配置路由資訊:
新增並開啟polls/urls.py:

from django.conf.urls import urlfrom . import viewsurlpatterns = [    url(r‘^$‘, views.index, name=‘index‘),]

開啟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),]

添加路由的方法,有幾種,根據不同的情況來使用。

第一種(Function views):

1.首先匯入view的模組,from . import views

2.在urlpatterns中插入新的路由,url(r‘^$‘, views.index, name=‘index‘)

第二種(Class-based views):

1.首先匯入view的模組,from other_app.views import Home

2.在urlpatterns中插入新的路由,url(r‘^$‘, Home.as_view(), name=‘home‘)

第三種(Including another URLconf):

1.首先匯入include()函數,from django.conf.urls import include, url

2.在urlpatterns中插入新的路由,url(r‘^polls/‘, include(‘polls.urls‘))

先初始化mysite項目的資料庫:

python manage.py migrate

配置完成後,運行mysite項目:

python manage.py runserver

 

python學習筆記08:安裝django

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.