1、添加一个项目
[[email protected] django]# django-admin.py startproject web [[email protected] django]# ll 总用量 8 drwxr-xr-x 3 root root 4096 1月 1 23:11 web [[email protected] django]# cd web [[email protected] 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 [[email protected] web]# cd web [[email protected] 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 [[email protected] web]# cat /etc/sysconfig/clock ZONE="Asia/Shanghai" [[email protected] web]# vim settings.py LANGUAGE_CODE = ‘zh-cn‘ #LANGUAGE_CODE = ‘en-us‘ TIME_ZONE = ‘Asia/Shanghai‘ #TIME_ZONE = ‘UTC‘ [[email protected] web]# cd /opt/python/django/web [[email protected] 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 [[email protected] web]# python manage.py runserver 112.65.140.133:8080 #启动服务端口 浏览器访问http://112.65.140.133:8080/
2、添加应用
[[email protected] django]# cd /opt/python/django/web/ [[email protected] 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 [[email protected] web]# python manage.py startapp blog #创建应用 [[email protected] web]# ls blog manage.py web [[email protected] web]# cd blog/ [[email protected] blog]# ls admin.py __init__.py models.py tests.py views.py
3、添加应用到项目中
[[email protected] 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 ) [[email protected] 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返回 ) [[email protected] 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://112.65.140.133:8080/blog/index/ 就可以访问了,并且返回Hello World
4、访问网页文件,将网页文件放到模板中,位置固定templates,在setting中更改
首先在应用下创建模板文件templates [[email protected] web]# cd blog/ [[email protected] blog]# mkdir templates [[email protected] blog]# cd templates/ [[email protected] templates]# vim index.html <h1>hello django</h1> [[email protected] templates]# cd .. [[email protected] blog]# ls admin.py __init__.py models.py templates views.py admin.pyc __init__.pyc models.pyc tests.py views.pyc [[email protected] 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
时间: 2024-10-13 08:01:43