Django urls space 在 main url 之后添加

Django urls space gets added after main url

我收到此错误消息

Using the URLconf defined in esarcrm.urls, Django tried these URL patterns, in this order:
1. ^person/ duplicate_check/(?P<entity>)/(?P<full_name>)/?$
2. ^admin/
3. ^api/v1/
4. ^api/v1/authenticate/$ [name='api_authenticate']
5. ^static\/(?P<path>.*)$
6. ^media\/(?P<path>.*)$
The current path, person/duplicate_check/candidate/tom, didn't match any of these.

请不要space这里1. ^person/[SPACE]duplicate_check

我的project/urls.py

urlpatterns = [
    url(r'^person/', include('person.urls')),
    url(r'^admin/', admin.site.urls),
    url(r'^api/v1/', include(router.urls)),
    url(r'^api/v1/authenticate/$', crm_views.ApiAuthenticateView.as_view(), name='api_authenticate'),
]

我的app.urls

urlpatterns = [
    url(r'duplicate_check/(?P<entity>)/(?P<full_name>)/?$', views.check_if_exist),
]

我的app.views

@api_view(['GET'])
def check_if_exist(request, entity, first_name):
    if entity == 'candidate':
        candidates = person_models.Candidate.objects.filter(first_name=first_name)
        serializer = person_serializers.CandidateMiniSerializer(candidates, many=True)
        return Response(serializer.data)

我到底错过了什么?

没有 space,这就是 Django 打印 URL 的方式。

问题与 space 无关,而与您的 URL 有关。 "duplicate_check" 包含在 person/ 下,但您正在尝试访问 p_check/....

编辑 您的 URL 模式实际上存在更大的问题。您实际上并没有给捕获组任何要捕获的东西。您需要在括号内使用某种模式。类似于:

r'^duplicate_check/(?P<entity>\w+)/(?P<full_name>\w+)/?$'

这将捕获实体和 full_name 的所有字母数字字符。