python-django ----------试图和网址

来源:互联网 发布:sql语句进阶 编辑:程序博客网 时间:2024/06/06 17:08

搭建环境、创建应用点击打开链接

打开views.py,做如下修改:

#coding:utf-8from django.http import HttpResponse def index(request):    return HttpResponse(u"欢迎光临")

第一行是声明编码为utf-8, 因为我们在代码中用到了中文,如果不声明就报错.

第二行引入HttpResponse,它是用来向网页返回内容的,就像Python中的 print 一样,只不过 HttpResponse 是把内容显示到网页上。


打开urls.py

from django.conf.urls import urlfrom django.contrib import adminfrom 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 runserverPerforming system checks...System check identified no issues (0 silenced).March 17, 2017 - 03:44:20Django 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/,会在页面上看到‘欢迎光临’四个字。




1 0