我在项目级别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')),
]
应用程序级别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'),
]
settings.py
:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'present',
]
目录结构:
├── 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
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'
答案 0 :(得分:3)
循环导入是由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')