3
我無法添加指向view.py中方法的工人URL。在下面的urls.py配置中,我創建了一個DefaultRouter,並註冊了6個URL。前5個工作正常(他們是基於類的視圖),但最後一個URL(工作人員,這是基於方法的視圖)不起作用。該URL與url.conf中列出的任何URL都不匹配。錯誤消息我正在'使用maidFactory.urls中定義的URLconf,Django按以下順序嘗試了這些URL模式: 。 。 。 。 。 。目前的網址,工作人員,沒有任何匹配。「無法在Django路由器中添加網址
基於router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
router.register(r'slots', views.SlotViewSet)
router.register(r'city', views.CityViewSet)
router.register(r'location',views.LocationViewSet,base_name='locationMy')
router.register(r'workers',views.WorkerViewSet,base_name='getWorkersBySlotAndLocation')
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
url(r'^', include(router.urls)),
#url(r'^', include('maidFactory.api.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^auth/', include('rest_framework_social_oauth2.urls')),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))]
我的方法觀點如下:
def WorkerViewSet(request):
cursor = connection.cursor()
#cursor.execute("UPDATE bar SET foo = 1 WHERE baz = %s", [self.baz])
cursor.execute("select p.wid,p.fname, a.description from workerProfile as p, workerAccount as a where a.isactive=1 and a.wid=p.wid")
row = cursor.fetchone()
return HttpResponse(row)
但是,如果我在URL模式如下增加工人本身,它的工作原理: URL模式= [ URL(R '^',包括(router.urls)), URL(R '^工/' (r'^ auth /',include('rest_framework_social_oauth2.urls')), url(r'^ admin /',include(admin.site.urls)), url '^ api-auth /',include('rest_framework.urls',namespace ='rest_framework')) ] 但是我想將它添加到DefaultRouter本身,以便我可以管理它們。 –