1
If you need more information about the problem (urls.py, complete code of the template, etc.) just ask me to post, just not to pollute the post too much.
General description of the problem: Update the chart created in a view according to the input provided by the user.
I have a Django View that creates a graph with values I defined in the view.
Follow her current code.
py views.
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 of a combobox for example) or enter in some text field a value, which will be returned to the view of that URL and there a new graph is generated with this new value and the updated template.
Working example: A quotation chart from 1980 to 2017 is generated by the view by default. The user enters a value in a textfield on the page and the page is reloaded, passing this value to the view and the same generates the graph again with the dates 2000 until 2017 for example.
What I’ve already tried:
py views.
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>
That way I can access the value of input
within the function get
, I even managed to print it on the terminal, but the graph of the get_context_data
is not generated.
Have you ever tried to capture this value within the
get_context_data
?– mazulo
@Not Zulo, I’ll try here
– Gabriel Belini
@But I tried, it didn’t work not, the chart was even generated now, but the variable
q
is only available within the get function and I need to use it to generate the new chart.– Gabriel Belini
Maybe a test would be to call the
get_context_data
within theget
, add the new values and then render. It would be something like:context = self.get_context_data()
context['q'] = request.GET.get('q')
– mazulo