*XML出力(設定) [#ofb5caa4]
soundkitchenさんの[[DjangoでXMLとか吐くメモ:http://humming.via-kitchen.com/2007/07/04/output-xml-file/]]をやっただけなので、soundkitchenさんのサイトを参考にしたほうがよいかと思います。

**プロジェクトの作成 [#i49abe6e]
 # django-admin.py startproject xmlprj
 # cd xmlprj
 # python manage.py runserver

**PostgreSQLのDB作成 [#ueb45842]

 # createdb -E UTF-8 ***

**アプリケーション作成 [#ld199f42]
 # python manage.py startapp display

**Setting.pyの編集 [#z9f81524]
INSTALLED_APPSに「django.contrib.admin」を追加。&br;
INSTALLED_APPSに「xmlprj.display」を追加。&br;
また、以下のように編集する。

 # vim settings.py

 DATABASE_ENGINE = 'postgresql_psycopg2'   # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
 DATABASE_NAME = '***'                     # Or path to database file if using sqlite3.
 DATABASE_USER = '***'                     # Not used with sqlite3.
 DATABASE_PASSWORD = '***'                 # Not used with sqlite3.
 DATABASE_HOST = 'localhost'               # Set to empty string for localhost. Not used with sqlite3.
 DATABASE_PORT = ''                        # Set to empty string for default. Not used with sqlite3.

 TEMPLATE_DIRS = (
    "/home/user/templates",
 )


**DB作成 [#i032642a]
 # python manage.py syncdb

**モデル作成 [#ze3798f3]
 # vim display/models.py


**CRATE TABLE SQL文を表示 [#f3779eff]
 # python manage.py sql display

**INSERT 文の作成 [#iaf64c65]
 # mkdir display/sql

 # vim display/sql/member.sql

 INSERT INTO display_member(member_name1,member_name2,prefectural_id,member_address1)VALUES(・・・


**テーブル作成 [#vfc6731d]
 # python manage.py syncdb


**URLconfの設定 [#w13f6107]
 # cp urls.py  display/urls.py

 # vim urls.py

 from django.conf.urls.defaults import *
 
 urlpatterns = patterns('xmlprj.display.views',
     (r'^display/', include('xmlprj.display.urls')),
 )


 # display/urls.py

 from django.conf.urls.defaults import *
 
 urlpatterns = patterns('xmlprj.display.views',
      (r'^admin/', include('django.contrib.admin.urls')),
      (r'^$', 'index'),
 )


**ビュー作成 [#ba6e69fd]

 # vim display/views.py


 from django.template import Context, loader
 from django.http import HttpResponse
 
 from django.shortcuts import render_to_response
 from xmlprj.display.models import Member
 
 //文字列を出力する
 def index(request):
     return HttpResponse("Hello, world.")
 
 //HTMLファイルを出力する
 def detail(request):
     t = loader.get_template('display/detail.html')
     c = Context()
     return HttpResponse(t.render(c))
 
 //XMLファイルを出力する
 def member(request):
     t = loader.get_template('display/member.xml')
     c = Context()
     return HttpResponse(t.render(c), mimetype='text/xml; charset=utf-8')
 
 //XMLファイルを出力する(データをXMLファイルに渡す)
 def member2(request):
     p = {}
     p['status'] = 1
     p['msg'] = 'Success'
     t = loader.get_template('display/member2.xml')
     c = Context({'data':p})
     return HttpResponse(t.render(c), mimetype='text/xml; charset=utf-8')
 
 //ショートカットを使う
 def member3(request):
     p = {}
     p['status'] = 1
     p['msg'] = 'Success'
     return render_to_response('display/member3.xml', {'data':p}, mimetype="application/xhtml+xml")
 
 //DBから値を取得し表示する
 def member4(request):
     try:
         list = Member.objects.filter(member_delete_flag=False).values('member_id', 'member_name1').order_by('-member_insert', 'member_id')
     except Member.DoseNotExist:
         p = {}
         p['satus'] = 9
         p['msg'] = 'Error'
         return render_to_response('display/member4.xml', {'data':p}, mimetype="application/xhtml+xml")
         #raise Http404
     p = {}
     p['status'] = 1
     p['msg'] = 'Success'
     p['member'] = list
     return render_to_response('display/member4.xml', {'data':p}, mimetype="application/xhtml+xml")
 
 //DBにUPDATEし表示する
 def change(request, member_id, status_id, query):
     id = member_id
     name1 = status_id
 
     try:
         m = Member.objects.get(member_id=id)
         m.member_name19 = name1
         m.save()
     # DoseNotExist = get(1つも値がないとき?)
     # AssertionError = get(値がいっぱいありすぎちゃうとき?)
     except Member.DoesNotExist:
         p = {}
         p['status'] = 9
         p['msg'] = 'Error'
         return render_to_response('display/change.xml', {'data':p}, mimetype="application/xhtml+xml")
         #raise Http404
     p = {}
     p['status'] = 1
     p['msg'] = 'Success'
     return render_to_response('display/change.xml', {'data':p}, mimetype="application/xhtml+xml")


**Detailテンプレート作成 [#v93331af]
 # vim /templates/display/detail.html

 内容略。

**Memberテンプレート作成 [#yffd9c62]
***member.xml [#x97ddffc]
 # vim display/templates/display/member.xml

 <?xml version="1.0" encoding="utf-8" ?>
 <info>
     <member>
         <array>
             <member_id>1</member_id>
             <member_name1>name1</member_name1>
         </array>
     </member>
 </info>

***member4.xml [#e78b1d32]
 # vim display/templates/display/member4.xml

 <?xml version="1.0" encoding="utf-8" ?>
 <info>
     <status>{{ data.status }}</status>
     <msg>{{ data.msg }}</msg>
     {% if data.member %}
     {% for value in data.member %}
     <member>
         <array>
             <member_id>{{ value.member_id }}</member_id>
             <member_name1>{{ value.member_name1 }}</member_name1>
         </array>
     </member>
     {% endfor %}
     {% endif %}
 </info>

**フォルダ構成 [#bbd984d4]
 path/
  |
  |-- __init__.py
  |-- __init__.pyc
  |-- manage.py
  |-- setting.py
  |-- setting.pyc
  |-- urls.py
  |-- urls.pyc
  |
  |-- display/
  |   |
  |   |-- __init__.py
  |   |-- __init__.pyc
  |   |-- models.py
  |   |-- models.pyc
  |   |-- urls.py
  |   |-- urls.pyc
  |   |-- views.py
  |   |-- views.pyc
  |   |
  |   |-- sql/
  |   |   |-- ***.sql
  |   |   |-- ***.sql
  |   |
  |   |-- templates/
  |       |-- 404.html
  |       |-- 500.html
  |       |
  |       |-- display/
  |           |-- change.xml
  |           |-- detail.xml
  |           |-- member.xml
  |           |-- member2.xml
  |           |-- member3.xml
  |           |-- member4.xml
  |

**確認 [#z52d103e]
ブラウザから「[[http://localhost:8000/display/]]」が表示されるか確認する。
 # python manage.py runserver

&br;
*結果 [#i5bba6aa]

#ref("xml1.jpg",left,wrap)
&br;


&br;
*URLメモ [#k9ee4572]
http://localhost:8000/display/
&br;
http://localhost:8000/display/member/
&br;
http://localhost:8000/display/member2/
&br;
http://localhost:8000/display/member3/
&br;
http://localhost:8000/display/member4/
&br;
http://localhost:8000/display/detail/
&br;
http://localhost:8000/display/change/1/1/1/
&br;

&br;
*Comment [#tb7222ed]
#comment_nospam
&br;
#counter


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS