use python and django make an web service

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):
                  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%}
                  <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('',
                  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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值