我在项目的主urls.py
文件中有以下内容:
# REST Framework packages
from rest_framework import routers
router = routers.DefaultRouter()
# ... My viewsets serialized
router.register(r'users', UserViewSet)
# ... Another viewsets
urlpatterns = [
url(r'^$', HomeView.as_view(), name='home'),
# Home url in my project
url(r'^', include('userprofiles.urls')),
# Call the userprofiles/urls.py application
url(r'^pacientes/', include('userprofiles.urls', namespace='pacientes')),
# Patients url
url(r'^api/', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
# Rest frameworks urls
]
在此之前,当我在浏览器中输入对本地服务器的调用http://localhost:8000/api/
时,我会得到这样的响应:
我的RESTurl's
序列化模型也会出现
之后,我在userprofiles/urls.py
应用程序中创建了一个附加的url,并使用以下方式的一些正则表达式:
from .views import PatientDetail
urlpatterns = [
url(r'^(?P<slug>[\w\-]+)/$', PatientDetail.as_view(), name='patient_detail'),
]
当我转到http://localhost:8000/api/
时,我得到了这样的回答:
Not Found: /api/
[08/Dec/2016 16:42:26] "GET /api/ HTTP/1.1" 404 1753
找不到我的rest frameworks url,在我的浏览器中,表示调用PatientDetailView的url是此问题的根源:
我的患者详细信息如下:
class PatientDetail(LoginRequiredMixin, DetailView):
model = PatientProfile
template_name = 'patient_detail.html'
context_object_name = 'patientdetail'
def get_context_data(self, **kwargs):
context=super(PatientDetail, self).get_context_data(**kwargs)
# And other actions and validations here in forward ...
在userprofiles/urls.py
中定义的正则表达式中,我正在执行以下操作:
url(r'^(?P<slug>[\w\-]+)/$', PatientDetail.as_view(), name='patient_detail')
模型PatientProfile有一个slug字段(patient的用户名)。我在url中传递这个slug。在
此外,我希望允许我的字母数字字符大写和小写与[\w\-]
参数,并允许下划线和连字符和多次。在
我的正则表达式可能是问题的根源吗?
找不到我的/api
django restframework url会发生什么情况?在
定义URL的顺序很重要。Django尝试根据每个模式匹配您的URL,直到其中一个匹配为止。在
请注意,您包括以下行:
在此之前:
^{pr2}$这不是问题,因为第一个匹配的模式是后者。在
但是,当您添加
PatientDetail
视图URL模式时:/api/
匹配。因此,调用了PatientDetail
视图,发生404错误的原因是没有找到用户名为api的患者,而不是因为找不到URL。在api
与使用in'的正则表达式[\w\-]+
完全匹配用户配置文件.url'. 所以当您输入http://localhost:8000/api/
时,Django返回第一个找到的urlpattern,它是url(r'^', include('userprofiles.urls'))
。尝试交换行:相关问题 更多 >
编程相关推荐