Python Django的使用:Writing your first Django app--实践

Writing your first Django app, part 1

安装django

C:\Windows\system32>python -m django --version

1.10.2

Creating a project

From the command line, cd into a directory where you’d like to store your code, then run the following command:

D:\>cd D:\code-github

D:\code-github>django-admin startproject mysite

创建之后的目录结构为:

D:\code-github\mysite>tree /f

D:.

│  db.sqlite3

│  manage.py

├─mysite

│  │  settings.py

│  │  urls.py

│  │  wsgi.py

│  │  __init__.py

│  │

│  └─__pycache__

│          settings.cpython-35.pyc

│          urls.cpython-35.pyc

│          wsgi.cpython-35.pyc

│          __init__.cpython-35.pyc

The outer mysite/ root directory is just a container for your project

manage.py: A command-line utility that lets you interact with this Django project in various ways

The inner mysite/ directory is the actual Python package for your project. Its name is the Python package name youll need to use to import anything inside it (e.g. mysite.urls)

mysite/__init__.py: An empty file that tells Python that this directory should be considered a Python package.

mysite/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.

mysite/urls.py: The URL declarations for this Django project; a table of contentsof your Django-powered site;

mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project

The development server

Lets verify your Django project works. Change into the outer mysite directory, if you havent already, and run the following commands:$ python manage.py runserver

Youve started the Django development server, a lightweight Web server written purely in Python. Weve included this with Django so you can develop things rapidly, without having to deal with configuring a production serversuch as Apacheuntil youre ready for production

已经启动django server,集成了一个轻量级、纯pythonweb server容器;可以快速用于开发

执行python manage.py runserver之后:

October 27, 2016 - 13:55:03 Django version 1.10.2, using settings 'mysite.settings'Starting development server athttp://127.0.0.1:8000/  Quit the server with CTRL-BREAK.

If you want to change the servers port, pass it as a command-line argument. For instance, this command starts the server on port 8080:$python manage.py runserver 8080

Creating the Polls app

Django comes with a utility that automatically generates the basic directory structure of an app, so you can focus on writing code rather than creating directories.

Your apps can live anywhere on your Python path. In this tutorial, well create our poll app right next to your manage.py file so that it can be imported as its own top-level module, rather than a submodule of mysite

To create your app, make sure youre in the same directory as manage.py and type this command:

$ python manage.py startapp polls

D:\code-github\mysite>tree /f polls

D:\CODE-GITHUB\MYSITE\POLLS

│  admin.py

│  apps.py

│  models.py

│  tests.py

│  views.py

│  __init__.py

└─migrations

        __init__.py

Write your first view

Lets write the first view. Open the filepolls/views.py and put the following Python code in it

from django.shortcutsimport render;
# Create your views here.
from django.httpimport HttpResponse;
def index(request):
    return HttpResponse("Hello, world. You're at the polls index.");

 

To call the view, we need to map it to a URL - and for this we need a URLconf.To create a URLconf in the polls directory, create a file called urls.py. In the polls/urls.py file include the following code:

from django.conf.urlsimport url
from . importviews
urlpatterns = [
    url(r'^$', views.index,

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值