Python之django(一)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明。谢谢!
设置阿里云的python镜像地址:
# vim ~/.pip/pip.conf
# [global]
# trusted-host=mirrors.aliyun.com
# index-url=http://mirrors.aliyun.com/pypi/simple/
安装django:(会自动uninstall 之前的django版本)
# sudo pip install django==1.8
阿里云,速度杠杠滴,谢谢阿里云。
检查django的版本:
# import django
# print (django.VERSION)
启动django:
# django-admin.py startproject mysite 在当前目录创建django项目目录
# ├── manage.py
# └── mysite
# ├── init.py
# ├── settings.py
# ├── urls.py
# └── wsgi.py
# python3.4 manag.py runserver 0.0.0.0:8000
测试django默认网页:
# curl http://127.0.0.1:8000
django第一个网页:
# vim mysite/urls.py
# from django.conf.urls import patterns, include, url
# from django.contrib import admin
# admin.autodiscover()
# urlpatterns = patterns ('',
# url(r'^admin/', admin.site.urls),
# url(r'^$','mysite.views.first_page')
# )
# 将根目录请求交给mysite.views.first_page这个对象处理。
# vim mysite/views.py
# # -- coding: utf-8 --
# from django.http import HttpResponse
# def first_page(request):
# return HttpResponse("<p> Hello ,world!</p>")
# first_page这个对象对请求return一个httpresponse对象。(hello,world!)
增加APP:
mysite目录下增加westAPP程序
# python3.4 manage.py startapp west
# neo@neo-virtual-machine:~/python3/mysite$ tree
# ├── manage.py
# ├── mysite
# │ ├── init.py
# │ ├── pycache
# │ ├── settings.py
# │ ├── urls.py
# │ ├── views.py
# │ └── wsgi.py
# └── west
# ├── admin.py
# ├── init.py
# ├── migrations
# │ └── init.py
# ├── models.py
# ├── tests.py
# └── views.py
在mysite/settings.py添加安装west项目
# vim mysite/settings.py
# INSTALLED_APPS = [
# 'django.contrib.admin', //管理
# 'django.contrib.auth', //用户认证
# 'django.contrib.contenttypes', //内容类型
# 'django.contrib.sessions', //会话管理
# 'django.contrib.messages', //消息
# 'django.contrib.staticfiles', //静态文件
# 'west', //自定义app
# ]
增加APP页面:
# 在mysite/urls.py配置文件的urlpatterns元组对象中添加
# url(r'^west/',include('west.urls')),
# 接着在west目录下创建urls.py文件
# vim west/urls.py
# # -- coding: utf-8 --
# from django.conf.urls import patterns, include, url
# urlpatterns = patterns ('',
# url(r'^$','west.views.first_page'),
# )
# 在west目录下修改views.py的页面文件
# # -- coding: utf-8 --
# from django.shortcuts import render
# from django.http import HttpResponse
# # Create your views here.
# def first_page(request):
# return HttpResponse("<p>世界,你好.</p>")
访问测试:
# neo@neo-virtual-machine:~/python3/mysite$ curl http://127.0.0.1:8000/west/
# <p>世界,你好.</p>
django报错:
ImportError: cannot import name 'patterns'
新版本的django没有此方法,在django1.8版本存在。
疑问:
1、在新版本1.10(目前python3.4的djangp阿里云新版本应该是1.10),如何实现简单页面?
2、ubuntu系统时间显示正确,在python启动django是,日志时间显示不正确?
[20/Feb/2017 01:28:06]"GET /west/ HTTP/1.1" 200 23
[20/Feb/2017 01:28:08]"GET /west/ HTTP/1.1" 200 23
3、后台运行:
python3.4 manage.py runserver 0.0.0.0:8000 &