首页 > 代码库 > django 学习笔记(一)搭建基础环境

django 学习笔记(一)搭建基础环境

1、安装django 下载地址 https://github.com/django/django

  解压后进入文件夹运行指令 >> python setup.py install

2、创建工程

     >> django-admin.py startproject web_1

3、修改默认配置

  cd web_1

  修改 settings.py

LANGUAGE_CODE = ‘zh-Hans‘

TIME_ZONE = ‘‘Asia/Shanghai‘

4、添加app

  修改 settings.py

INSTALLED_APPS = [
    ‘django.contrib.admin‘,
    ‘django.contrib.auth‘,
    ‘django.contrib.contenttypes‘,
    ‘django.contrib.sessions‘,
    ‘django.contrib.messages‘,
    ‘django.contrib.staticfiles‘,
    ‘blog‘,
] 

添加blog应用

5、添加blog应用对应的url

  修改 urls.py

from django.conf.urls import url
from django.contrib import admin
# 引入模块
from blog import views as blog_views

urlpatterns = [
    url(r^admin/, admin.site.urls),
    # 添加url映射
    url(r^blog/index/$,blog_views.index),
]    

  创建blog应用

  返回工程根目录

  执行命令 >> django-admin.py startapp blog

6、添加view.index 解析

  blog/views.py:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render

from django.http import HttpResponse

# Create your views here.

def index(req):
    return HttpResponse("<h1>hello world!</h1>")

7、运行服务

  >> python manage.py runserver

  如果出现如下错误:

Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run ‘python manage.py migrate‘ to apply them.

  先运行指令: >> python manage.py migrate

  重新启动服务:>> python manage.py runserver

  出现如下内容,恭喜你,服务启动成功了

Performing system checks...

System check identified no issues (0 silenced).
December 07, 2016 - 08:18:12
Django version 1.11, using settings ‘web_1.settings‘
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

8、http://127.0.0.1:8000/blog/index

  

 

 

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff } span.s1 { }</style> <style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff } span.s1 { }</style> <style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff } span.s1 { }</style> <style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff } span.s1 { }</style> <style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff } span.s1 { }</style> <style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff } span.s1 { }</style>

django 学习笔记(一)搭建基础环境