您的位置:首页 > 编程语言 > Python开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: