标签:python import please django
一、创建django程序
终端命令:django-admin startproject sitename
IDE创建Django程序时,本质上都是自动执行上述命令
二、程序目录
配置:
root@kali:~/PycharmProjects/untitled2# python manage.py startapp app01
"""untitled2 URL Configuration The `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 url from django.contrib import admin from app01 import views urlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r"^home/",views.home) ]
from django.shortcuts import render # Create your views here. from django.shortcuts import HttpResponse def home(request): return HttpResponse("ok")
python manage.py runserver 127.0.0.1:8000
浏览器访问效果:
三、配置文件
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常用匹配语法:
from django.shortcuts import render # Create your views here. from django.shortcuts import HttpResponse def 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))
"""untitled2 URL Configuration The `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 url from django.contrib import admin from app01 import views urlpatterns = [ 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) ]
网页演示效果:
标签:python import please django
原文地址:http://9272317.blog.51cto.com/9262317/1834810