changing form to search box

徘徊边缘 提交于 2020-01-25 01:01:07

问题


Here is my code for form where a user insert stock symbol and the result of the input is shown in the new page. What I did so far:

in models.py

from django.db import models
from django.contrib import auth


class Child(models.Model):
    name = models.CharField(max_length=150, blank=True)

in forms.py

from django import forms
from .models import Child

class ChildlForm(forms.ModelForm):
    class Meta:
        model = Child
        fields = ('name',)

In views.py

from django.shortcuts import render
from .forms import ChildForm
from pandas_datareader import data as wb
from yahoofinancials import YahooFinancials

# Create your views here.


def home(request):
    form = ChildForm()
    if request.method == "POST":
        form = ChildForm(request.POST)
        if form.is_valid():
            data = form.save(commit=True)
            name = data.name

            symbols = [name]
            yahoo_financials = YahooFinancials(symbols)
            new_data = pd.DataFrame()
            for s in symbols :
                new_data[s] = wb.DataReader(s, data_source ='yahoo', start = '2014-1-1')['Adj Close']

            a = new_data[s]
            b = a[-1]


            context={
            'name':name,
            'b':b,}

        else:
            form = ChildForm()
            return render(request,'main/index.html',{'form':form})
        return render(request,'main/index2.html',context)


    return render(request,'main/index.html',{'form':form})

the index.html file

<form method="POST">
    {{ form }}
    {% csrf_token %}
    <input class="form-control mr-sm-2" type="text">
    <button type="submit">OK</button>
  </form>

I realised that the form method has limitation and puts name in front of input box which looks ugly.

I tried to build serach box in index2.html file with search box but it does not work:

<nav class="navbar navbar-default">
        <div class="container-fluid">
            <form id="searchbox" method="get" autocomplete="off" class="navbar-form navbar-left" role="search">
                <input name='q' type="text" type="text" class="form-control" placeholder="Search" />
                <button id="button-submit" type="submit" value=" " class="btn btn-default" />
            </form>
            </div>
    </nav>

I am confused how i should connect the form with the search box and should i even connect. What should i in this case do so the data from search box is converted to index and used in the next page. I also want in the next page to keep search box so a user do not have to return to the previous page to look for new symbols.

Would appreciate any help an advise.

来源:https://stackoverflow.com/questions/58813945/changing-form-to-search-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!