1. 程式人生 > >python web筆記(二)

python web筆記(二)

python

python web筆記(二)

=================================================================================================

三、Django創建App

=================================================================================================

1、首先使用django-admin創建好一個django項目

django-admin startproject test01


2、在test01目錄下面執行命令

D:\python2.7.13\exercise\test01>python manage.py startapp cmdb #app名稱為cmdb

D:\python2.7.13\exercise\test01>python manage.py startapp openstack


3、查看目錄(使用pycharm打開查看),確認是否創建成功!

技術分享


4、實現瀏覽器中訪問項目cmdb

1) 新創建的cmdb App中的目錄有:

技術分享

2) 修改cmdb中的views.py文件,具體配置如下:

from django.shortcuts import render
from django.shortcuts import HttpResponse  #加入

# Create your views here.
def home(request):                         #定義函數home
    return HttpResponse(‘<h1>這是我的第一個Django--App程序!!!<h1>‘)                        HttpResponse()

3) 修改項目中的urls.py文件

from django.conf.urls import url
from django.contrib import admin
from django.shortcuts import HttpResponse  #導入HttpServer模塊
import time                                #加入
from cmdb import views                    #導入cmdb app中的views
def home(request):                         #定義函數home
    return HttpResponse(‘<h1>這是我的第一個Django程序!!!<h1>‘)

urlpatterns = [
    url(r‘^admin/‘, admin.site.urls),
    url(r‘^index.html‘,home),           #添加index.html
    url(r‘^cmdb‘,views.home),            #將cmdb app添加到url中
]           ]

4) 開啟django服務器:

5) 瀏覽器訪問測試:

技術分享


本文出自 “doublelinux” 博客,謝絕轉載!

python web筆記(二)