简体   繁体   English

Django Deliciouspie:带有“空格”的反向URL。 怎么样?

[英]Django Tastypie: reverse url with “spaces”. How?

It's driving me crazy: 这让我疯狂:

  • I've got a Tastypie custom resource (not ModelResource). 我有一个Tastypie定制资源(不是ModelResource)。
  • I need to allow "spaces" (%20 code) for the "primary key". 我需要为“主键”允许“空格”(%20代码)。 For example: 例如:

    localhost:8000/api/v1/collections/i%20am%20a%20space/

  • Adding a regex with "\\s" works for GET (obj_get) , PUT (obj_update) and DELETE (obj_delete) 添加带有“ \\ s”的正则表达式适用于GET (obj_get)PUT (obj_update)DELETE (obj_delete)

     def override_urls(self): return [url(r"^(?P<resource_name>%s)/(?P<name>[\\w\\d\\s_.-]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail")] 
  • The problem is with obj_get_list : when returning the object list, the parameter resource_uri is empty if a space is present. 问题出在obj_get_list :返回对象列表时,如果存在空格,则参数resource_uri为空。 The code fails in the function reverse (django.core.urlresolvers) with a NoReverseMatch exception. 代码在具有NoReverseMatch异常的reverse函数(django.core.urlresolvers)中失败。

I tried even to overwrite the base_urls from tastypie but it didn't work and I am really lost right now. 我什至尝试覆盖了好吃的base_urls,但是它没有用,我现在真的迷路了。 Maybe it's a noob question but take into account that I started with django and python about 1 month ago: I am a noob :-P 也许这是一个菜鸟问题,但考虑到大约一个月前我从django和python开始:我是菜鸟:-P

刚刚在正则表达式中更改了“ pk”的“名称”:

^(?P<resource_name>%s)/(?P<name>[\w\d\s_.-]+)/$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM