我在项目级urls.py文件中收到以下循环导入错误:
ImproperlyConfigured: The included URLconf 'pres_value.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
需要明确的是,pres_value是项目级别,present是应用级别。所以显然错误发生在项目级urls文件中。该应用程序已在settings.py文件中注册。
对这里出了什么问题的任何帮助表示赞赏。
项目级别pres_value/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('present/', include('present.urls')),
]
Run Code Online (Sandbox Code Playgroud)
应用级别present/urls.py:
from django.urls import path
from .views import HomePageView, PresentValueView, AboutPageView, ResultView
urlpatterns = [
path('', HomePageView.as_view(), name='home'),
path('about/', AboutPageView.as_view(), name='about'),
path('pv/', PresentValueView.as_view(), name='present_value'),
path('result/', ResultView.as_view(), name='result'),
]
Run Code Online (Sandbox Code Playgroud)
settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'present',
]
Run Code Online (Sandbox Code Playgroud)
目录结构:
??? Pipfile
??? Pipfile.lock
??? db.sqlite3
??? manage.py
??? pres_value
? ??? __init__.py
? ??? __pycache__
? ??? settings.py
? ??? urls.py
? ??? wsgi.py
??? present
? ??? __init__.py
? ??? __pycache__
? ??? admin.py
? ??? apps.py
? ??? forms.py
? ??? migrations
? ? ??? 0001_initial.py
? ? ??? __init__.py
? ? ??? __pycache__
? ??? models.py
? ??? templates
? ? ??? present
? ? ??? about.html
? ? ??? home.html
? ? ??? pv.html
? ? ??? result.html
? ??? tests.py
? ??? urls.py
? ??? views.py
??? templates
??? base.html
??? footer.html
??? header.html
??? nav.html
Run Code Online (Sandbox Code Playgroud)
views.py
from django.shortcuts import reverse
from django.views.generic import TemplateView
from django.views.generic.edit import CreateView
from .forms import PresentForm
from .models import PresentValue
# Create your views here.
class HomePageView(TemplateView):
template_name = 'present/home.html'
class PresentValueView(CreateView):
model = PresentValue
template_name = 'present/pv.html'
# fields = ['first', 'second']
success_url = reverse('result')
class AboutPageView(TemplateView):
template_name = 'present/about.html' ## this is where I show an error
class ResultView(TemplateView):
template_name = 'present/result.html'
Run Code Online (Sandbox Code Playgroud)
循环导入是由reverse().
解决方法:使用
reverse_lazy.
from django.core.urlresolvers import reverse_lazy
class PresentValueView(CreateView):
model = PresentValue
template_name = 'present/pv.html'
# fields = ['first', 'second']
success_url = reverse_lazy('result')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
602 次 |
| 最近记录: |