AttributeError: 'str' object has no attribute 'regex' django 1.9

前端 未结 4 1322
无人共我
无人共我 2021-02-19 22:28

I am working with django 1.9 and I am currently coding - in Windows Command Prompt - python manage.py makemigrations and the error:

AttributeErr

4条回答
  •  囚心锁ツ
    2021-02-19 22:55

    Also make sure to remove the beginning empty url pattern--can be overlooked when migrating your urls.

    urlpatterns = ['', # <== this blank element ('') produces the error.
        ...
    ]
    

    tl;dr

    For the curious, I found this out by adding a warning to the check_pattern_startswith_slash method in the django.core.checks.urls module:

    def check_pattern_startswith_slash(pattern):
        """
        Check that the pattern does not begin with a forward slash.
        """
        if not hasattr(pattern, 'regex'):
            warning = Warning(
                "Invalid pattern '%s'" % pattern,
                id="urls.W002",
            )
            return [warning]
    

    And, sure enough, I got a bunch of warnings like this:

    ?: (urls.W002) Invalid pattern ''
    

提交回复
热议问题