搭建環境、創建應用點擊打開鏈接
打開views.py,做如下修改:
#coding:utf-8
from django.http import HttpResponse
def index(request):
return HttpResponse(u"歡迎光臨")
第一行是聲明編碼爲utf-8, 因爲我們在代碼中用到了中文,如果不聲明就報錯.
第二行引入HttpResponse,它是用來向網頁返回內容的,就像Python中的 print 一樣,只不過 HttpResponse 是把內容顯示到網頁上。
打開urls.py
from django.conf.urls import url
from django.contrib import admin
from learn import views as blong_views # new
urlpatterns = [
url(r'^$', blong_views.index), # new
url(r'^admin/', admin.site.urls),
]
在終端運行python manage.py runserver,會看到如下信息:
^CNuoYanmac-3:mysite wcj$ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
March 17, 2017 - 03:44:20
Django version 1.10.6, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
我們打開瀏覽器,訪問 http://127.0.0.1:8000/,會在頁面上看到‘歡迎光臨’四個字。