If you need more information about the problem (urls.py, complete template code, etc.) just ask that I post, I just did not put it to not pollute the post a lot.
Problem overview: Refresh the chart created in a view according to user input.
I have a django View that creates a chart with values that I set in the view.
Follow her current code.
views.py
class ComecandoView(TemplateView):
def get_context_data(self, **kwargs):
context = super(ComecandoView, self).get_context_data(**kwargs)
lista_precos = []
lista_datas = []
for variacao in range(10500):
lista_precos.append(rjson['dataset']['data'][variacao][4])
lista_datas.append(rjson['dataset']['data'][variacao][0])
# Create a trace
trace = go.Scatter(
y = lista_precos,
x = lista_datas
)
data = [trace]
fig = go.Figure(data=data)
div = opy.plot(fig, auto_open=False, output_type='div')
context['graph'] = div
return context
template_name = 'comecando.html'
What I want is for the user to be able to select (a value from a combobox for example) or to enter in a text field a value, which will be returned to the view of that URL and there a new graphic is generated with that new value and updated template.
Functional example: A quote chart from 1980 until 2017 is generated by the view by default. The user types a value in a textfield on the page and the page is reloaded, passing that value to the view and it generates the graph again with dates from 2000 through 2017 for example.
What I've already tried:
views.py
def get(self, request, *args, **kwargs):
q = request.GET.get('q')
error = ''
if not q:
error = "error message"
return render(request, self.template_name, {'error': error})
starting.html (template)
<form method="get">
<input type="text" name="q">
<input type="submit" value="Search">
</form>
This way I can access the value of input
within the function get
, until I was able to print it in the terminal, but the get_context_data
graph is not generated.