Kesalahan yang saya dapatkan adalah

django.core.exceptions.ImproperlyConfigured: URLconf 'buttonpython.urls' yang disertakan tampaknya tidak memiliki pola apa pun di dalamnya. Jika Anda melihat pola yang valid dalam file, maka masalahnya mungkin disebabkan oleh impor melingkar.

Saya mengikuti tutorial Django yang lebih lama karena hanya itu yang dapat saya temukan dan dapatkan kesalahannya. Berikut adalah 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 (saya menyertakan ini karena saya menemukan variabel buttonpython.urls di dalamnya):

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})

Saya bingung bantuan apa pun akan dihargai.

0
Fr06t 30 Mei 2019, 20:46

2 jawaban

Jawaban Terbaik

Mulai proyek:

django-admin.py startproject buttonpython

cd buttonpython

Mulai aplikasi:

django-admin.py startapp app_name

Pengaturan.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 sama dengan views.py dalam pertanyaan Anda. tanpa impor urls.py.

Url.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")
]
0
katoozi 30 Mei 2019, 18:22

Seperti yang dikatakan kesalahan, Anda memiliki impor melingkar. Untuk beberapa alasan views.py Anda mengimpor urls.py. Tidak ada alasan untuk melakukan itu, Anda harus menghapus impor itu.

1
Daniel Roseman 30 Mei 2019, 17:57