码迷,mamicode.com
首页 > 移动开发 > 详细

2、编写第一个django app

时间:2016-01-26 23:39:04      阅读:416      评论:0      收藏:0      [点我收藏+]

标签:

1、创建project

 

django-admin startproject mysite

 

输入以上命令后, 在当前目录下生成 mysite 目录 和 相应文件:

[root@learn-python django]# tree .
.
└── mysite    项目容器, django 不关注, 可随意命名
    ├── manage.py   一种命令行工具,允许你以多种方式与该 Django 项目进行交互,可用 python manage.py help 查看使用方式
    └── mysite      项目的python package 实际存放的位置, import package 时需要用到,如(import mysite.urls
        ├── __init__.py  空文件, 告诉python 这个目录是一个python package
        ├── settings.py  本django 项目的配置
        ├── urls.py      项目url 声明, 也就是django 路由设置
        └── wsgi.py      一个wsgi 兼容服务器的入口点

2 directories, 5 files

 

2、开启web server 服务

[root@learn-python mysite]# python manage.py  runserver  0.0.0.0:8000
Performing system checks...

System check identified no issues (0 silenced).
January 26, 2016 - 14:16:55
Django version 1.8.2, using settings ‘mysite.settings‘
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

浏览器访问:

技术分享

 

3、创建 app

由上, 现在已经创建了一个项目, 可以开始编写应用(app)。  django 中的每个app 为一个package (即一个目录), 可通过命令生成

python manage.py startapp polls

[root@learn-python mysite]# tree ./polls/
./polls/
├── admin.py
├── __init__.py
├── migrations
│   └── __init__.py
├── models.py
├── tests.py
└── views.py


编写视图  polls/views.py

# cat ./polls/views.py
from django.http import HttpResponse
def index(request):
    return HttpResponse("Hello, world. You‘re at the polls index.")

编写URL配置 polls/urls.py

# cat ./polls/urls.py
from django.conf.urls import url

from . import views

urlpatterns = [
    url(r‘^$‘, views.index, name=‘index‘),
]


在mysite 下URL配置文件中设置转发:

# cat mysite/urls.py
"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r‘^$‘, views.home, name=‘home‘)
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r‘^$‘, Home.as_view(), name=‘home‘)
Including another URLconf
    1. Add an import:  from blog import urls as blog_urls
    2. Add a URL to urlpatterns:  url(r‘^blog/‘, include(blog_urls))
"""
from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r‘^admin/‘, include(admin.site.urls)),
    url(r‘^polls/‘, include(‘polls.urls‘)),
]

 

开启web server 服务

# python manage.py  runserver  0.0.0.0:8000
Performing system checks...

System check identified no issues (0 silenced).
January 26, 2016 - 14:28:09
Django version 1.8.2, using settings ‘mysite.settings‘
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

 

上面中访问http://0.0.0.0:8000/polls ,  将跳转到polls.urls 解析路由, 即 url(r‘^$‘, views.index, name=‘index‘)

 

页面访问测试

技术分享

 

2、编写第一个django app

标签:

原文地址:http://www.cnblogs.com/lowry-zhuang/p/5161835.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!