use python and django make an web service
2013-10-12 17:23
369 查看
first i want to say that it is very simple tocreate an web site using the Django framework.
ubuntu 11.10 python 2.7.2+
i am assume you run the follow commands as root
install pip:
# apt-get install python-pip
install Django using pip:
# pip install Django
create a djangoproject:
# django-admin.py startproject myweb
now there is a myweb directory in your current location. that's seewhat are there in it:
# cd myweb; ls -R
.:
manage.py myweb
./myweb:
__init__.py settings.py wsgi.py urls.py
then create my firstapp:
#python manage.py startapp myapp
#ls myapp
_init__.py models.py views.py tests.py
edit myapp/views.py
#vimmyapp/views.py
1 # Create your viewshere.
2 import commands
3 from django.http importHttpResponse
4 from django.template importloader, Context
5 from django.shortcuts importrender_to_response
6
7
8 def index(request):
9 ls = commands.getoutput('ls')
10 file_list = ls.split('\n')
11 return render_to_response('path/index.html',{'file_list':file_list})
make the template htmlfile
# mkdir /my/path/to/python/template/; cd/my/path/to/python/template/
# vim index.html
1<html>
2<title>files</title>
3<body>
4<h2>you can upload thesefiles</h2>
5 {% for file in file_list%}
6 <li>{{file}}</li><br/>
7 {%endfor%}
8</body>
9</html>
edit myweb/setting.py
#vim myweb/setting.py
12 DATABASES = {
13 'default': {
14 'ENGINE': 'django.db.backends.sqlite3', # Add'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
15 'NAME':'/myhome/django/myweb/sql.db', # Or path to database file if using sqlite3.
16 'USER':'', # Not used with sqlite3.
17 'PASSWORD':'', # Not used with sqlite3.
18 'HOST':'', # Set to empty string for localhost. Not used with sqlite3.
19 'PORT':'', # Set to empty string for default. Not used with sqlite3.
20 }
21 }
.............................
108 TEMPLATE_DIRS = (
109 '/my/path/to/python/template',
110 # Put strings here, like "/home/html/django_templates" or"C:/www/django/templates".
111 # Always use forward slashes, even on Windows.
112 # Don't forget to use absolute paths, not relative paths.
113 )
114
115 INSTALLED_APPS = (
116 'django.contrib.auth',
117 'django.contrib.contenttypes',
118 'django.contrib.sessions',
119 'django.contrib.sites',
120 'django.contrib.messages',
121 'django.contrib.staticfiles',
122 # Uncomment the next line to enable the admin:
123 # 'django.contrib.admin',
124 # Uncomment the next line to enable admin documentation:
125 # 'django.contrib.admindocs',
127 'myapp',
126 )
edit myweb/urls.py
# vim myweb/urls.py
1 from django.conf.urls importpatterns, include, url
2
3 urlpatterns =patterns('',
4 url(r'^$', 'getpath.views.index'),
5 )
now it is the time to start my web site
# python manage.py runserver
open your browser and type "127.0.0.1:8000" in the url, yes, thereis the web site we just create
ubuntu 11.10 python 2.7.2+
i am assume you run the follow commands as root
install pip:
# apt-get install python-pip
install Django using pip:
# pip install Django
create a djangoproject:
# django-admin.py startproject myweb
now there is a myweb directory in your current location. that's seewhat are there in it:
# cd myweb; ls -R
.:
manage.py myweb
./myweb:
__init__.py settings.py wsgi.py urls.py
then create my firstapp:
#python manage.py startapp myapp
#ls myapp
_init__.py models.py views.py tests.py
edit myapp/views.py
#vimmyapp/views.py
1 # Create your viewshere.
2 import commands
3 from django.http importHttpResponse
4 from django.template importloader, Context
5 from django.shortcuts importrender_to_response
6
7
8 def index(request):
9 ls = commands.getoutput('ls')
10 file_list = ls.split('\n')
11 return render_to_response('path/index.html',{'file_list':file_list})
make the template htmlfile
# mkdir /my/path/to/python/template/; cd/my/path/to/python/template/
# vim index.html
1<html>
2<title>files</title>
3<body>
4<h2>you can upload thesefiles</h2>
5 {% for file in file_list%}
6 <li>{{file}}</li><br/>
7 {%endfor%}
8</body>
9</html>
edit myweb/setting.py
#vim myweb/setting.py
12 DATABASES = {
13 'default': {
14 'ENGINE': 'django.db.backends.sqlite3', # Add'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
15 'NAME':'/myhome/django/myweb/sql.db', # Or path to database file if using sqlite3.
16 'USER':'', # Not used with sqlite3.
17 'PASSWORD':'', # Not used with sqlite3.
18 'HOST':'', # Set to empty string for localhost. Not used with sqlite3.
19 'PORT':'', # Set to empty string for default. Not used with sqlite3.
20 }
21 }
.............................
108 TEMPLATE_DIRS = (
109 '/my/path/to/python/template',
110 # Put strings here, like "/home/html/django_templates" or"C:/www/django/templates".
111 # Always use forward slashes, even on Windows.
112 # Don't forget to use absolute paths, not relative paths.
113 )
114
115 INSTALLED_APPS = (
116 'django.contrib.auth',
117 'django.contrib.contenttypes',
118 'django.contrib.sessions',
119 'django.contrib.sites',
120 'django.contrib.messages',
121 'django.contrib.staticfiles',
122 # Uncomment the next line to enable the admin:
123 # 'django.contrib.admin',
124 # Uncomment the next line to enable admin documentation:
125 # 'django.contrib.admindocs',
127 'myapp',
126 )
edit myweb/urls.py
# vim myweb/urls.py
1 from django.conf.urls importpatterns, include, url
2
3 urlpatterns =patterns('',
4 url(r'^$', 'getpath.views.index'),
5 )
now it is the time to start my web site
# python manage.py runserver
open your browser and type "127.0.0.1:8000" in the url, yes, thereis the web site we just create
相关文章推荐
- [zz]Make Yahoo! Web Service REST calls with Python
- 【HTTP】Speed and Mobility: An Approach for HTTP 2.0 to Make Mobile Apps and the Web Faster
- Implement, deploy and call an EJB 3.0 webservice using Glassfish and JAX-WS
- Contention, poor performance, and deadlocks when you make Web service requests from ASP.NET applications
- GridSAM an open-source job submission and monitoring web service
- Creation of an Axis Web Service and a gSOAP client
- Creation of a gSOAP Web Service a gSOAP client and an Axis client
- Professional Python Frameworks: Web 2.0 Programming with Django and Turbogears
- [转]Django 是一个 Python 下的 web 开发框架[学习资料]
- Python Web框架Django框架安装配置
- python常用web框架性能测试(django,flask,bottle,tornado)
- lynda.com教程之Learing Python and Django零基础搭建Django项目
- Difference between servlet and web service
- python之web开发1:Eclipse+PyDev+Django+Mysql搭建Python web开发环境
- python TabError: Inconsistent use of tabs and spaces in indentation
- python web框架——扩展Django&tornado
- [React Intl] Use Webpack to Conditionally Include an Intl Polyfill for Older Browsers
- Rule 8: Make JavaScript and CSS External(Chapter 8 of High performance Web Sites)
- python REST Webservice