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& ...
随机推荐
- jquery操作select取值赋值与设置选中[转]
本节内容:jquery实现select下拉框的取值与赋值,设置选中的方法大全. 比如<select class="selector"></select> 1 ...
- [Forward]Ten Caching Mistakes that Break your App
Caching large objects, duplicate objects, caching collections, live objects, thread unsafe caching a ...
- 命令行模式启动VMWare虚拟机
工作中使用到在centos中安装vmware Workstation部署虚拟机,以前都是使用图形界面启动虚拟机,由此要调整VNC的分辨率大小,重启VNC Server后所有虚拟机都关闭了.事后分析可能 ...
- Linux下硬链接与软链接
linux下的链接文件,尤其是软链接使用非常的频繁: 链分为硬链接(hard link)与软链接(symbolic link) 两种:关键在于inode: 硬链接: 当系统需要读取一个文件时,就会去读 ...
- C# 计算代码运行时间
Stopwatch watch = new Stopwatch(); watch.Start(); int num = myhelper.MySQLExecuteNonQuery(comlist); ...
- html固定宽度下拉框内容显示不全问题解决方法
不少时候在页面中为了布局的需要,下拉列表<select>的宽度需要设成比较小的值,这时如果恰巧它包含的选择项<option>的内容比较长,那么超出select宽度的部分将会被截 ...
- iOS: 定义 Block
定义 typedef void (^RFAudioBasicBlock) (void); typedef void (^RFAudioSuccessBlock) (BOOL flag); typede ...
- Xcode: Show Bounds Rectangles for UIView in Interface Builder
选中一个 Xib 文件,然后依次选择菜单中的 Editor - Canvas - Show Bounds Rectangles
- SpringBoot整合cxf发布webService
1. 看看项目结构图 2. cxf的pom依赖 1 <dependency>2 <groupId>org.apache.cxf</groupId>3 <art ...
- tomcat设置debug模式
1.设置 编辑catalina.bat,在 rem Guess CATALINA_HOME if not definedset "CURRENT_DIR=%cd%"if not & ...