標籤:python import please django
一、建立django程式
二、程式目錄
650) this.width=650;" src="http://images2015.cnblogs.com/blog/425762/201603/425762-20160306052708955-2108919642.png" width="590" height="304" alt="425762-20160306052708955-2108919642.png" />
配置:
[email protected]:~/PycharmProjects/untitled2# python manage.py startapp app01
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M00/85/77/wKiom1ekQY7QRs2AAAFmvEhJGgE620.png" title="QQ圖片20160805153352.png" alt="wKiom1ekQY7QRs2AAAFmvEhJGgE620.png" />
"""untitled2 URL ConfigurationThe `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.9/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. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r‘^blog/‘, include(‘blog.urls‘))"""from django.conf.urls import urlfrom django.contrib import adminfrom app01 import viewsurlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r"^home/",views.home)]
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M00/85/77/wKiom1ekQdaDa3ruAADqyYje75o823.png-wh_500x0-wm_3-wmp_4-s_3585479311.png" title="QQ圖片20160805153352.png" alt="wKiom1ekQdaDa3ruAADqyYje75o823.png-wh_50" />
from django.shortcuts import render# Create your views here.from django.shortcuts import HttpResponsedef home(request): return HttpResponse("ok")
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M00/85/77/wKioL1ekQhjBfuv0AABXc_s-Yik421.png" title="QQ圖片20160805153352.png" alt="wKioL1ekQhjBfuv0AABXc_s-Yik421.png" />
python manage.py runserver 127.0.0.1:8000
瀏覽器訪問效果:
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M01/85/77/wKioL1ekRIfy5wSvAAB3VH4Nt2Q929.png-wh_500x0-wm_3-wmp_4-s_270991957.png" title="QQ圖片20160805154640.png" alt="wKioL1ekRIfy5wSvAAB3VH4Nt2Q929.png-wh_50" />
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/85/77/wKiom1ekRK7j_EY0AACX38a4a4Y807.png-wh_500x0-wm_3-wmp_4-s_3484632269.png" title="QQ圖片20160805154640.png" alt="wKiom1ekRK7j_EY0AACX38a4a4Y807.png-wh_50" />
三、設定檔
1、資料庫
DATABASES = { ‘default‘: { #‘ENGINE‘: ‘django.db.backends.sqlite3‘, #‘NAME‘: os.path.join(BASE_DIR, ‘db.sqlite3‘), ‘ENGINE‘: ‘django.db.backends.mysql‘, ‘NAME‘:‘xiaoshuo‘, ‘USER‘: ‘xiaoshuo‘, ‘PASSWORD‘: ‘qiangzi()‘, ‘HOST‘: ‘mysql.litianqiang.com‘, ‘PORT‘: ‘7150‘, }}
2、模版
TEMPLATE_DIRS = ( os.path.join(BASE_DIR,‘templates‘), )
3、靜態檔案
STATICFILES_DIRS = ( os.path.join(BASE_DIR,‘static‘), )
路由系統
1、每個路由規則對應一個view中的函數
url(r‘^index/(\d*)‘, views.index),url(r‘^manage/(?P<name>\w*)/(?P<id>\d*)‘, views.manage),url(r‘^manage/(?P<name>\w*)‘, views.manage,{‘id‘:333}),
2、根據app對路由規則進行一次分類
url(r‘^web/‘,include(‘web.urls‘)),
url常用匹配文法:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/85/78/wKiom1ekU36zKBEMAAFeNl6Db4s983.png-wh_500x0-wm_3-wmp_4-s_140676705.png" title="QQ圖片20160805154640.png" alt="wKiom1ekU36zKBEMAAFeNl6Db4s983.png-wh_50" />
from django.shortcuts import render# Create your views here.from django.shortcuts import HttpResponsedef home(request): return HttpResponse("ok")def year_archive(request,year): print "<<<<<<<<",year,">>>>>>>>" return HttpResponse(year)def month_archive(request,year,month): print ">>>>>>>",year,">>>>>",month return HttpResponse("{}{}".format(year,month))def article_detial(request,year,month,article,): print ">>>>>{}>>{}>>>{}>>>>".format(year,month,article,) return HttpResponse("{}/{}/{}".format(year,month,article,))def nyr(request,year,month,article,shifen): print "----{}-{}-{}-{}".format(year,month,article,shifen) return HttpResponse("{}/{}/{}/{}".format(year,month,article,shifen))
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/85/78/wKiom1ekU6ywRp8AAAGTSQu7V5k217.png-wh_500x0-wm_3-wmp_4-s_3420938179.png" title="QQ圖片20160805154640.png" alt="wKiom1ekU6ywRp8AAAGTSQu7V5k217.png-wh_50" />
"""untitled2 URL ConfigurationThe `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.9/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. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r‘^blog/‘, include(‘blog.urls‘))"""from django.conf.urls import urlfrom django.contrib import adminfrom app01 import viewsurlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r"^home/",views.home), url(r"^artiles/([0-9]{4})/$",views.year_archive), url(r"^artiles/([0-9]{4})/([0-9]{2})/([0-9]{2})/$",views.article_detial), url(r"^artiles/([0-9]{4})/([0-9]{2})/$",views.month_archive), url(r"^artiles/([0-9]{4})/([0-9]{2})/([0-9]{2})/([0-9]{2}:[0-9]{2})/$",views.nyr)]
網頁示範效果:
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M02/85/78/wKiom1ekU-vAnqYuAABzCviaAjQ028.png-wh_500x0-wm_3-wmp_4-s_436930500.png" title="QQ圖片20160805154640.png" alt="wKiom1ekU-vAnqYuAABzCviaAjQ028.png-wh_50" />
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M02/85/78/wKioL1ekVBWwsZJcAAB1HTOZrCc283.png-wh_500x0-wm_3-wmp_4-s_2989508090.png" title="QQ圖片20160805154640.png" alt="wKioL1ekVBWwsZJcAAB1HTOZrCc283.png-wh_50" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/85/78/wKiom1ekVDLgybeVAABvQfjZf7s669.png-wh_500x0-wm_3-wmp_4-s_3316228610.png" title="QQ圖片20160805154640.png" alt="wKiom1ekVDLgybeVAABvQfjZf7s669.png-wh_50" />
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M00/85/78/wKiom1ekVFizuv2nAABy4rJlWoY206.png-wh_500x0-wm_3-wmp_4-s_2640160988.png" title="QQ圖片20160805154640.png" alt="wKiom1ekVFizuv2nAABy4rJlWoY206.png-wh_50" />
django 基礎學習