장고 잔고

김찬울·2021년 10월 18일
0

오늘 할 것! 장고로 통장 잔고 받아오기 (라임 최고다.)

로컬

pip install mysqlclient

mysql설치

python manage.py startapp '파일명'

'파일명'으로 앱을 실행시키는데 앱이 없으면 새롭게 폴더를 만든다.

안의 설치되는 것은

  • init.py: python package
  • wsgi.py: special type of web server
  • settings.py: overall settings for the entire website
  • urls.py: table of contents for the website : look at the url and perform the functionality
    이 있다.

settings에서 ('파일'명의 상위 폴더의 config부분) app부분을 "'파일명'/apps/파일명Config" 를 추가

파일안에 들어가서 mkdir로 urls.py추가

from django.urls import path
from stock import views


app_name = 'stock'

urlpatterns = [
    path('', views.main_view, name='main'),
    
]

request받을때 가끔 header가 없어도 괜찮을 때가 있다.

views부분을 바꿔야하는데 책에서 제공되는 코드 사용

templates를 만들고 또 제공되는 html 사용

{% load static %}
<html>
    <head>
        <title>Balance: {{ total }}</title>  <!-- ① -->
        <link rel="stylesheet" href="{% static 'balance/b_style.css' %}"/>
    </head>
    <body>
         <table>
            <tr>
                <th>종목명</th>
                <th>종목코드</th>
                <th>현재가</th>
                <th>주식수</th>
                <th>등락률</th>
                <th>평가금액</th>
            </tr>
            {% for row in rows %}  <!--temlplete코드 -->
            <tr>
                {% for x in row %}
                <td>{{ x }}</td>  <!-- ② -->
                {% endfor %}
            </tr>
            {% endfor %}
            <tr>
                <th colspan=3>계좌 잔고</th>
                <th colspan=3>{{ total }}</th>  <!-- ③ -->
            </tr>
        </table>
    </body>
</html>

여기서 mysite의 url이 잡고 stock url로 보내고 ?매개변수를 전달하면 url과 변수를 view로 전달
main_view에서 request로 받고 GET방식으로 url 받아옴
매개변수를 for문으로 하나씩 받아오면서 get_data (크롤링)해온다.
이를 row (출력) 부분에 집어넣고 총 가격을 구하기위해 total에 받아온다.

이를 또 balance에(html) 전달한다. 이때 키값만 전달되는데 이를 바탕으로
html이 구동된다.
후에 push한다.

putty에서도 이제 실행하면 된다.
mysite가상환경에서
lxml과 bs4를 설치하고
pip install bs4
pip install lxml

git pull로 push한 내용을 받아오면 이제 새롭게 만든 파일명을 받아온다.

받아오고 (work/repos에서)
python manage.py runserver 0:8000 으로 현재 ip의 8000번으로 서버를 실행할 수 있다.

profile
코린코린이

0개의 댓글