问题
Checking the documentation doesn't show any potential cause for the error. I have a django project with a number of apps (dir layout:
)
settings.py (cryptoboard):
...
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
AUTH_USER_MODEL = "cryptousers.CryptoUser"
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'leads',
'rest_framework',
'frontend',
'knox',
'cryptousers',
'cryptocurrency',
]
...
ROOT_URLCONF = 'cryptoboard.urls'
urls.py (cryptoboard):
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('frontend.urls')),
path('', include('leads.urls')),
path('', include('cryptousers.urls')),
path('', include('cryptocurrency.urls')) # <== WORKS IF COMMENTED OUT
]
urls.py (cryptocurrency):
import sys
from django.conf.urls import url
from django.urls import path
from . import views
urlpatterns = [
path('get_currency_latest/', views.get_latest_currency, name='get_currency_latest'),
# this used to work
# url(r'^get_currency_on_date/(?P<date_day>\d{4}-\d{2}-\d{2})/$',
# views.get_currency_on_date, name='get_currency_on_date'),
# # Sample:
# # http://127.0.0.1:8000/get_currency_between_dates/2020-11-24/2020-11-25
# url(r'^get_currency_between_dates/(?P<date_start_day>\d{4}-\d{2}-\d{2})/(?P<date_end_day>\d{4}-\d{2}-\d{2})$',
# views.get_currency_between_dates, name='get_currency_between_dates')
re_path(r'^get_currency_on_date/(?P<date_day>\d{4}-\d{2}-\d{2})/$',
views.get_currency_on_date, name='get_currency_on_date'),
re_path(r'^get_currency_between_dates/(?P<date_start_day>\d{4}-\d{2}-\d{2})/(?P<date_end_day>\d{4}-\d{2}-\d{2})$',
views.get_currency_between_dates, name='get_currency_between_dates')
]
The same error is thrown regardless whether the urls.py above is as it is or empty.
views.py (cryptocurrency)
from django.shortcuts import render
import json
from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.utils import timezone
from django.utils.datetime_safe import datetime
from django.views import generic
from .coins_constants import coins_ids, IS_DEBUG_MODE, DATE_FORMAT
from .get_coins_scheduler import update_coins_table
from .models import Currency
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from .setup_test_db.setup_debug_tables import init_debug_tables
if IS_DEBUG_MODE:
print('[!!!INFO!!!] DEBUG MODE SET! USING GENERATED TABLES')
init_debug_tables()
# Don't use django for ui
def get_latest_currency(self):
"""
Return most up to date value
"""
update_coins_table()
up_to_date_currency = Currency.objects.order_by('-currency_value_in_dollars_date')[:len(coins_ids)]
return JsonResponse({"up_to_date_currency": list(up_to_date_currency.values())})
(all views are functions at this point).
models.py (cryptocurrency):
from django.db import models
class DebugConf(models.Model):
is_setup = models.BooleanField(default=False)
debug_setup_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.is_setup
class Currency(models.Model):
currency_name = models.CharField(max_length=100, unique=True)
currency_value = models.FloatField()
currency_value_in_dollars = models.FloatField()
currency_value_in_dollars_date = models.DateTimeField()
def __str__(self):
return self.currency_name
class Transaction(models.Model):
transaction_type = models.CharField(max_length=200)
transaction_amount = models.FloatField()
transaction_date = models.DateTimeField(auto_now_add=True)
transaction_currency = models.ForeignKey(Currency, on_delete=models.CASCADE)
transaction_buyer = models.ForeignKey('cryptousers.CryptoUser', related_name='transaction_buyer', on_delete=models.CASCADE)
transaction_seller = models.ForeignKey('cryptousers.CryptoUser', related_name='transaction_seller', on_delete=models.CASCADE)
def __str__(self):
return self.transaction_currency
Full stack trace:
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\urls\resolvers.py", line 591, in url_patterns
iter(patterns)
TypeError: 'module' object is not iterable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\dev\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\dev\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\core\management\base.py", line 396, in check
databases=databases,
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\urls\resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\dev\.virtualenvs\crypto-currency-board-2JkLYBxc\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.ImproperlyConfigured: The included URLconf 'cryptoboard.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.
Most answers to similar questions suggest to look for typos in the string 'urlpatterns' but this doesn't seem to be the case here. What am I missing?
回答1:
You do not have an import of re_path like commented already:
from django.urls import re_path
As this will always cause an error, you must have got a reference to a re_path by some way . . And THAT re_path can not be used like re_path(...) because it must be a different type of object ... now lets find the source if that re_path
回答2:
I'm not so sure why it works, but deleting all tables, recreating and rerunning migrations, and reordering the urls:
urls.py (cryptoboard)
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('frontend.urls')),
path('', include('leads.urls')),
path('', include('cryptousers.urls')),
path('', include('cryptocurrency.urls'))
]
to
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('cryptocurrency.urls')), # <= DOES NOT DEPEND ON ANY OF THE APPS
# BELOW (ONLY A MODEL FROM CRYPTOUSERS)
path('', include('frontend.urls')),
path('', include('leads.urls')),
path('', include('cryptousers.urls')),
]
works in my case.
The circular error import exception message seems to be an umbrella message in that, it cannot be caused only by an actual circular import. Running manage.py in the debugger revealed that a package (pika) was also missing (needed by dramatiq) among other stuff ...
来源:https://stackoverflow.com/questions/65753866/the-included-urlconf-appname-urls-does-not-appear-to-have-any-patterns-in-it