def stock_entry_add(request): template_name = 'stock_entry_form.html' stock_form=Stock() stock_item_formset = inlineformset_factory( Stock, StockItems, form=StockItemsForm, extra=0, min_num=1, validate_min=True, ) if request.method == 'POST': form = StockForm(request.POST, instance=stock_form, prefix='main') formset = stock_item_formset( request.POST, instance=stock_form, prefix='stock' ) if form.is_valid() and formset.is_valid(): form=form.save() formset.save() url = 'stock:stock_entry_detail' return HttpResponseRedirect(resolve_url(url, form.pk)) else: form = StockForm(instance=stock_form, prefix='main') formset = stock_item_formset(instance=stock_form, prefix='stock') context = {'form': form, 'formset': formset} return render(request, template_name, context)
def collect(): if current_user.is_authenticated: form = StockForm() if form.validate_on_submit(): try: start = datetime.datetime(int(form.start_year.data), int(form.start_month.data), int(form.start_date.data)) end = datetime.datetime(int(form.end_year.data), int(form.end_month.data), int(form.end_date.data)) stock = form.search_stock.data result = web.DataReader(stock, 'yahoo', start, end) result_info.update(result) stocl_info.append(form.search_stock.data) start_year.append(form.start_year.data) start_month.append(form.start_month.data) start_date.append(form.start_date.data) end_year.append(form.end_year.data) end_month.append(form.end_month.data) end_date.append(form.end_date.data) except: flash('Type error!', 'warning') return redirect(url_for('stock.collect')) return render_template('stock_result.html', data=result.to_html()) else: flash('You need login!', 'warning') return redirect(url_for('main.home')) return render_template('stock.html', form=form)
def collect(): if current_user.is_authenticated: form = StockForm() if form.validate_on_submit(): try: start = datetime.datetime(int(form.start_year.data), int(form.start_month.data), int(form.start_date.data)) end = datetime.datetime(int(form.end_year.data), int(form.end_month.data), int(form.end_date.data)) stock = form.search_stock.data result = web.DataReader(stock, 'yahoo', start, end) # df = pd.DataFrame(column.values, columns=['選擇', '股票代號', '時間', '成交', '買進', '賣出', '漲跌', '張數', '昨收', '開盤', '最高', '最低']) session['result'] = result.to_json() except Exception as e: print('error====', e) flash('Type error!', 'warning') return redirect(url_for('stock.collect')) return render_template('stock_result.html', data=result.to_html()) else: flash('You need login!', 'warning') return redirect(url_for('main.home')) return render_template('stock.html', form=form)