Pages

10/03/2013

Django(python) - url controller

url을 작성해보자. urls.py에 정의하면되는데 친절하게 example까지 나와있다.

from django.conf.urls import patterns, include, url
from basic_board import views

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'django_board.views.home', name='home'),
    # url(r'^django_board/', include('django_board.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
    url(r'^$', views.home),
    url(r'^boards/', views.getAllList),
    url(r'^test/', views.test),
)
다음은 컨트롤러를 작성하자. testapp폴더에 views.py를 수정하자.
# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.core import serializers

from django.http import HttpResponse

from django.views.decorators.csrf import csrf_exempt

from django.utils import timezone
from django.utils import simplejson

from testapp.models import Board


def home(req):
 return render_to_response('index.html')

def test(req):
 return render_to_response('test.html')

@csrf_exempt
def getAllList(req):
 if req.method == 'POST':
  pass

 boardList = Board.objects.order_by('-id')
 print req.method
 return HttpResponse(serializers.serialize('json', boardList), content_type="application/json") 
source 자세한건 천천히 업데이트해야겟다..콜록

1 개의 댓글: