Django学习(1)一首情诗

Django是Python中Web开发的最有代表性的框架之一。本文将简单介绍如何入门Django开发。
首先确保你的linux系统已安装django模块。打开Python3,利用以下命令可找到django所在目录:

>>> import django
>>> print(django.__path__)

笔者显示的是:

['/usr/lib/python3/dist-packages/django']

接下来开始django开发!首先切换到django目录,新建poem项目:

django-admin.py startproject poem

输入tree poem,查看poem项目的树结构:
在这里插入图片描述
在urls.py的urlpatterns列表中添加:

url(r'^$', 'poem.views.output'),

在/poem/poem文件夹下,创建views.py文件,输入代码:

from django.http import HttpResponse
 '''
遇到不懂的问题?Python学习交流群:821460695满足你的需求,资料都已经上传群文件,可以自行下载!
'''
def output(request):
   title = "<h1>When You Are Old</h1>"
   author = "<h2>William Butler Yeats</h2>"
   content = """
         When you are old and grey and full of sleep,<br/>
         And nodding by the fire, take down this book,<br/>
         And slowly read, and dream of the soft look<br/>
         Your eyes had once, and of their shadows deep;<br/>
         How many loved your moments of glad grace,<br/>
         And loved your beauty with love false or true,<br/>
         But one man loved the pilgrim soul in you,<br/>
         And loved the sorrows of your changing face;<br/>
         And bending down beside the glowing bars,<br/>
         Murmur, a little sadly, how love fled<br/>
         And paced upon the mountains overhead<br/>
         And hid his face amid a crowd of stars.<br/>
         """
   return HttpResponse([title, author, content])

进入poem文件夹,输入命令:

python3 manage.py runserver 8000

打开浏览器,输入localhost:8000,页面如下图:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值