【问题标题】:Regular expression not matching the given pattern正则表达式与给定模式不匹配
【发布时间】:2019-07-04 19:02:01
【问题描述】:
regular expression not matching.
The current path, attendance/B-2019-07-05, didn't match any of these.

从 django.urls 导入路径,re_path 从 django.contrib.auth 导入视图作为 auth_views 从 。将视图导入为视图

urlpatterns = [
    path('plan/', views.plan_view, name='plan_name'),
    path('journey/', views.Journey_view),
    path('attendance/', views.attendance_view),
    re_path(r'^attendance/(?P<date>\[AB]-\d{4}-\d{2}-\d{2})/$', views.index, name='update_data'),
    path('login/', views.user_login, name='login',),
    path('', views.user_logout, name='logout_now'),
    path('logout/', views.logout_view, name='logout_name'),
    path('register/', views.register),
    path('attendance/', views.attendance_view, name='attendance'),  


    path('passwordchange/', views.PasswordChangeView, name='change_password'),
    path('passwordchangedone/', views.PasswordChangeDoneView, name='password_change_done'),
    # path('attendance/', views.attendance_view),  
    re_path(r'^export/csv/$', views.export_report_csv, name='export_report_csv'),
]

【问题讨论】:

  • \[AB 中的反斜杠在这里做什么?你能分享你的urls.py吗?
  • admin/plan/ [name='plan_name'] 旅程/出席/ ^attendance/(?P[AB]-\d{4}-\d{2}-\d {2})/$ [name='update_data']
  • 这不是urls.py 文件。请edit您的问题。

标签: django


【解决方案1】:

您的re_path 包含反斜杠:

r'^attendance/(?P<date>\[AB]-\d{4}-\d{2}-\d{2})/$'
#                      ^

你应该删除它,因为通过转义[,你指定用户应该*字面上写[,所以它会接受一个URL attendance/[AB]-2019-07-04/

re_path 应如下所示:

re_path(r'^attendance/(?P&lt;date&gt;<b>[AB]</b>-\d{4}-\d{2}-\d{2})/$', views.index, name='update_data'),

【讨论】:

    猜你喜欢
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多