django 01--创建APP

初体验

安装

pip install django

建立工程

django-admin startproject mysite

运行工程

python manage.py runserver
python manage.py runserver 8000
python manage.py runserver 0.0.0.0:8000

第一个页面

在mysite/mysite下创建views.py

# -*- coding: utf-8 -*-
from django.http import HttpResponse

def index(request):
    return HttpResponse("<h2>你好,Django!</h2>")

将urls.py修改为

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

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

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

访问

访问127.0.0.1:8000

增加APP

python manage.py startapp west

在mysite/setting.py中添加:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'west',
]

将mysite/urls.py改成:

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

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

    url(r'^$', views.index, name='index'),
    url(r'^west/', include('west.urls')),
]

增加文件west/urls.py

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

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

增加文件west/views.py

# -*- coding: utf-8 -*-
from django.http import HttpResponse


def index(request):
    return HttpResponse("<h2>西餐!</h2>")

访问http://127.0.0.1:8000/west,查看效果。

posted @ 2016-10-31 15:12  你我之间  阅读(250)  评论(0编辑  收藏  举报