我得到的错误是
django.core.exceptions.ImproperlyConfigured:包含的URLconf “ buttonpython.urls”似乎没有任何模式。如果你 查看文件中的有效模式,则问题可能是由于 循环导入。
我正在遵循较旧的django教程,因为从字面上看,我可以找到所有这些并得到他的错误。 这是urls.py:
from . import views
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.button)
]
Settings.py(之所以包括在内,是因为我在其中找到了buttonpython.urls变量):
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'buttonpython.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'buttonpython.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
Views.py:
from django.shortcuts import render
from . import urls.py
import requests
from bs4 import BeautifulSoup
def button(request):
return render(request, 'home.html')
def output(request):
def trade_spider(max_pages):
page = 1
data = ''
while(page <= max_pages):
search = 'Ipad'
url = 'https://www.ebay.com/sch/i.html?_from=R40&_nkw=' + search + '&_sacat=0&_pgn=' + str(page)
src = requests.get(url)
text = src.text
soup = BeautifulSoup(text, features="html.parser")
for link in soup.findAll('a', {'class': 's-item__link'}):
href = link.get('href')
title = link.string
if(title == None):
title = "Sorry the title was unavailable however you can check the price."
price = get_item_price(href)
data += href + '\n' + title + '\n' + price + '\n'
page+=1
return data
def get_item_price(item_url):
src = requests.get(item_url)
text = src.text
soup = BeautifulSoup(text, features="html.parser")
for item_price in soup.findAll('span', {'class': 'notranslate'}):
price = item_price.string
return price
data = trade_spider(1)
return render(request, 'home.html', {'data':data})
我很沮丧,将不胜感激。
答案 0 :(得分:0)
如错误所述,您有一个循环导入。由于某些原因,您的views.py导入urls.py。没有理由这样做,您应该删除该导入。
答案 1 :(得分:0)
开始项目:
django-admin.py startproject buttonpython
cd buttonpython
启动应用程序:
django-admin.py startapp app_name
settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_name',
]
app_name/views.py
与问题中的views.py
相同。不导入urls.py
。
urls.py:
from app_name import views
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.button, name="home")
]