Django doc summary (1)

Demo

Install Verifing

C:\Users\sam_rui>python --version
Python 3.5.0
C:\Users\sam_rui>python
Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:16:59) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print(django.get_version())
1.9
>>>

Create a demo project###

cd into a directory, then create a demo project.

D:\SamProject\django>django-admin startproject samsite

Start a dev server

D:\SamProject\django\samsite>python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
December 04, 2015 - 17:25:36
Django version 1.9, using settings 'samsite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

Start a app

D:\SamProject\django\samsite>python manage.py startapp samapp

Now, the directory tree is like below.

D:\SamProject\django\samsite>tree /F
Folder PATH listing
Volume serial number is 0000003E F203:C3A2
D:.
│  db.sqlite3
│  manage.py
│
├─samapp
│  │  admin.py
│  │  apps.py
│  │  models.py
│  │  tests.py
│  │  views.py
│  │  __init__.py
│  │
│  └─migrations
│          __init__.py
│
└─samsite
    │  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

Start a new view

add code in file 'samapp/views.py'

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
	return HttpResponse("Hello, this is sam site.")

add a internal url conf file in samapp directory

urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

modify the site url conf

samsite/urls.py

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
	url(r'^samapp/', include('samapp.urls')),
]

restart the server and view the url 'http://127.0.0.1:8000/samapp/'

posted @ 2015-12-10 22:30  sam_rui  阅读(176)  评论(0编辑  收藏  举报