django-piston错误的重定向

时间:2011-09-10 09:34:05

标签: python django django-piston

我的django api有问题。我有一个运行良好的网站,现在我想用django-piston制作api来制作Android应用程序。当我尝试获取一些数据,并在网址中发送了一些密钥时,我的api项目的处理程序是空的,这些是我在api app中的urls.py和handlers.py。

urls.py

from django.conf.urls.defaults import *
from piston.resource import Resource
from myproject.api.handlers import ProgramadorHandler
from myproject.api.handlers import UserHandler
from myproject.api.handlers import GetProgHandler
from myproject.api.handlers import GetUserHandler

prog_handler = Resource(ProgramadorHandler)
user_handler = Resource(UserHandler)
get_prog_handler = Resource(GetProgHandler)
get_user_handler = Resource(GetUserHandler)

urlpatterns = patterns('',
url(r'^1.0/(?P<username>)\w+/(?P<password>)\w+/(?P<api_key>)\d+/programadores.(?P<emitter_format>.+)$', prog_handler),
url(r'^1.0/(?P<username>)\w+/(?P<password>)\w+/(?P<api_key>)\d+/programadores/(?P<prog_id>\d+)/prog.(?P<emitter_format>.+)$', get_prog_handler),
url(r'^1.0/(?P<username>)\w+/(?P<password>)\w+/(?P<api_key>)\d+/usuarios.(? P<emitter_format>.+)$', user_handler),
url(r'^1.0/(?P<username>)\w+/(?P<password>)\w+/(?P<api_key>)\d+/user.(? P<emitter_format>.+)$', get_user_handler),
)

这是我的handlers.py

from piston.handler import BaseHandler
from piston.utils import rc, HttpStatusCode
from myproject.web.models import *
from django.contrib.auth import authenticate

class ProgramadorHandler(BaseHandler):
allowed_methods = ('GET',)
model = Programador

def read(self, request, username, password, api_key):
    user = authenticate(username=username, password=password)
#       if (api_key != 29010):
#           return rc.FORBIDDEN

    if user is not None:
        return Programador.objects.filter(nombre="Programador completo 2")

    return Programador.objects.all()


class GetProgHandler(BaseHandler):
allowed_methods = ('GET',)
model = Programador

def read(self, request, username, password, api_key, prog_id):
    user = authenticate(username=username, password=password)
    if user is None or (api_key != 29010):
        return rc.FORBIDDEN

    return Programador.objects.all()


class UserHandler(BaseHandler):
allowed_methods = ('GET',)
model = Usuario

def read(self, request, username, password, api_key):
    user = authenticate(username=username, password=password)
    if(api_key != 29010):
        return rc.FORBIDDEN

    return Usuario.objects.all()


class GetUserHandler(BaseHandler):
allowed_methods = ('GET',)
model = Usuario

def read(self, request, username, password, api_key):
    #user = authenticate(username=username, password=password)
#       if (api_key != 29010):
#           user = "hola"
        #return rc.FORBIDDEN

    return Usuario.objects.get(username="ersame")

我如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我想你可能在urls.py中有你的正则表达式错了。组的模式说明符需要在括号内,例如

而不是:

url(r'^1.0/(?P<username>)\w+/(?P<password>)\w+/(?P<api_key>)\d+/programadores.(?P<emitter_format>.+)$', prog_handler), ...

尝试:

url(r'^1.0/(?P<username>\w+)/(?P<password>\w+)/(?P<api_key>\d+)/programadores.(?    P<emitter_format>.+)$', prog_handler), ...