Django 1.11循环导入错误及URL路径不匹配问题求助
Hey there, let's work through your two Django 1.11 issues—circular imports and the frustrating "current path doesn't match" URL error. I'll break down practical fixes for each below.
First: Resolving Circular Imports
Circular imports pop up when two modules try to import from each other at the top level. Here are the most reliable fixes for Django projects:
Delay imports to inside functions/methods
Instead of importing a module at the top of your file, move the import statement inside the function that actually uses it. This avoids the upfront loop:# Bad: Circular import risk at top level from myapp.views import MyView def my_function(): # ...# Good: Import only when needed def my_function(): from myapp.views import MyView # Use MyView hereExtract shared code to a separate module
If two modules are importing each other to access shared logic (like constants, utility functions, or base classes), move that code into a new module (e.g.,myapp/utils.pyormyapp/core.py). Both original modules can import from this shared file without creating a loop.Use string references for Django model relationships
When defining ForeignKey, ManyToManyField, etc., use a string like'app_name.ModelName'instead of importing the model directly. Django resolves this lazily, skipping the circular import:# Instead of: from otherapp.models import OtherModel class MyModel(models.Model): other = models.ForeignKey(OtherModel, on_delete=models.CASCADE) # Do this: class MyModel(models.Model): other = models.ForeignKey('otherapp.OtherModel', on_delete=models.CASCADE)
Second: Fixing the "Current Path Doesn't Match" URL Error
This error means the URL you're requesting doesn't align with any pattern in your URLconf. Let's check the most common culprits:
Verify your root URLconf includes
Double-check that you've correctly included your app's URLs in the project'surls.py—no typos in the path, regex, or namespace:# Project urls.py from django.conf.urls import url, include urlpatterns = [ url(r'^myapp/', include('myapp.urls')), # Confirm regex and app name are correct ]Check app-level URL patterns
Ensure your app'surls.pyhas valid regex patterns. Common mistakes include missing trailing slashes (Django'sAPPEND_SLASHsetting might handle this, but not always), incorrect capture groups, or typos in view names:# App urls.py from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.home, name='home'), # Matches /myapp/ url(r'^detail/(?P<id>\d+)/$', views.detail, name='detail'), # Matches /myapp/detail/123/ ]Confirm the requested URL matches exactly
Django's URL matching is case-sensitive and exact (unless your regex uses wildcards). If you're requestinghttp://localhost:8000/myapp/Detail/123/but your pattern usesdetail, it won't match. Also, check for extra slashes or missing path segments.Check named URL usage
If you're usingreverse()or template{% url %}tags, make sure you're passing the correct arguments and using the right namespace (if set ininclude()). A missing or incorrect argument can generate a non-existent URL, triggering the error.
Your Traceback for Reference
Traceback (most recent call last):
File "C:\Python3\envs\myDjangoEnv\lib\site-packages\django\utils\autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "C:\Python3\envs\myDjangoEnv\lib\site-packages\django\core\management\commands\runserver.py", line 124, in inner_run
self.check(display_num_errors=True)
File "C:\Python3\envs\myDjangoEnv\lib\site-packages\django\core\management\base.py", line 359, in check
include_deployment_checks=include...
内容的提问来源于stack exchange,提问作者Elijah Lewis

