【问题标题】:when i run my django project on terminal i get the AttributeError: 'str' object has no attribute 'regex'当我在终端上运行我的 django 项目时,我得到 AttributeError: 'str' object has no attribute 'regex'
【发布时间】:2016-09-09 03:00:54
【问题描述】:

当我在终端上运行我的 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 文件

urlpatterns =[
        url(r'^api/account/', include("wheredego_service.accounts.account_urls")),
        url(r'^api/fileupload/', include("wheredego_service.file_manager.file_manager_urls")),
        url(r'^api/activity_category', include(
                               "wheredego_service.activity_category.activity_category_urls")),
        url(r'^api/destination', include("wheredego_service.destinations.destination_urls")),
        url(r'^api/trip', include("wheredego_service.trip.trip_urls")),
        url(r'^api/bucket', include("wheredego_service.bucket.bucket_urls")),
        url(r'^api/experience', include("wheredego_service.experience.experience_urls")),
        url(r'^api/region', include("wheredego_service.region.region_urls")),
        url(r'^api/friend', include("wheredego_service.friend.friend_urls")),
    ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这是 My 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()), ]

请帮助解决这个错误。

【问题讨论】:

  • 请将问题出处的项目urls.py 的内容发布出来。您的其中一个 URL 定义有误。
  • 我在项目中有不同的应用程序,每个应用程序都有不同的 url 文件。我应该发布所有还是主要的 url 文件?
  • 这是我的主要网址文件
  • 错误出现在单个应用程序的 URL 定义之一中……您有很多!
  • 我已经发布了我的一个应用程序的网址,请你看看

标签: python django rest frameworks


【解决方案1】:

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

您需要查看您的 urls.py 并找到类似的内容:

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

很遗憾,我们无法确定问题出在哪个urls.py。您可以尝试将includes() 逐一注释掉。如果错误停止,那么您已找到导致问题的原因。

在 Django 1.10 中,url 检查略有改进,check_pattern_startswith_slash 不会再失败,而是会收到关于字符串的特定警告。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 2017-10-26
    相关资源
    最近更新 更多