How to store data in DB

Asked 2 years ago, Updated 2 years ago, 45 views

# model.py
from django.db import models

# # Create your models here.

class Weather(models.Model):
    Temperature = models.CharField(max_length=20)
    Humidity = models.CharField(max_length=20)
    Check_Time = models.CharField(max_length=20)

    def __str__(self):
        return self.name
# views.py
from django.shortcuts import render
from django.http import HttpResponse
import requests
from bs4 import BeautifulSoup
import time
from .models import Weather

# # Create your views here.

def index(request):

    req = requests.get('https://search.naver.com/search.naver?sm=tab_hty.top&where=nexearch&query=%EC%84%B1%EB%82%A8%EC%8B%9C+%EC%88%98%EC%A0%95%EA%B5%AC+%EC%8B%A0%ED%9D%A5%EB%8F%99+%EB%82%A0%EC%94%A8&oquery=%EC%84%B1%EB%82%A8%EC%8B%9C+%EC%88%98%EC%A0%95%EA%B5%AC+%EB%82%A0%EC%94%A8&tqi=UC6hpdprvhGssb2o%2B3Vssssssrl-217403')

    html = req.text

    weather = BeautifulSoup(html, 'html.parser')

    bs1 = weather.find('div' , class_ = 'main_info')
    bs2 = bs1.find('div' , class_ = 'info_data')
    bs3 = bs2.find('p' , class_ = 'info_temperature')
    bs4 = bs3.find('span' , class_ = 'todaytemp')

    temp = bs4.text

    bs1 = weather.find('div' , class_ = 'info_list humidity _tabContent')
    bs2 = bs1.find('ul' , class_ = 'list_area')
    bs3 = bs2.find('li' , class_ = 'on now')
    bs4 = bs3.find('dd' , class_ = 'weather_item _dotWrapper')
    bs5 = bs4.find('span')

    humi = bs5.text

    a = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time()))

    Weather = {'Temperature':temp, 'Humidity':humi, 'time':a}

    print("Temperature: %s°C & Humidity: %s%%" %(temp, humi))

    # # b = Weather(Temperature = temp, Humidity = humi, Check_Time = a)

    # # b.create()

    # # dic = {Weather.Temperature : temp, Weather.Humidity : humi, Weather.Check_Time : a}

    # # dic.save()

    # # q = Weather(Temperature=temp, Humidity=humi, Check_Time=a)

    # # q.save()


    return render(request, 'Weather_data/index.html')

def update(request):
    return HttpResponse('Update Page :)')

I want to save the crawled data directly to DB when accessing the website.

I'm asking you this question because I keep getting errors in the DB storage section.

# b = Weather(Temperature = temp, Humidity = humi, Check_Time = a)

# # b.create()

The 'dict' object is not callable error comes out

# dic = {Weather.Temperature : temp, Weather.Humidity : humi, Weather.Check_Time : a}

# # dic.save()

'dict' object has no attribute 'Temperature' error.

#q = Weather(Temperature=temp, Humidity=humi, Check_Time=a)

#q.save()

The error 'dict' object is not callable appears

What should I do?

https://docs.djangoproject.com/en/3.0/topics/db/queries/#creating-objects

I've referred to this page

django python database

2022-09-21 10:01

1 Answers

from .models import Weather
Weather = {'Temperature':temp, 'Humidity':humi, 'time':a}

I think these two are in conflict with each other. Let's not be fooled and try to rename the second dictionary to something like w.


2022-09-21 10:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.