Python Django学习笔记

本文详细介绍了一个简单的Django项目从创建到运行的过程。包括环境搭建、项目及应用的创建、URL配置、视图编写等关键步骤。

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

创建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

测试项目自带的url

新建一个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“

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值