[Python] WebApplication framework

http://www.djangoproject.com/

RubyOnRails의 영향으로 만들어짐. 짧은 시간내에 짧은 코드로 원하는 기능을 구현하고자함. 관리자용 인터페이스가 미리 구현되어(확장가능) 있다는 점이 특징.

관련포스트

이용가능한 개발환경

각종 자료들

튜토리얼번역

0.91 버전을 번역. 최신버전은 http://djangoproject.com/documentation/ 의 원본 문서를 참고

  1. [/Tutorial1] : Initialization, creating models, the database API
  2. [/Tutorial2] : Exploring the automatically-generated admin site
  3. [/Tutorial3] : Creating the public interface views
  4. [/Tutorial4] : Simple form processing and generic views

관련문서들

  1. [/AdminInterface]

  2. [/DbApi]

몇가지 팁들

원격지의 컴퓨터에서 runserver를 확인하고 싶으면 아래의 명령 사용

python manage.py runserver 0.0.0.0:8080

template에서 모델객체 사용시, 메쏘드 호출에 "()"를 쓰지 않아야 한다.

다른 스크립트에서 django로 만든 프로젝트로 접근하는 방법

   1 import sys, os
   2 sys.path.append('/home/yong27/test/django') # upper directory of the project
   3 os.environ['DJANGO_SETTINGS_MODULE'] = 'antifungal.settings' # project.settings

[Sqlite]를 쓰면서 ModPython으로 서비스를 시작하면 다음에러를 만난다.

OperationalError: SQL logic error or missing database

sqlite db의 permission을 수정해준다.

offline에서 문서를 읽고 싶으면 다음처럼...

wget http://www.djangoproject.com/documentation/ -r -np -k

기타

다른건 다 마음에 드는데, web-templating이 별로다. [Twisted]-[Nevow]에 쓰는 방식을 도입하면 어떨까. -- ["yong27"] DateTime(2006-03-18T08:23:45Z)

template 에서 "."을 많이 쓰지 않도록 한다. 가능한한 context로 넘겨주는 것이 [SQL]쿼리 숫자를 줄이는 것이다. -- ["yong27"] DateTime(2006-12-08T09:01:24Z)

토론

from django.conf.urls.defaults import *
import datetime
from django.shortcuts import render_to_response

def current_datetime(request):
    now = datetime.datetime.now()
    return render_to_response('current_datetime.html', {'current_date': now})

urlpatterns = patterns('',
    # Example:
    # (r'^test2/', include('test2.apps.foo.urls.foo')),

    # Uncomment this for admin:
    (r'^admin/', include('django.contrib.admin.urls')),
    (r'^test/', 'current_datetime'),
)

프로젝트 생성시 가장 상단에 자동으로 생성되는 urls.py를 다음과 같이 고치고 테스트 했는데 다음과 같은 에러가 나옵니다

