Возникла проблема с 'KeyError at / new_search 0' на Django - PullRequest
1 голос
/ 18 января 2020

Я застрял, когда строил проект django, который является клоном Craigslist. Я сделал панель поиска и модель поиска и добавил ее в представления. Но когда я пытался что-то искать на моем веб-сайте, он выдавал ошибку «KeyError at / new_search 0». Другие подробности ...

Request Method: POST
Request URL:    http://127.0.0.1:8000/new_search
Django Version: 2.2.7
Exception Type: KeyError
Exception Value:    
0
Exception Location: C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\bs4\element.py in __getitem__, line 1321
Python Executable:  C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\python.exe
Python Version: 3.7.1
Python Path:    
["D:\\Users\\Admin\\Desktop\\Mugdho's List\\mugdhos_list",
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python37-32\\lib',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python37-32',
 'C:\\Users\\Admin\\AppData\\Roaming\\Python\\Python37\\site-packages',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages']

Вот мой views.py

import requests
from requests.compat import quote_plus
from django.shortcuts import render
from bs4 import BeautifulSoup
from . import models

BASE_CRAIGSLIST_URL = 'https://losangeles.craigslist.org/search/?query={}'

# Create your views here.
def home(request):
    return render(request, 'base.html')

def new_search(request):
    search = request.POST.get('search')
    models.Search.objects.create(search = search)
    final_url = BASE_CRAIGSLIST_URL.format(quote_plus(search))
    response = requests.get(final_url)
    data = response.text
    soup = BeautifulSoup(data, features='html.parser')
    post_listings = soup.find_all('li',{'class': 'result-row'})

    final_postings = []
    for post in post_listings:
        post_title = post[0].find(class_='result-title').text
        post_url = post[0].find('a').get('href')
        post_price = post[0].find(class_='result-price').text

        final_postings.append((post_title, post_url, post_price))

    stuff_for_frontend = {
        'search' : search,
        'final_postings' : final_postings
        }
    return render(request, 'my_app/new_search.html', stuff_for_frontend)

Вот мой новый_поиск. html

{% extends "base.html" %}
{% block content %}
<h3 class="center">{{ search | title }}</h3>

{% for post in final_postings %}
    <p>{{ post.0 }}</p>
    <p>{{ post.1 }}</p>
    <p>{{ post.2 }}</p>
{% endfor %}

<div class="row">
    <div class="col s4">
        <div class="card">
            <div class="card-image">
                <a href="/"><img src=""></a>
            </div>
            <div class="card-content">
                <p>TEST</p>
            </div>
            <div class="card-action">
                <a href="/">View listing: Price TEST PRICE</a>
            </div>
        </div>
    </div>

    <div class="col s4">
        <div class="card">
            <div class="card-image">
                <a href="/"><img src=""></a>
            </div>
            <div class="card-content">
                <p>TEST</p>
            </div>
            <div class="card-action">
                <a href="/">View listing: Price TEST PRICE</a>
            </div>
        </div>
    </div>

    <div class="col s4">
        <div class="card">
            <div class="card-image">
                <a href="/"><img src=""></a>
            </div>
            <div class="card-content">
                <p>TEST</p>
            </div>
            <div class="card-action">
                <a href="/">View listing: Price TEST PRICE</a>
            </div>
        </div>
    </div>

    <div class="col s4">
        <div class="card">
            <div class="card-image">
                <a href="/"><img src=""></a>
            </div>
            <div class="card-content">
                <p>TEST</p>
            </div>
            <div class="card-action">
                <a href="/">View listing: Price TEST PRICE</a>
            </div>
        </div>
    </div>
</div>
{% endblock %}

Что мне теперь делать?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...