python添加应用
1、添加一个项目
代码语言:javascript复制[root@133 django]# django-admin.py startproject web
[root@133 django]# ll
总用量 8
drwxr-xr-x 3 root root 4096 1月 1 23:11 web
[root@133 django]# cd web
[root@133 web]# ll
总用量 8
-rwxr-xr-x 1 root root 246 1月 1 23:11 manage.py
drwxr-xr-x 2 root root 4096 1月 1 23:11 web
[root@133 web]# cd web
[root@133 web]# ll
总用量 12
-rw-r--r-- 1 root root 0 1月 1 23:11 __init__.py
-rw-r--r-- 1 root root 1963 1月 1 23:11 settings.py
-rw-r--r-- 1 root root 294 1月 1 23:11 urls.py
-rw-r--r-- 1 root root 381 1月 1 23:11 wsgi.py
[root@133 web]# cat /etc/sysconfig/clock
ZONE="Asia/Shanghai"
[root@133 web]# vim settings.py
LANGUAGE_CODE = 'zh-cn'
#LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Shanghai'
#TIME_ZONE = 'UTC'
[root@133 web]# cd /opt/python/django/web
[root@133 web]# ll
-rwxr-xr-x 1 root root 246 1月 1 23:11 manage.py
drwxr-xr-x 2 root root 4096 1月 1 23:14 web
[root@133 web]# python manage.py runserver 112.65.140.133:8080 #启动服务端口
浏览器访问http://11.65.140.13:8080/
2、添加应用
代码语言:javascript复制[root@133 django]# cd /opt/python/django/web/
[root@133 web]# ll
-rwxr-xr-x 1 root root 246 1月 1 23:11 manage.py
drwxr-xr-x 2 root root 4096 1月 1 23:16 web
[root@133 web]# python manage.py startapp blog #创建应用
[root@133 web]# ls
blog manage.py web
[root@133 web]# cd blog/
[root@133 blog]# ls
admin.py __init__.py models.py tests.py views.py
3、添加应用到项目中
代码语言:javascript复制[root@133 web]# vim /opt/python/django/web/web/settings.py
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog', #添加blog
)
[root@133 web]# vim /opt/python/django/web/web/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'web.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/index/$', 'blog.views.index'), #添加匹配正则表达式,当在浏览器中访问blog开头,index结尾,转到blog应用,views视图中index返回
)
[root@133 web]# vim /opt/python/django/web/blog/views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse('Hello World')
http://11.65.140.13:8080/blog/index/ 就可以访问了,并且返回Hello World
4、访问网页文件,将网页文件放到模板中,位置固定templates,在setting中更改
代码语言:javascript复制首先在应用下创建模板文件templates
[root@133 web]# cd blog/
[root@133 blog]# mkdir templates
[root@133 blog]# cd templates/
[root@133 templates]# vim index.html
<h1>hello django</h1>
[root@133 templates]# cd ..
[root@133 blog]# ls
admin.py __init__.py models.py templates views.py
admin.pyc __init__.pyc models.pyc tests.py views.pyc
[root@133 blog]# vim views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader, Context
# Create your views here.
def index(request):
t = loader.get_template('index.html')
c = Context({})
return HttpResponse(t.render(c))
访问测试:
打印:hello django