Django 添加应用
一个项目可以添加多个应用,可以使用以下两种方法来添加应用:
[root@localhost web]$ python manage.py startapp blog
[root@localhost web]$ django-admin.py startapp blog
[root@localhost web]$ ll blog
-rw-r--r--. 1 root root 63 Jan 26 08:47 admin.py # 应用的后台管理站点配置
-rw-r--r--. 1 root root 0 Jan 26 08:47 __init__.py # 表明这是一个包
-rw-r--r--. 1 root root 57 Jan 26 08:47 models.py # 模型,负责业务对象与数据库的映射
-rw-r--r--. 1 root root 60 Jan 26 08:47 tests.py # 用于开发测试,编写单元测试
-rw-r--r--. 1 root root 63 Jan 26 08:47 views.py # 视图,负责与用户的交互,定义url
在项目的配置文件中添加该应用:
[root@localhost web]$ vim web/settings.py
INSTALLED_APPS = (
......
'blog',
)
在项目的视图文件中添加应用的URL:
[root@localhost web]$ vim web/urls.py
urlpatterns = patterns('',
......
url(r'^blog/$', 'blog.views.index'), # 表明当我访问 http://192.168.216.128:8000/blog 时,会去访问blog应用目录下的views文件里的index函数
)
[root@localhost web]$ cat blog/views.py # 在views文件中定义index函数
from django.shortcuts import render
from django.http import HttpResponse # Create your views here. def index(request): # request表示用户对页面的请求
return HttpResponse("<h1> Hello World </h1>") # HttpResponse()方法用于返回信息给用户请求
启动项目并访问应用:
[root@localhost web]$ python manage.py runserver 0.0.0.0:8000

如下,我们是直接把HTML写在视图文件中,但实际中我们会用一个目录来保存这些HTML文件
[root@localhost web]$ cat blog/views.py
from django.shortcuts import render
from django.http import HttpResponse def index(request):
return HttpResponse("<h1> Hello World </h1>")
首先创建一个模板目录,使用该目录来存放HTML文件,目录名固定是 templates:
[root@localhost web]$ mkdir blog/templates
[root@localhost web]$ cat blog/templates/index.html
<html> <head>
<title>我的第一个HTML页面</title>
</head> <body>
<h1> Hello World ! </h1>
<h1> Hello Django ! </h1>
</body> </html>
改写视图文件:
[root@localhost web]$ cat blog/views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader, Context # loader()方法用来导入模板的HTML文件
# Context()方法用来解析模板的HTML文件
def index(request):
t = loader.get_template('index.html') # 加载HTML文件,创建一个模板对象
c = Context({}) # 解析上下文,因为HTML文件中没有定义变量,所以不需要解析
return HttpResponse(t.render(c)) # 最后返回渲染的模板
启动项目并访问应用:
[root@localhost web]$ python manage.py runserver 0.0.0.0:8000

Django 添加应用的更多相关文章
- django 添加comments app
django 添加comments app 参看 django comments 文档 安装和配置comments 1.安装comments,运行:pip install django-contrib ...
- django 添加动态表格的方法
传统方法(基于方法的视图):http://stellarchariot.com/blog/2011/02/dynamically-add-form-to-formset-using-javascrip ...
- Django添加Last-Modified和ETag
用Django REST Framework做的REST API,其中有个API有这样的需求: APP端请求这个API,服务器端从数据库读数据,返回json.返回的数据量稍微有些大,但是可能一年才修改 ...
- django添加静态文件
最近做了一个todolist webapp,需要稍微添加css时候又忘记django的添加方法了,查看了以前的项目才想起来,所以记录一下. 1.settings.py 将以下代码放到最下面 STATI ...
- Django添加ckeditor富文本编辑器
源码 https://github.com/django-ckeditor/django-ckeditor 通过pip安装. pip3 install django-ckeditor pip3 ins ...
- django添加装饰器
引入模块: from django.utils.decorators import method_decorator 添加:@method_decorator(func) from django.ut ...
- Django添加tinyMCE编辑器
tinymce的使用方法很简单,只需要在html页面中包含如下: <!-- Place inside the <head> of your HTML --> <scrip ...
- Python - Django - 添加首页尾页上一页下一页
添加首页和尾页: views.py: from django.shortcuts import render from app01 import models def book_list(reques ...
- Python - Django - 添加作者
在 book_list.html 的页面下方加上 “添加作者” 的链接 <!DOCTYPE html> <html lang="en"> <head& ...
随机推荐
- ggplot2 pdf import in Adobe Illustrator missing font AdobePiStd
The font AdobePiStd is missing. Affected text will be displayed using a substitute font. 缺少字体 AdobeP ...
- html5和css3实现的3D滚动特效
今天给大家带来一款html5和css3实现的3D滚动特效.效果图如下: 在线预览 源码下载 实现的代码. html代码: <div class="container"&g ...
- SQL server 分页方法小结
这里面介绍一下常用的分页方法: 1.使用top来分页 select top @pageSize * from table where id not in (select top @pageSize*( ...
- (笔记)Linux服务器中判断客户端socket断开连接的方法
下面来罗列一下判断远端已经断开的方法:(转自http://blog.csdn.net/god2469/article/details/8801356) 法一: 当recv()返回值小于等于0时,soc ...
- (笔记)arm-linux-gcc/ld/objcopy/objdump参数总结
说明:gcc是编译器,负责对c代码的编译, ld是连接器 负责将多个*.o的目标文件链接成elf可执行文件.elf可执行文件是unix常用的可执行文件类型,就像windows的exe文件.elf文件中 ...
- Maven目标
Maven主要目标是提供给开发人员: 项目是可重复使用,易维护,更容易理解的一个综合模型. 插件或交互的工具,这种声明性的模式. Maven项目的结构和内容在一个XML文件中声明,pom.xml 项目 ...
- C++ namespace的用法
//namesp.h namespace pers{ const int LEN = 40; struct Person{ char fname[LEN]; ...
- 大数据:Spark Core(二)Driver上的Task的生成、分配、调度
1. 什么是Task? 在前面的章节里描写叙述过几个角色,Driver(Client),Master,Worker(Executor),Driver会提交Application到Master进行Wor ...
- MySQL库和表的管理
MySQL数据库服务配置好后,系统会有4个默认的数据库. information_schema:虚拟对象,其对象都保存在内存中performance_schema:服务器性能指标库mysql:记录用户 ...
- spring + springMVC + spring Data + jpa + maven 项目框架搭建
首先看一下项目结构: 所用到的jar(pom.xml): <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:x ...