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

时间:2016-05-13 04:11:01

标签: python django rest frameworks

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

这是我的旅行应用的观点,我在所有网址文件中都遵循相同的模式。

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

请帮助解决此错误。

1 个答案:

答案 0 :(得分:1)

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

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

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

不幸的是,我们无法确定问题所在的urls.py。您可以尝试逐一评论includes()。如果错误停止,那么您找到了导致问题的那个。

在Django 1.10中,网址检查略有改进,check_pattern_startswith_slash不再失败,您将收到有关该字符串的特定警告。