代码之家  ›  专栏  ›  技术社区  ›  floflo29

混合Django和DRF URL

  •  0
  • floflo29  · 技术社区  · 6 年前

    到目前为止,我已经用Django REST框架(DRF)构建了一个REST API,任何前端都可以使用它。让我们调用此API backend 。 我正在尝试添加另一个Django应用程序(这次是常规应用程序,即没有DRF),它共享相同的模型。让我们调用此应用 webapp

    然而,似乎链接到 Web应用程序 不可用。

    这是我的URL。py:

    from django.conf.urls import url, include
    from django.contrib import admin
    from rest_framework import routers
    from rest_framework.authtoken import views as token_views
    from backend import views
    from webapp import views as webapp_views
    
    router = routers.SimpleRouter()
    router.register(r'users', views.UserViewSet, 'User')
    router.register(r'games', views.GameViewSet, 'Game')
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^connect/', views.CustomObtainAuthToken.as_view()),
        url(r'membership_create/', views.MembershipCreate.as_view()),
        url(r'auth/connect_with_fb/', views.ConvertTokenViewWithData.as_view()),
        url(r'auth/connect_with_credentials/', views.TokenViewWithData.as_view()),
        url(r'debug/', views.UserLoginAndIdView.as_view()),
        url(r'^auth/', include('rest_framework_social_oauth2.urls'),
        url(r'^test/', webapp_views.home, name='test'), # the new view
            )
    
    ]
    
    urlpatterns += router.urls
    

    以及单一视图(来自webapp.views):

    from django.http import HttpResponse
    from django.shortcuts import render
    
    def home(request):
        return HttpResponse("""
            <h1>WEBAPP !</h1>
            <p>test webapp</p>
        """)
    

    因此,我想知道DRF和常规Django视图是否可以如此轻松地混合,或者这不是正确的方法。

    编辑:

    Using the URLconf defined in WMC.urls, Django tried these URL patterns, in this order:
    
    ^admin/
    ^connect/
    membership_create/
    auth/connect_with_fb/
    auth/connect_with_credentials/
    debug/
    ^auth/
    ^users/$ [name='User-list']
    ^users/(?P<pk>[^/.]+)/$ [name='User-detail']
    ^users/(?P<pk>[^/.]+)/games/$ [name='User-games']
    ^games/$ [name='Game-list']
    ^games/(?P<pk>[^/.]+)/$ [name='Game-detail']
    The current URL, test/, didn't match any of these.
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   floflo29    6 年前

    移动 url(r'^test/', webapp_views.home, name='test') 之前 url(r'^admin/', admin.site.urls) 解决了这个问题,虽然我不知道为什么。

    urlpatterns = [
            url(r'^test/', webapp_views.home, name='test'),
            url(r'^admin/', admin.site.urls),
            url(r'^connect/', views.CustomObtainAuthToken.as_view()),
            url(r'^membership_create/', views.MembershipCreate.as_view()),
            url(r'^auth/connect_with_fb/', views.ConvertTokenViewWithData.as_view()),
            url(r'^auth/connect_with_credentials/', views.TokenViewWithData.as_view()),
            url(r'^debug/', views.UserLoginAndIdView.as_view()),
            url(r'^auth/', include('rest_framework_social_oauth2.urls'),
    
                )
    
        ]