将DRF路由器与django urlpattern结合使用

时间:2018-04-26 13:44:17

标签: django django-rest-framework

如何使用app_name = 'scrumboard'将django urlpatterns与DRF路由器结合在一起?我尝试了DRF documentation解释过的所有方法,但仍然无法解决这个问题。到目前为止,我有以下urls.py文件:

#scrumboard/urls.py

from rest_framework.routers import DefaultRouter
from .api import ListViewSet, CardViewSet
from django.urls import re_path, path, include
from . import views

router = DefaultRouter()
router.register(r'lists', ListViewSet)
router.register(r'cards', CardViewSet)

app_name = 'scrumboard'
urlpatterns = [
    path('', views.index, name="index"),
    re_path(r'^api/', include((router.urls))),
]

这是我的项目urls.py:

#project/urls.py

from django.contrib import admin
from django.urls import include, path, re_path
#Add URL maps to redirect the test URL to our application
from django.views.generic import RedirectView
# Use static() to add url mapping to serve static files during development (only)
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('', include('home.urls')),
    path('admin/', admin.site.urls),
    path('accounts/', include('django.contrib.auth.urls')),
    path('test/', RedirectView.as_view(url='/catalog/', permanent=True)),
    path('polls/', include('polls.urls')),
    path('blog/', include('blog.urls')),
    path('catalog/', include('catalog.urls')),
    path('scrumboard/', include('scrumboard.urls'))
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

在我的模板中,我将有机会定义动态网址,如下所示:

<li><a href="{% url 'scrumboard:index' %}">Scrumboard</a></li>

将把我带到我的Scrumboard应用程序。

使用上面的代码,当我转到此url localhost:8000 / scrumboard /我收到错误

enter image description here

更新

当我去localhost:8000 / scrumboard / api时效果很好

enter image description here

更新

完全追溯http://dpaste.com/07QR05W

更新

添加了项目urls.py代码

2 个答案:

答案 0 :(得分:0)

我们如何期望/scrumboard/匹配第一个正则表达式?是否有一个外部urls.py文件,其中包含您提到的文件?

如果没有更多信息,这是否有效?

urlpatterns = [
    path('^scrumboard/?$', views.index, name="index"),
    re_path(r'^api/', include((router.urls))),
]

答案 1 :(得分:0)

我只是想删除这个问题,但我想也许其他人会反对这一点并决定描述问题究竟是什么。

由于经验不足,我在错误的地方寻找问题。

按照主页模板上的链接:

<li><a href="{% url 'scrumboard:index' %}">Scrumboard</a></li>
#scrumboard/urls.py

from django.urls import re_path, path, include
from . import views

app_name = 'scrumboard'
urlpatterns = [
    path('', views.index, name="index"),
]

我们运行了一个在view.py中定义的函数,该函数接受此请求并在scrumboard/index.html下呈现模板

#scrumboard/views.py

def index(request):
    return render(request, 'scrumboard/index.html', {})

这里有错误。

Exception Type: NoReverseMatch at /scrumboard/
Exception Value: Reverse for 'lists' not found. 'lists' is not a valid view function or pattern name.

我使用了django Reversing namespaced URLs,因为我有几个应用程序,每个应用程序都有自己的index.html模板。我不得不猜测问题出在这个模板的某个地方:

{% extends 'base.html' %}
{% load static %}


{% block title %}
    <title>Scrumboard</title>
{% endblock %}
{% block extracss %}
    <link rel="stylesheet" href="{% static 'scrumboard/css/main.css' %}" />
{% endblock %}

{% block content %}
        <a href="{% url 'scrumboard:lists' %}" target="_blank" class="top-menu">Lists</a><br>
        <a href="{% url 'scrumboard:cards' %}" target="_blank" class="top-menu">Cards</a>
        <hr>
          <div ng-app="scrumboard.demo">
            {% verbatim %}
            <div ng-controller="ScrumboardController">
              <div ng-repeat="list in data">
                <h3>{{list.name}}</h3>
                <ul>
                  <li ng-repeat="card in list.cards">{{card.title}}</li>
                </ul>
                <div class="input-group">
                  <input type="text" ng-model="new_title">
                  <button class="btn btn-outline-secondary" ng-click="add(list, new_title)">+</button>
                </div>
              </div>
            </div>
            {% endverbatim %}
              <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
              <script type="text/javascript" src="{% static 'scrumboard/js/scrumboard.js' %}"></script>
          </div>
{% endblock %}

看到这些界限,我认为问题可能在于:

<a href="{% url 'scrumboard:lists' %}" target="_blank" class="top-menu">Lists</a><br>
<a href="{% url 'scrumboard:cards' %}" target="_blank" class="top-menu">Cards</a>

我决定只评论他们。在认为问题已经解决之后,我重新加载了页面,但错误仍然会再次出现。

<!-- <a href="{% url 'scrumboard:lists' %}" target="_blank" class="top-menu">Lists</a><br>
<a href="{% url 'scrumboard:cards' %}" target="_blank" class="top-menu">Cards</a> -->

但是django忽略了通常的html注释并尝试阅读注释代码。所以我的错误没有消失,我开始在其他地方寻找问题。 由于缺乏经验,我问了一个不正确的问题。 只需删除此行即可解决我的问题。