gpt4 book ai didi

python - django-piston 的错误重定向

转载 作者:太空宇宙 更新时间:2023-11-03 19:27:57 25 4
gpt4 key购买 nike

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

url.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")

我该如何解决这个问题?

最佳答案

我认为 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), ...

关于python - django-piston 的错误重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7370860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com