gpt4 book ai didi

python - 如何使用 django 基于类的 View 获取用户输入

转载 作者:行者123 更新时间:2023-12-01 02:48:45 27 4
gpt4 key购买 nike

EDIT 2: Complete traceback with the line context = super(ComecandoView, self).get_context_data(**kwargs) uncommented. If I change this line to context = {} I don't get any errors, but still can't use the user's variable.

  1. 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 add context {} the code works fine but it's not running the function form_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 to post but no luck.

我想要做什么:我想根据用户的输入更新在 View 中创建的图表(即,用户输入图表的日期范围,模板使用以下命令生成一个新图表)更新值)。

我有一个 django View ,它已经使用预定义值创建了图表。

检查下面的代码,它工作正常。

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

我想要的是用户能够从组合框中选择一个值,或者在文本字段中输入一个值,然后在他单击按钮后,他输入的值将在我的 View 中可用,以便我'将能够生成具有新值的新图表。

我尝试过的:

views.py

我在基于类的 View 中添加了此方法,但没有按预期输出,我只能在 get 函数中使用“q”形式的值以及图表没有像以前那样生成。

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(模板)

<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:

最佳答案

您有一个良好的起点,只需完成 form handling正确。

将父 View 从 TemplateView 更改为 FormView 并调整必要的内容:创建一个表单,将其添加到 View 中,调整模板。

然后删除 get 方法并添加以下内容:

def form_valid(self, form):
# store the user input here. These variables you can access then in your get_context_data method.
self.q=form.cleaned_data['q']
return super(ComecandoView, self).form_valid(form)

您需要将模板更改为: {{form.as_table}}

并将获取值的行替换为有意义的内容:“q”必须替换为表单的字段名称(“名称”或“消息”)。

关于python - 如何使用 django 基于类的 View 获取用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45022248/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com