def include(module, namespace=None, app_name=None):
from django.conf.urls import include
if django.VERSION < (1,9):
return include(module, namespace, app_name)
else:
return include((module, app_name), namespace)
python类urls()的实例源码
def include(module, namespace=None, app_name=None):
from django.conf.urls import include
if django.VERSION < (1,9):
return include(module, namespace, app_name)
else:
return include((module, app_name), namespace)
def urls(self):
urls = []
for route, view in self.route_views.items():
name = view.model.__name__ if view.model else route.route
# List and detail endpoints
if route.list_endpoint:
urls.append(django.conf.urls.url(r'^{}/$'.format(route.route), view.as_view(), {'router': self}, name=name))
if route.detail_endpoint:
urls.append(django.conf.urls.url(r'^{}/(?P<pk>[0-9]+)/$'.format(route.route), view.as_view(), {'router': self}, name=name))
# History views
if view.model and hasattr(view.model, 'Binder') and view.model.Binder.history:
urls.append(django.conf.urls.url(r'^{}/(?P<pk>[0-9]+)/history/$'.format(route.route), view.as_view(), {'history': 'normal', 'router': self}, name=name))
urls.append(django.conf.urls.url(r'^{}/(?P<pk>[0-9]+)/history/debug/$'.format(route.route), view.as_view(), {'history': 'debug', 'router': self}, name=name))
# File field endpoints
for ff in view.file_fields:
urls.append(django.conf.urls.url(r'^{}/(?P<pk>[0-9]+)/{}/$'.format(route.route, ff),
view.as_view(), {'file_field': ff, 'router': self}, name='{}.{}'.format(name, ff)))
# Custom endpoints
for m in dir(view):
method = getattr(view, m)
if hasattr(method, 'detail_route') or hasattr(method, 'list_route'):
route_name = method.route_name
extra = method.extra_route
kwargs = {'method': m, 'router': self}
if method.unauthenticated:
kwargs['unauthenticated'] = True
if hasattr(method, 'detail_route'):
urls.append(django.conf.urls.url(r'^{}/(?P<pk>[0-9]+)/{}/{}$'.format(route.route, route_name, extra),
view.as_view(), kwargs, name='{}.{}'.format(name, route_name)))
if hasattr(method, 'list_route'):
urls.append(django.conf.urls.url(r'^{}/{}/{}$'.format(route.route, route_name, extra),
view.as_view(), kwargs, name='{}.{}'.format(name, route_name)))
return urls
def urls(self):
urls = []
for route, view in self.route_views.items():
name = view.model.__name__ if view.model else route.route
# List and detail endpoints
if route.list_endpoint:
urls.append(django.conf.urls.url(r'^{}/$'.format(route.route), view.as_view(), {'router': self}, name=name))
if route.detail_endpoint:
urls.append(django.conf.urls.url(r'^{}/(?P<pk>[0-9]+)/$'.format(route.route), view.as_view(), {'router': self}, name=name))
# History views
if view.model and hasattr(view.model, 'Binder') and view.model.Binder.history:
urls.append(django.conf.urls.url(r'^{}/(?P<pk>[0-9]+)/history/$'.format(route.route), view.as_view(), {'history': 'normal', 'router': self}, name=name))
urls.append(django.conf.urls.url(r'^{}/(?P<pk>[0-9]+)/history/debug/$'.format(route.route), view.as_view(), {'history': 'debug', 'router': self}, name=name))
# File field endpoints
for ff in view.file_fields:
urls.append(django.conf.urls.url(r'^{}/(?P<pk>[0-9]+)/{}/$'.format(route.route, ff),
view.as_view(), {'file_field': ff, 'router': self}, name='{}.{}'.format(name, ff)))
# Custom endpoints
for m in dir(view):
method = getattr(view, m)
if hasattr(method, 'detail_route') or hasattr(method, 'list_route'):
route_name = method.route_name
extra = method.extra_route
kwargs = {'method': m, 'router': self}
if method.unauthenticated:
kwargs['unauthenticated'] = True
if hasattr(method, 'detail_route'):
urls.append(django.conf.urls.url(r'^{}/(?P<pk>[0-9]+)/{}/{}$'.format(route.route, route_name, extra),
view.as_view(), kwargs, name='{}.{}'.format(name, route_name)))
if hasattr(method, 'list_route'):
urls.append(django.conf.urls.url(r'^{}/{}/{}$'.format(route.route, route_name, extra),
view.as_view(), kwargs, name='{}.{}'.format(name, route_name)))
return urls