Django 初探

本文详细介绍了如何在Python 2.7环境下安装并配置Django 1.10.2,包括创建项目、运行服务器及基本的应用开发流程,适合Django初学者快速上手。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Django    

      Python下有许多款不同的 Web 框架。Django是重量级选手中最有代表性的一位。许多成功的网站和APP都基于Django。Django是一个开放源代码的Web应用框架,由Python写成。

  

开始搭建

环境:python2.7 + centos 7.3 + pycharm
先安装Django。

# pip install Django==1.10.2

确认Django安装成功。
# python -m django --version
1.10.2

创建项目

在要创建项目的目录。
# django-admin startproject hello
没有报错就是创建成功

# 使用Pycharm打开工程。

在执行django-admin startproject hello 后会生成一个目录、
# tree 
.
└── myblog
    ├── manage.py
    └── myblog
        ├── __init__.py
        ├── settings.py
        ├── urls.py
        └── wsgi.py

manage.py  与项目进行交互的命令行工具集的入口。
# python manage.py 

Type 'manage.py help <subcommand>' for help on a specific subcommand.

Available subcommands:

[auth]
    changepassword
    createsuperuser

[django]
    check
    compilemessages
    createcachetable
    dbshell
    diffsettings
    dumpdata
    flush
    inspectdb
    loaddata
    makemessages
    makemigrations
    migrate
    sendtestemail
    shell
    showmigrations
    sqlflush
    sqlmigrate
    sqlsequencereset
    squashmigrations
    startapp
    startproject
    test
    testserver

[sessions]
    clearsessions

[staticfiles]
    collectstatic
    findstatic
    runserver


其中 runserver
[root@lol myblog]# python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

May 22, 2017 - 11:33:33
Django version 1.10.2, using settings 'myblog.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.


访问127.0.0.1:8000

[root@lol myblog]# python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

May 22, 2017 - 11:33:33
Django version 1.10.2, using settings 'myblog.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.



moblog目录。
项目的一个容器,包含项目的一些基本的配置。
[root@lol myblog]# ls
__init__.py   settings.py   urls.py   wsgi.py

wsgi.py   (Web Server Gateway Interface) 服务器网关接口。Python应用于Web之间的接口。一般不会用到这个,所以不要动这个。

urls.py 
URL配置文件
Django项目中的所有地址都需要自己去配置其URL。

setting.py
项目的总配置文件。有数据库,时间等等。。

__init__.py 
声明模块的文件,一般内容为空。

创建应用

进入与manager.py 同级目录。命令行输入: python manager.py startapp blog
并且添加应用。

[root@lol myblog]# python manage.py startapp blog
[root@lol myblog]# ls
blog  db.sqlite3  manage.py  myblog

多出来了一个blog目录。然后添加应用。在setting.py中。

应用目录。
[root@lol myblog]# tree blog/
blog/
├── admin.py
├── apps.py
├── __init__.py
├── migrations
│   └── __init__.py
├── models.py
├── tests.py
└── views.py

1 directory, 7 files

migrations   数据移植模块
admin          该应用的后台管理系统配置
apps            当前应用的配置
models        数据模型模块   ORM框架
tests            自动化测试
views           执行响应的代码  (项目中大部分的编写再此)

创建第一个页面
一。配置url
在myblog的urls.py 中

二。在blog的views.py中

访问试试

python manager.py runserver

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值