开发者

Custom Django admin site with parent url parameter

my url pattern looks like that: (r'^fb/custom/(?P[a-zA-Z0-9+]*)/admin/', include(custom_admin_site.urls)),

I overrode the admin_view methode of my admin site:

def admin_view(self, view, cacheable=False):
    def inner(request, *args, **kwargs):
        if kwargs.has_key('custom_id'):
            request.custom_id = kwargs.pop('custom_id')
  开发者_开发问答      return view(request, *args, **kwargs)
    if not cacheable:
        inner = never_cache(inner)
    # We add csrf_protect here so this function can be used as a utility
    # function for any view, without having to repeat 'csrf_protect'.
    if not getattr(view, 'csrf_exempt', False):
        inner = csrf_protect(inner)
    return update_wrapper(inner, view)

This way I don't need the paramter custom_id in the view methods like index. My problem is that i can't use urlresolvers.reverse('custom-admin:index'). If I use it without parameter i get this error:

Page not found. Request URL:    http://localhost:8000/fb/custom/(?P%3Ccustom_id%3E[a-zA-Z0-9%5C+]*)/admin/

Ok no suprise. I didn't provide the parameter custom_id. But with the paramter I get this error:

reverse() got an unexpected keyword argument 'custom_id'

Any idea how to solve this. I would really like to use the reverse lookup. The url template tag has the same problem.


A few issues with your url pattern:

  1. Unless your are trying to match empty string for your custom id (which I would guess you are not) you should use + instead of *.
  2. The + sign should go outside of your brackets.
  3. If you are just trying to match letters and numbers, your regex can be simplified to [\w]+
  4. Finally, if you want a named argument for custom id, you must include the name in your pattern.

So ultimately, your url patter should look like this:

(r'^fb/custom/(?P<custom_id>[\w]+)/admin/', include(custom_admin_site.urls)),

Now I'm not sure how you're trying to call urlresolvers.reverse, but if you need to pass args or kwargs, it should something look like one of these:

urlresolvers.reverse('custom-admin:index', args=[custom_id])

or for kwargs, such as in the pattern I included above:

urlresolvers.reverse('custom-admin:index', kwargs={'custom_id':custom_id})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