Django网址空间在主网址后添加

时间:2017-05-18 10:26:30

标签: django django-rest-framework django-urls

我收到此错误消息

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.

请注意1. ^person/[SPACE]duplicate_check

这里的空格

我的项目/ 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)

我到底错过了什么?

1 个答案:

答案 0 :(得分:2)

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

问题与空格有关,但与您的网址无关。 “duplicate_check”包含在person /下,但您正在尝试访问p_check /....

修改您的网址格式实际上存在较大问题。您实际上没有给捕获组提供任何捕获。你需要在括号内使用某种模式。类似的东西:

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

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