[关闭]
@beier 2015-12-12T09:16:02.000000Z 字数 3342 阅读 1343

HelloWorld tutorial for Django

web


执行命令默认都是在终端下,本教程适合linux及mac os下练习

1.安装django

推荐使用pip安装:pip install django
检查django已安装版本:python -c "import django; print(django.get_version())"

2.创建HelloWorld工程

执行cd命令(cd projects)进入相应目录,然后执行django-admin startproject HelloWorld,该命令将在当前目录下新建HelloWorld工程目录,其中包含manage.py文件(project管理)、HelloWorld文件夹(存放project的相关配置文件),如下所示:

  1. HelloWorld/
  2. manage.py
  3. HelloWorld/
  4. __init__.py
  5. settings.py
  6. urls.py
  7. wsgi.py

3.创建hellos应用

执行cd HelloWorld命令进入到manage.py文件所在目录,可通过ls命令查看当前目录是否包含manage.py文件来确认。然后执行python manage.py startapp hellos来创建django应用hellos,该命令将在当前目录下新建hellos应用目录,包含应用的相关配置文件,如下所示:

  1. hellos/
  2. __init__.py
  3. admin.py
  4. apps.py
  5. migrations/
  6. __init__.py
  7. models.py
  8. tests.py
  9. views.py

4.编辑views.py文件,添加view调用函数

  1. from django.shortcuts import render
  2. def hello(request):
  3. return render(request, 'hellos/hello.html')
  4. # 后面将介绍html文件的创建
  5. def hello_world(request):
  6. if request.POST['greet']:
  7. greet = request.POST['greet']
  8. name = request.POST['name']
  9. # 从字典request.POST取回用户输入的内容,‘greet’和‘name’作为字典的key在表单form中的input定义
  10. else:
  11. greet = 'Hello'
  12. name = 'Nobody'
  13. return render(request, 'hellos/hello_world.html', {'greet': greet, 'name': name})
  14. # 字典{'greet': greet, 'name': name}构建template文件中greet、name与函数中greet、name参数的索引,template文件中的字符'greet'、'name'作为字典的key。

hello函数返回hello.html文件的浏览页面,页面将会提供一个可供用户输入的form表单,并将用户输入内容放入request.POST字典,然后提交到hello_world函数所绑定的url地址;hello_form函数将根据form表单中input的name属性内容来取出字典request.POST中对应的用户输入,并赋值给相应变量,hello_world.html文件取得变量的值,并在浏览器显示页面。

5.url绑定

编辑应用目录下的urls.py文件,如下:

  1. from django.conf.urls import url
  2. from . import views
  3. app_name = 'hellos'
  4. # 方便在template文件中使用{% url 'hellos:hello_world' %},hellos是应用绑定名称,hello_world是url绑定名称
  5. urlpatterns = [
  6. url(r'^hello/$', views.hello, name='hello'),
  7. url(r'^hello_world/$', views.hello_world, name='hello_world'),
  8. ]

编辑工程配置目录下的urls.py文件,如下:

  1. from django.conf.urls import include, url
  2. urlpatterns = [
  3. url(r'^hellos/', include('hellos.urls')),
  4. # 包含应用hellos下的urls.py文件
  5. ]

6.创建view函数的template

在hellos应用目录下新建templates文件夹,在templates文件夹下新建hellos文件夹,在里面创建所需的模板文件hello.html与hello_world.html,目录结构大致如下:

  1. HelloWorld/
  2. manage.py
  3. HelloWorld/
  4. hellos/
  5. templates/
  6. hellos/
  7. hello.html
  8. hello_world.html

编辑hello.html文件,最后如下

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Hello!Form</title>
  6. </head>
  7. <body>
  8. <h1 align="center" style="color: deepskyblue;">Fill Out This Form</h1>
  9. <form action="{% url 'hellos:hello_world' %}" method="post" align="center" style="color: green;font-size: 1.7em;">
  10. {% csrf_token %}
  11. <label for="greeting" style="display: inline-block;width: 170px;">A Greeeting:</label>
  12. <input type="text" name="greet" id="greeting" style="font-size: 1em;">
  13. <br/>
  14. <label for="names" style="display:inline-block;width: 170px;">Your Name:</label>
  15. <input type="text" name="name" id="names" style="font-size: 1em;">
  16. <br/>
  17. <input type="submit" style="color: orange;">
  18. </form>
  19. </body>
  20. </html>

这里需要注意的是form表单中的action内容指向view函数hello_world所绑定的url地址,即提交完表单后跳转的url地址,方法使用的是post,因此添加{% csrf_token %}防止伪提交;表单中用户输入的内容将存储在字典request.POST提交到跳转的url地址,input中name的内容将作为request.POST字典的key,而与key绑定的value就是input中用户输入的内容。

编辑hello_world.html文件,最后如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Greet</title>
  6. </head>
  7. <body>
  8. <p align="center" style="color: green;font-size: 3em">I just want to say <em style="color: deepskyblue;">{{ greet }}! {{ name }}</em>.</p>
  9. <p align="center"><input type="submit" value="重新填写" onclick="window.history.back()" align="center" style="color: peru;"></p>
  10. </body>
  11. </html>

7.演示

工程目录下,即与manage.py文件同一目录下执行:python manage.py runserver
根据提示在浏览器输入相应的地址,默认为http://127.0.0.1:8000/hellos/hello,输入相应内容,提交后即可看到。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注