Django学习(一)一首情诗

  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项目的树结构:


Django学习(一)一首情诗_第1张图片
poem项目的树结构

在urls.py的urlpatterns列表中添加:

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

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

from django.http import HttpResponse
 
def output(request):
    title = "

When You Are Old

" author = "

William Butler Yeats

" content = """ When you are old and grey and full of sleep,
And nodding by the fire, take down this book,
And slowly read, and dream of the soft look
Your eyes had once, and of their shadows deep;
How many loved your moments of glad grace,
And loved your beauty with love false or true,
But one man loved the pilgrim soul in you,
And loved the sorrows of your changing face;
And bending down beside the glowing bars,
Murmur, a little sadly, how love fled
And paced upon the mountains overhead
And hid his face amid a crowd of stars.
""" return HttpResponse([title, author, content])

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

python3 manage.py runserver 8000

  打开浏览器,输入localhost:8000,页面如下图:
  

Django学习(一)一首情诗_第2张图片
网页展示

  这样我们就完成了一个简单的django项目的开发。期待下期分享~~


本次分享到此结束,欢迎大家批评与交流~~

你可能感兴趣的:(Django学习(一)一首情诗)