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 you’ll 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 contents”of your Django-powered site;
mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project
The development server
Let’s verify your Django project works. Change into the outer mysite directory, if you haven’t already, and run the following commands:$ python manage.py runserver
You’ve started the Django development server, a lightweight Web server written purely in Python. We’ve included this with Django so you can develop things rapidly, without having to deal with configuring a production server– such as Apache– until you’re ready for production
已经启动django server,集成了一个轻量级、纯python的web 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 server’s 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, we’ll 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 you’re 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
Let’s write the first view. Open the filepolls/views.py and put the following Python code in it
from django.shortcutsimport render; |
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 |