Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 74158

Having problem on 'KeyError at /new_search 0' on Django

$
0
0

I got stuck when I was building a django project which is a clone of craigslist. I made a search bar and Search model and added it to views. But when ever I tried to search anything in my website, it throws me a error called "KeyError at /new_search 0" Other details are...

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']

Here is my 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)

Here is my new_search.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 %}

What should I do now?


Viewing all articles
Browse latest Browse all 74158

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>