当我在终端上运行Django项目时,我得到AttributeError:'str'对象没有属性'regex'

2024-10-03 21:26:51 发布

您现在位置:Python中文网/ 问答频道 /正文

当我在终端上运行Django应用程序时,我收到以下错误:

Unhandled exception in thread started by <function wrapper at 0x7f51ce501aa0>
    Traceback (most recent call last):
      File "/home/macrewsupreet/heroku_wheredego1/wheredg-backend/macrewsupreet/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
        fn(*args, **kwargs)
      File "/home/macrewsupreet/heroku_wheredego1/wheredg-backend/macrewsupreet/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
        self.check(display_num_errors=True)
      File "/home/macrewsupreet/heroku_wheredego1/wheredg-backend/macrewsupreet/local/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
        include_deployment_checks=include_deployment_checks,
      File "/home/macrewsupreet/heroku_wheredego1/wheredg-backend/macrewsupreet/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
        new_errors = check(app_configs=app_configs)
      File "/home/macrewsupreet/heroku_wheredego1/wheredg-backend/macrewsupreet/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
        return check_resolver(resolver)
      File "/home/macrewsupreet/heroku_wheredego1/wheredg-backend/macrewsupreet/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 31, in check_resolver
        warnings.extend(check_pattern_startswith_slash(pattern))
      File "/home/macrewsupreet/heroku_wheredego1/wheredg-backend/macrewsupreet/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 67, in check_pattern_startswith_slash
        regex_pattern = pattern.regex.pattern
    AttributeError: 'str' object has no attribute 'regex'

我的主urls.py文件

^{pr2}$

这是我的Trip应用程序的视图,我在所有url文件中都遵循相同的模式。在

from django.conf.urls import url
from wheredego_service.trip import trip_views
from wheredego_service.trip.itinerary import itinerary_views

urlpatterns =[
url(r'^/(?P<trip_id>[0-9,a-z,_]+)/itinerary', itinerary_views.ListItineraryTripView.as_view()), url(r'^/me', trip_views.ListMyTripsView.as_view()), ]

请帮助解决此错误。在


Tags: djangoinpybackendhomeherokulibpackages
1条回答
网友
1楼 · 发布于 2024-10-03 21:26:51

在一个urls.py文件中,URL模式列表中有一个字符串。这可能是因为您在从patterns(prefix, ...)切换到url()列表时忘记删除前缀

你需要看看你的网址.py找到一些类似的东西:

urlpatterns =[
    '' # <- this is the problem, remove this string
    url(...)
    url(...)
]

不幸的是,我们不可能分辨出问题出在哪个urls.py。您可以尝试逐个注释includes()。如果错误停止,则您已找到导致问题的原因。在

在django1.10中,url检查有了一些改进,check_pattern_startswith_slash不会再失败了,您将得到关于字符串的特定警告。在

相关问题 更多 >