ValueError at /test/
substring not found
Request Method:         GET
Request URL:    http://127.0.0.1:8000/test/
Exception Type:         ValueError
Exception Value:        substring not found
Exception Location:     /usr/lib/python2.4/site-packages/Django-0.95.1-py2.4.egg/django/core/urlresolvers.py in get_mod_func, line 23
Traceback (innermost last)
Switch to copy-and-paste view

    * /usr/lib/python2.4/site-packages/Django-0.95.1-py2.4.egg/django/core/handlers/base.py in get_response
        58. for middleware_method in self._request_middleware:
        59. response = middleware_method(request)
        60. if response:
        61. return response
        62.
        63. resolver = urlresolvers.RegexURLResolver(r'^/', settings.ROOT_URLCONF)
        64. try:
        65. callback, callback_args, callback_kwargs = resolver.resolve(path) ...
        66.
        67. # Apply view middleware
        68. for middleware_method in self._view_middleware:
        69. response = middleware_method(request, callback, callback_args, callback_kwargs)
        70. if response:
        71. return response
      ▶ Local vars
      Variable  Value
      exceptions        
      <module 'django.core.exceptions' from '/usr/lib/python2.4/site-packages/Django-0.95.1-py2.4.egg/django/core/exceptions.pyc'>
      mail_admins       
      <function mail_admins at 0xb6daa8ec>
      middleware_method         
      <bound method AuthenticationMiddleware.process_request of <django.contrib.auth.middleware.AuthenticationMiddleware object at 0xb6dabc6c>>
      path      
      '/test/'
      request   
      <WSGIRequest GET:<MultiValueDict: {}>, POST:<MultiValueDict: {}>, COOKIES:{'sessionid': '354fed1fb48f42ce4f5ce89f301ed501'}, META:{'COLORTERM': 'gnome-terminal', 'CONTENT_LENGTH': '', 'CONTENT_TYPE': 'text/plain', 'DBUS_SESSION_BUS_ADDRESS': 'unix:abstract=/tmp/dbus-Y1yu84SphQ,guid=d173e2459cd85c7e3601c600dd012f00', 'DESKTOP_SESSION': 'default', 'DESKTOP_STARTUP_ID': '', 'DISPLAY': ':0.0', 'DJANGO_SETTINGS_MODULE': 'test2.settings', 'GATEWAY_INTERFACE': 'CGI/1.1', 'GDMSESSION': 'default', 'GDM_XSERVER_LOCATION': 'local', 'GNOME_DESKTOP_SESSION_ID': 'Default', 'GNOME_KEYRING_SOCKET': '/tmp/keyring-GemmVy/socket', 'GTK_IM_MODULE': 'scim', 'GTK_RC_FILES': '/etc/gtk/gtkrc:/home/athxue/.gtkrc-1.2-gnome2', 'HISTCONTROL': 'ignoredups', 'HOME': '/home/athxue', 'HTTP_ACCEPT': 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5', 'HTTP_ACCEPT_CHARSET': 'EUC-KR,utf-8;q=0.7,*;q=0.7', 'HTTP_ACCEPT_ENCODING': 'gzip,deflate', 'HTTP_ACCEPT_LANGUAGE': 'ko-kr,ko;q=0.8,en-us;q=0.5,en;q=0.3', 'HTTP_CACHE_CONTROL': 'max-age=0', 'HTTP_CONNECTION': 'keep-alive', 'HTTP_COOKIE': 'sessionid=354fed1fb48f42ce4f5ce89f301ed501', 'HTTP_HOST': '127.0.0.1:8000', 'HTTP_KEEP_ALIVE': '300', 'HTTP_USER_AGENT': 'Mozilla/5.0 (X11; U; Linux i686; ko-KR; rv:1.8.1.1) Gecko/20060601 Firefox/2.0.0.1 (Ubuntu-edgy)', 'LANG': 'ko_KR.UTF-8', 'LESSCLOSE': '/usr/bin/lesspipe %s %s', 'LESSOPEN': '| /usr/bin/lesspipe %s', 'LOGNAME': 'athxue', 'LS_COLORS': 'no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.flac=01;35:*.mp3=01;35:*.mpc=01;35:*.ogg=01;35:*.wav=01;35:', 'OLDPWD': '/home/athxue', 'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games', 'PATH_INFO': '/test/', 'PWD': '/home/athxue/django-test/test2', 'QT_IM_MODULE': 'scim', 'QUERY_STRING': '', 'REMOTE_ADDR': '127.0.0.1', 'REMOTE_HOST': 'localhost', 'REQUEST_METHOD': 'GET', 'RUN_MAIN': 'true', 'SCRIPT_NAME': '', 'SERVER_NAME': 'localhost', 'SERVER_PORT': '8000', 'SERVER_PROTOCOL': 'HTTP/1.1', 'SERVER_SOFTWARE': 'WSGIServer/0.1 Python/2.4.4c1', 'SESSION_MANAGER': 'local/athxue-desktop:/tmp/.ICE-unix/4254', 'SHELL': '/bin/bash', 'SHLVL': '1', 'SSH_AGENT_PID': '4395', 'SSH_AUTH_SOCK': '/tmp/ssh-yGWBkV4254/agent.4254', 'TERM': 'xterm', 'TZ': 'America/Chicago', 'USER': 'athxue', 'USERNAME': 'athxue', 'WINDOWID': '41945821', 'XAUTHORITY': '/home/athxue/.Xauthority', 'XMODIFIERS': '@im=SCIM', '_': '/usr/bin/python', 'wsgi.errors': <open file '<stderr>', mode 'w' at 0xb7d6a0b0>, 'wsgi.file_wrapper': <class 'django.core.servers.basehttp.FileWrapper'>, 'wsgi.input': <socket._fileobject object at 0xb6e18dbc>, 'wsgi.multiprocess': False, 'wsgi.multithread': True, 'wsgi.run_once': False, 'wsgi.url_scheme': 'http', 'wsgi.version': (1, 0)}>
      resolver  
      <django.core.urlresolvers.RegexURLResolver object at 0xb6dabd4c>
      response  
      None
      self      
      <django.core.handlers.wsgi.WSGIHandler object at 0xb79b590c>
      settings  
      <django.conf.LazySettings object at 0xb7c7280c>
      urlresolvers      
      <module 'django.core.urlresolvers' from '/usr/lib/python2.4/site-packages/Django-0.95.1-py2.4.egg/django/core/urlresolvers.pyc'>
    * /usr/lib/python2.4/site-packages/Django-0.95.1-py2.4.egg/django/core/urlresolvers.py in resolve
       140. def resolve(self, path):
       141. tried = []
       142. match = self.regex.search(path)
       143. if match:
       144. new_path = path[match.end():]
       145. for pattern in self.urlconf_module.urlpatterns:
       146. try:
       147. sub_match = pattern.resolve(new_path) ...
       148. except Resolver404, e:
       149. tried.extend([(pattern.regex.pattern + ' ' + t) for t in e.args[0]['tried']])
       150. else:
       151. if sub_match:
       152. return sub_match[0], sub_match[1], dict(match.groupdict(), **sub_match[2])
       153. tried.append(pattern.regex.pattern)
      ▶ Local vars
      Variable  Value
      match     
      <_sre.SRE_Match object at 0xb6db0e58>
      new_path  
      'test/'
      path      
      '/test/'
      pattern   
      <django.core.urlresolvers.RegexURLPattern object at 0xb6db2c0c>
      self      
      <django.core.urlresolvers.RegexURLResolver object at 0xb6dabd4c>
      sub_match         
      None
      tried     
      ['^admin/']
    * /usr/lib/python2.4/site-packages/Django-0.95.1-py2.4.egg/django/core/urlresolvers.py in resolve
       105. args = match.groups()
       106. # In both cases, pass any extra_kwargs as **kwargs.
       107. kwargs.update(self.default_args)
       108.
       109. try: # Lazily load self.func.
       110. return self.func, args, kwargs
       111. except AttributeError:
       112. self.func = self.get_callback() ...
       113. return self.func, args, kwargs
       114.
       115. def get_callback(self):
       116. mod_name, func_name = get_mod_func(self.callback)
       117. try:
       118. return getattr(__import__(mod_name, '', '', ['']), func_name)
      ▶ Local vars
      Variable  Value
      args      
      ()
      kwargs    
      {}
      match     
      <_sre.SRE_Match object at 0xb6d5bb80>
      path      
      'test/'
      self      
      <django.core.urlresolvers.RegexURLPattern object at 0xb6db2c0c>
    * /usr/lib/python2.4/site-packages/Django-0.95.1-py2.4.egg/django/core/urlresolvers.py in get_callback
       109. try: # Lazily load self.func.
       110. return self.func, args, kwargs
       111. except AttributeError:
       112. self.func = self.get_callback()
       113. return self.func, args, kwargs
       114.
       115. def get_callback(self):
       116. mod_name, func_name = get_mod_func(self.callback) ...
       117. try:
       118. return getattr(__import__(mod_name, '', '', ['']), func_name)
       119. except ImportError, e:
       120. raise ViewDoesNotExist, "Could not import %s. Error was: %s" % (mod_name, str(e))
       121. except AttributeError, e:
       122. raise ViewDoesNotExist, "Tried %s in module %s. Error was: %s" % (func_name, mod_name, str(e))
      ▶ Local vars
      Variable  Value
      self      
      <django.core.urlresolvers.RegexURLPattern object at 0xb6db2c0c>
    * /usr/lib/python2.4/site-packages/Django-0.95.1-py2.4.egg/django/core/urlresolvers.py in get_mod_func
        16.
        17. class NoReverseMatch(Exception):
        18. pass
        19.
        20. def get_mod_func(callback):
        21. # Converts 'django.views.news.stories.story_detail' to
        22. # ['django.views.news.stories', 'story_detail']
        23. dot = callback.rindex('.') ...
        24. return callback[:dot], callback[dot+1:]
        25.
        26. def reverse_helper(regex, *args, **kwargs):
        27. """
        28. Does a "reverse" lookup -- returns the URL for the given args/kwargs.
        29. The args/kwargs are applied to the given compiled regular expression.
      ▶ Local vars 

substring에러라는데 어떤 에러인가요?


인실리코젠이 장고 확산에 일등공신이라는 느낌이 듭니다. 앞으로 장고에 대해 세미나 혹은 강좌를 계획하고 계신것이 있나요?


admin시스템을 사용하려고 설정을 하고 로그인을 했는데 다음과 같은 에러를 만났습니다

ProgrammingError at /admin/
(1146, "Table 'django_test.django_admin_log' doesn't exist")
Request Method:         GET
Request URL:    http://127.0.0.1:8000/admin/
Exception Type:         ProgrammingError
Exception Value:        (1146, "Table 'django_test.django_admin_log' doesn't exist")
Exception Location:     /usr/lib/python2.4/site-packages/MySQLdb/connections.py in defaulterrorhandler, line 35
Template error

In template /usr/lib/python2.4/site-packages/Django-0.95.1-py2.4.egg/django/contrib/admin/templates/admin/index.html, error at line 56

어떤 문제 때문인가요?


계속 장고에 대해 질문하게 되네요 장고를 학습하기가 여의치 않습니다. django book과 공식 홈페이지의 문서를 참조중인데 완전히 독립된 [MVC]에 적응이 잘 안되네요. 어떻게 하면 장고를 효과적으로 학습할수 있을까요?


Django기반으로된 게시판 혹은 블로그가 있을까요?


PHP로만 웹개발을 한지 6개월 된것 같습니다. 사실 PHP는 웹 개발을 위한 스크립트의 형식이 강해 본인의 의지에 의하지 않으면 스파게티코드 가 될수밖에 없는 구조를 가지고 있다고 생각됩니다. 그래서 요즘 체계적인 개발과 반복적인 작업을 줄이기 위한 방편으로 프레임워크를 알아보 다 Django를 찾게 되었습니다. 그러나 궁금한 점은 Django를 통해 원하는 개발을 모두 할 수 있는지 입니다. 예를 들어 쇼핑몰을 개발한다던가, 단순한 홈페이지 형식이 아닌 웹 어플리케이션([ERP]등등)이 가능하냐는 것입니다.


CategoryProgramLibrary

Django (last edited 2007-02-26 15:17:03 by 222)