python添加应用,,python添加应用


python添加应用


1、添加一个项目

[root@133django]#django-admin.pystartprojectweb[root@133django]#ll总用量8drwxr-xr-x3rootroot40961月123:11web[root@133django]#cdweb[root@133web]#ll总用量8-rwxr-xr-x1rootroot2461月123:11manage.pydrwxr-xr-x2rootroot40961月123:11web[root@133web]#cdweb[root@133web]#ll总用量12-rw-r--r--1rootroot01月123:11__init__.py-rw-r--r--1rootroot19631月123:11settings.py-rw-r--r--1rootroot2941月123:11urls.py-rw-r--r--1rootroot3811月123:11wsgi.py[root@133web]#cat/etc/sysconfig/clockZONE="Asia/Shanghai"[root@133web]#vimsettings.pyLANGUAGE_CODE=‘zh-cn‘#LANGUAGE_CODE=‘en-us‘TIME_ZONE=‘Asia/Shanghai‘#TIME_ZONE=‘UTC‘[root@133web]#cd/opt/python/django/web[root@133web]#ll-rwxr-xr-x1rootroot2461月123:11manage.pydrwxr-xr-x2rootroot40961月123:14web[root@133web]#pythonmanage.pyrunserver112.65.140.133:8080#启动服务端口浏览器访问http://112.65.140.133:8080/


2、添加应用

[root@133django]#cd/opt/python/django/web/[root@133web]#ll-rwxr-xr-x1rootroot2461月123:11manage.pydrwxr-xr-x2rootroot40961月123:16web[root@133web]#pythonmanage.pystartappblog#创建应用[root@133web]#lsblogmanage.pyweb[root@133web]#cdblog/[root@133blog]#lsadmin.py__init__.pymodels.pytests.pyviews.py


3、添加应用到项目中

[root@133web]#vim/opt/python/django/web/web/settings.py#ApplicationdefinitionINSTALLED_APPS=(‘django.contrib.admin‘,‘django.contrib.auth‘,‘django.contrib.contenttypes‘,‘django.contrib.sessions‘,‘django.contrib.messages‘,‘django.contrib.staticfiles‘,‘blog‘,#添加blog)[root@133web]#vim/opt/python/django/web/web/urls.pyfromdjango.conf.urlsimportpatterns,include,urlfromdjango.contribimportadminadmin.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@133web]#vim/opt/python/django/web/blog/views.pyfromdjango.shortcutsimportrenderfromdjango.httpimportHttpResponse#Createyourviewshere.defindex(request):returnHttpResponse(‘HelloWorld‘)

http://112.65.140.133:8080/blog/index/就可以访问了,并且返回Hello World


4、访问网页文件,将网页文件放到模板中,位置固定templates,在setting中更改

首先在应用下创建模板文件templates[root@133web]#cdblog/[root@133blog]#mkdirtemplates[root@133blog]#cdtemplates/[root@133templates]#vimindex.html<h1>hellodjango</h1>[root@133templates]#cd..[root@133blog]#lsadmin.py__init__.pymodels.pytemplatesviews.pyadmin.pyc__init__.pycmodels.pyctests.pyviews.pyc[root@133blog]#vimviews.pyfromdjango.shortcutsimportrenderfromdjango.httpimportHttpResponsefromdjango.templateimportloader,Context#Createyourviewshere.defindex(request):t=loader.get_template(‘index.html‘)c=Context({})returnHttpResponse(t.render(c))访问测试:打印:hellodjango



本文出自 “梅花香自苦寒来!” 博客,请务必保留此出处http://daixuan.blog.51cto.com/5426657/1888195

python添加应用

评论关闭