Django学习(1)一首情诗

简介:   Django是Python中Web开发的最有代表性的框架之一。本文将简单介绍如何入门Django开发。   首先确保你的linux系统已安装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项目的树结构:
这里写图片描述
在urls.py的urlpatterns列表中添加:

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

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

from django.http import HttpResponse

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,页面如下图:
  这里写图片描述
  这样我们就完成了一个简单的django项目的开发。期待下期分享~~



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

目录
相关文章
|
7月前
|
存储 缓存 数据库
Django框架学习(四)
Django框架学习(四)
|
7月前
|
前端开发 关系型数据库 MySQL
Django框架学习(三)
Django框架学习(三)
|
7月前
|
JSON 前端开发 数据库
Django框架学习(一)
Django框架学习(一)
|
4月前
|
调度 Apache 数据库
Django 学习教程-第一个 Django 应用
本章主要讲述了如何创建一个Django项目,以一个投票案例程序介绍了Django的请求和响应流程。
51 1
|
4月前
|
Python
Django 学习教程-介绍与安装
本章介绍了Django是什么,以及为什么使用Django和安装Django
40 1
Django 学习教程-介绍与安装
|
9月前
|
Python
【Django学习】(十六)session_token认证过程与区别_响应定制
【Django学习】(十六)session_token认证过程与区别_响应定制
|
5月前
|
前端开发 数据库 网络架构
Python Django框架中文教程:学习简单、灵活、高效的Web应用程序框架
Python Django框架中文教程:学习简单、灵活、高效的Web应用程序框架
|
7月前
|
存储 NoSQL 中间件
Django框架学习(二)
Django框架学习(二)
|
7月前
|
关系型数据库 MySQL 数据库
基于 Python Django 的在线编程学习平台(仿慕课网)
基于 Python Django 的在线编程学习平台(仿慕课网)
|
9月前
|
JSON API 数据安全/隐私保护
【Django学习】(十五)API接口文档平台_项目流程分析_日志器_认证_授权
【Django学习】(十五)API接口文档平台_项目流程分析_日志器_认证_授权