Home >  > 股票网站

股票网站

0

一.安装django
直接看这里

在settings.py中的apps中添加刚刚建立的app

二.建立一个数据库,名称为stock

三、修改settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'GP'
]
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME':  'stock',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}

这里还需要pip install mysqlclient。

四、数据表

from django.db import models

# Create your models here.

class company_basic(models.Model):
    ts_code = models.CharField(max_length=100,null=True)
    chairman = models.CharField(max_length=100,null=True)
    manager = models.CharField(max_length=100,null=True)
    secretary = models.CharField(max_length=100,null=True)
    reg_capital = models.CharField(max_length=100,null=True)
    setup_date = models.CharField(max_length=100,null=True)
    province = models.CharField(max_length=100,null=True)
    def __str__(self):
        return self.ts_code

class shareholder_basic(models.Model):
    ts_code = models.CharField(max_length=100,null=True)
    ann_date = models.CharField(max_length=100,null=True)
    end_date = models.CharField(max_length=100,null=True)
    holder_name = models.CharField(max_length=100,null=True)
    hold_amount = models.CharField(max_length=100,null=True)
    hold_ratio = models.CharField(max_length=100,null=True)
    def __str__(self):
        return self.ts_code

class shareholder1_basic(models.Model):
    ts_code = models.CharField(max_length=100,null=True)
    ann_date = models.CharField(max_length=100,null=True)
    end_date = models.CharField(max_length=100,null=True)
    holder_name = models.CharField(max_length=100,null=True)
    hold_amount = models.CharField(max_length=100,null=True)
    def __str__(self):
        return self.ts_code
class K_data(models.Model):
    ts_code = models.CharField(max_length=100, null=True)
    trade_date=models.CharField(max_length=100,null=True)
    open = models.FloatField(max_length=100,null=True)
    high = models.FloatField(max_length=100,null=True)
    close = models.FloatField(max_length=100,null=True)
    low= models.FloatField(max_length=100,null=True)
    vol = models.FloatField(max_length=100,null=True)
    def __str__(self):
        return self.ts_code

数据库同步

python manage.py makemigrations GP
python manage.py migrate

五、数据入库

from sqlalchemy import create_engine
import tushare as ts
import pandas as pd

cn = create_engine('mysql+pymysql://root@localhost:3306/stock?charset=utf8')
pro = ts.pro_api('your token')    ####详细查看自己tushare的个人页面
company = pro.stock_company(exchange='SZSE', fields='ts_code,chairman,manager,secretary,reg_capital,setup_date,province')
shareholder = pro.query('top10_holders', ts_code='600000.SH', start_date='20190101', end_date='20190501')
shareholder1 = pro.top10_floatholders(ts_code='600000.SH', start_date='20190101', end_date='20190601')
Ddata = pro.daily(ts_code='000001.SZ', start_date='20190101', end_date='20190601', fields='ts_code,trade_date,open,high,low,close,vol')
company.to_sql('gp_company_basic',cn,index=False,if_exists='append')
shareholder.to_sql('gp_shareholder_basic',cn,index=False,if_exists='append')
shareholder1.to_sql('gp_shareholder1_basic',cn,index=False,if_exists='append')
Ddata.to_sql('gp_k_data',cn,index=False,if_exists='append')

五、前端展示
1.url
项目的url

from django.urls import path,include


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('GP.urls')),
]

App的URL

from django.conf.urls import url,include
from django.contrib import admin
from GP import views
from django.urls import path
from django.views.static import serve
from . import views


urlpatterns = [
    path('', views.index, name='index'),
    path('index/', views.index, name='index'),
    path('company_basic/', views.company_basic, name='company_basic'),
    path('shareholder_basic/', views.shareholder_basic, name='shareholder_basic'),
    path('shareholder1_basic/', views.shareholder1_basic, name='shareholder1_basic'),
    path('K_data/', views.K_data, name='K_data'),
]

2.view

from django.shortcuts import render

# Create your views here.

from GP import models
from .models import company_basic
from .models import K_data


def index(request):
    return render(request, 'stock/index.html')

def company_basic(request):
    com_list = models.company_basic.objects.all()
    return render(request,'stock/company_basic.html',{'com_list':com_list})

def shareholder_basic(request):
    sha_list = models.shareholder_basic.objects.all()
    return render(request,'stock/shareholder_basic.html',{'sha_list':sha_list})

def shareholder1_basic(request):
    sha1_list = models.shareholder1_basic.objects.all()
    return render(request,'stock/shareholder1_basic.html',{'sha1_list':sha1_list})

def K_data(request):
    k_d = models.K_data.objects.all()
    return render(request,'stock/K_data.html',{'myChart':k_d})

3.添加模板
在stock\shares\GP\template\stock\下面添加模板文件。

开始老是找不到模板,后来在settings.py中添加下面一行代码:

DIRS': [os.path.join(BASE_DIR, 'GP/template/')]

终于可以了。

六、成果展示:

参考:https://blog.csdn.net/weixin_43832703/article/details/91559942

暧昧帖

本文暂无标签

发表评论

*

*