EDIT 2: Complete traceback with the line
context = super(ComecandoView, self).get_context_data(**kwargs)
uncommented. If I change this line tocontext = {}
I don't get any errors, but still can't use the user's variable.
- response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\gabriel\PycharmProjects\django-solo-testes\wvevn\lib\site-packages\django\views\generic\base.py" in view 68. return self.dispatch(request, *args, **kwargs)
File "C:\Users\gabriel\PycharmProjects\django-solo-testes\wvevn\lib\site-packages\django\views\generic\base.py" in dispatch 88. return handler(request, *args, **kwargs)
File "C:\Users\gabriel\PycharmProjects\django-solo-testes\wvevn\lib\site-packages\django\views\generic\edit.py" in get 174. return self.render_to_response(self.get_context_data())
File "C:\Users\gabriel\PycharmProjects\django-solo-testes\papaBelini\core\views.py" in get_context_data 107. context = super(ComecandoView, self).get_context_data(**kwargs)
File "C:\Users\gabriel\PycharmProjects\django-solo-testes\wvevn\lib\site-packages\django\views\generic\edit.py" in get_context_data 93. kwargs['form'] = self.get_form()
File "C:\Users\gabriel\PycharmProjects\django-solo-testes\wvevn\lib\site-packages\django\views\generic\edit.py" in get_form 45. return form_class(**self.get_form_kwargs())
Exception Type: TypeError at /comecando Exception Value: 'NoneType' object is not callable
EDIT: I did some changes as asked and updated my post but it still doesn't work. The error I'm getting now is saying that NoneType object is not callable on the line
context = super(ComecandoView, self).get_context_data(**kwargs)
. If I comment this and addcontext {}
the code works fine but it's not running the functionform_valid
, I've tried to print stuff inside that function into the terminal and nothing happens, but the code still doing what it's supposed to after the changes, apart from not retreiving the user's input. I've also tried changing the form method topost
but no luck.
What I want to do: I want to update a graph created in a view according to the user's input (i.e., user types the date range for the graph and the template generate a new graph with updated values).
I have a django view which already creates the graph with pre-defined values.
Check the code below, it works fine.
views.py
class ComecandoView(FormView):
template_name = 'comecando.html'
form = GraphForm
def form_valid(self, form):
# store the user input here. These variables you can access then in your get_context_data method.
print('Hi') # Doesnt get called
self.q=form.cleaned_data['q']
return super(ComecandoView, self).form_valid(form)
def get_context_data(self, **kwargs):
#context = super(ComecandoView, self).get_context_data(**kwargs) # Commented this.
context = {} # ADDED THIS
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.html (comecando.html)
{% extends 'base.html' %}
{% block container %}
<form method="get">
<input type="text" name="q">
<input type="submit" value="Search">
</form>
</div>
{% if graph %}
<div class="row">
<div class="col s12">
{{ graph|safe }}
</div>
</div>
{% endif %}
{% endblock %}
forms.py
from django import forms
class GraphForm(forms.Form):
name = forms.CharField()
message = forms.CharField(widget=forms.Textarea)
def send_email(self):
pass
What I want is for the user to be able to select a value from a combobox for example or type a value into a text field and after he clicks a button the value he typed will be available in my view so that I'll be able to generate a new graph with new values.
What I've tried:
views.py
I added this method inside my class based view but didn't output as expected, I was only able to use the value of the form 'q' inside the get
function and also, the graph wasn't generated as it previously were.
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})
comecando.html (template)
<form method="get">
<input type="text" name="q">
<input type="submit" value="Search">
</form>
There are plenty of topics that are very similar to my problem but I've gone through lots of them and still couldn't solve it, here are a couple of examples that I've tried applying to my problem but didn't work out: