您的位置:首页 > 编程语言 > Python开发

Python Django学习笔记

2016-07-09 11:30 531 查看
创建django vritualenv

virtualenv  firstDjangoApp
source firstDjangoApp
pip install django

(firstDjangoApp) fangfenghuadeMacBook-Pro:django fangfenghua$ django-admin  help check
usage: django-admin check [-h] [--version] [-v {0,1,2,3}]
[--settings SETTINGS] [--pythonpath PYTHONPATH]
[--traceback] [--no-color] [--tag TAGS]
[--list-tags] [--deploy]
[app_label [app_label ...]]

Checks the entire Django project for potential problems.

positional arguments:
app_label

optional arguments:
-h, --help            show this help message and exit
--version             show program's version number and exit
-v {0,1,2,3}, --verbosity {0,1,2,3}
Verbosity level; 0=minimal output, 1=normal output,
2=verbose output, 3=very verbose output
--settings SETTINGS   The Python path to a settings module, e.g.
"myproject.settings.main". If this isn't provided, the
DJANGO_SETTINGS_MODULE environment variable will be
used.
--pythonpath PYTHONPATH
A directory to add to the Python path, e.g.
"/home/djangoprojects/myproject".
--traceback           Raise on CommandError exceptions
--no-color            Don't colorize the command output.
--tag TAGS, -t TAGS   Run only checks labeled with given tag.
--list-tags           List available tags.
--deploy              Check deployment settings.


至此,django已经安装完毕。开始创建,第一个django project吧。

django-admin startproject firstProject

(firstDjangoApp) fangfenghuadeMacBook-Pro:django fangfenghua$ tree firstProject
firstProject
├── firstProject
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py


下面,开始了解下项目目录中每个文件吧。

manage.py:包含了管理django服务相应的控制命令,其中的内不需要更改。

firstProject/wsgi.py:启动一个兼容wsgi的服务的接口

firstProject/settings.py:django项目的配置脚本,暂时无序修改

firstProject/urls.py:https://docs.djangoproject.com/en/1.9/topics/http/urls/

启动server:

python manage.py  runserver $ip:$port




新建一个app:

firstrApp/
├── __init__.py
├── admin.py
├── apps.py
├── migrations
│   └── __init__.py
├── models.py
├── tests.py
└── views.py


在firstProject/urls.py中增加:

url(r'^firstrApp/', views.firstApp)


修改firstProject/firstApp/views.py

(firstDjangoApp) fangfenghuadeMacBook-Pro:firstProject fangfenghua$ cat firstProject/firstApp/views.py
from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def firstApp(request):
return  HttpResponse("hello firstrapp")


重新启动server。输入http://127.0.0.1:8000/firstApp/。页面即可返回“hello firstrapp“
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: