django 学习笔记搭建基础环境
Posted 夜未央丿
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于django 学习笔记搭建基础环境的主要内容,如果未能解决你的问题,请参考以下文章
Python学习笔记——1环境搭建篇(如何在Pycharm中配置Python和Django)