如果提交了无效数据,我的表单有问题。我的ErrorView
视图使用app/feriehus_detail.html
模板,但我未在模板上下文中包含price_data
。
当模板尝试在过滤器中使用KeyError
作为参数时,这似乎会导致price_data
。
我无法弄清楚如何将其添加到模板上下文中?任何帮助都感激不尽。
我使用的是Python 3.5和Django 1.9。
回溯:
Template error:
In template C:\Conference\app\templates\app\feriehus_detail.html, error at line 221
Failed lookup for key [%s] in %r 211 : <td class="text-center">{{ object.price_rec_F }} kr</td>
213 : <td class="text-center">{{ object.price_rec_F|percentage_dif:object.price_cur_F }}</td>
214 : </tr>
215 : </tbody>
216 : </table>
217 : </div>
218 : <div class="col-xs-12" style="padding-bottom:25px;">
219 : <div class="grid">
220 : <div class="col-1-1">
221 : {% column_chart price_data with height='500px' %}
222 : </div>
223 : </div>
224 : </div>
模板:
<form role="form" action="{% url 'error' pk=feriehus.id %}" method="post">
{% csrf_token %}
{{ form|crispy }}
<button type="submit">Send</button>
</form>
forms.py:
class ErrorForm(forms.Form):
content = forms.CharField(
required=True,
widget=forms.Textarea
)
def __init__(self, *args, **kwargs):
super(ErrorForm, self).__init__(*args, **kwargs)
self.fields['content'].label = "Beskriv fejl"
self.helper = FormHelper()
urls.py:
url(r'^feriehus/(?P<pk>[0-9]+)/$', views.FeriehusDetail.as_view(), name='feriehus_detail'),
url(r'^feriehus/(?P<pk>[0-9]+)/error/$', views.ErrorView.as_view(), name='error'),
views.py:
class FeriehusDetail(DetailView, FormMixin):
model = Feriehus
form_class = ErrorForm
def get_context_data(self, **kwargs):
context = super(FeriehusDetail, self).get_context_data(**kwargs)
context['price_data'] = CreateContext.price_time_serie(pk=self.kwargs['pk'])
return context
class ErrorView(FormView):
form_class = ErrorForm
template_name = 'app/feriehus_detail.html'
def get_success_url(self, **kwargs):
return reverse_lazy('feriehus_detail', kwargs={'pk': self.kwargs['pk']})
def get_context_data(self, **kwargs):
context = super(ErrorView, self).get_context_data(**kwargs)
context['object'] = get_object_or_404(Feriehus, pk=self.kwargs['pk'])
context['feriehus'] = get_object_or_404(Feriehus, pk=self.kwargs['pk'])
#context['price_data'] = get_object_or_404(CreateContext.price_time_serie(pk=self.kwargs['pk']))
return context
def form_valid(self, form, **kwargs):
form_content = form.cleaned_data['content']
template = get_template('error_template.txt')
context = Context({
'form_content': form_content
})
content = template.render(context)
email = EmailMessage(
'mail',
content,
'from@email.com' + '',
['to@email.com']
)
email.send()
return super(FeedbackView, self).form_valid(form, **kwargs)
CreateContext.price_time_serie(pk=self.kwargs['pk'])
的输出:
[{'data': [('Week 49', 654645), ('Week 01', 554645)], 'name': 'Recommended price'}, {'data': [('Week 49', 3398), ('Week 01', 3398)], 'name': 'Current price'}]
答案 0 :(得分:1)
正如上面的评论所说,我不知道CreateContext.price_time_serie()
应该做什么。直到你解释我们只能推测你想要在那里实现的目标。
如果这是获取CreateContext
记录主键的一种方法,那么你必须在函数中添加另一个参数,因为get_object_or_404()
预期至少有两个参数 - 第一个是您正尝试获取的模型类,其他是SQL查询的参数,用于标识要获取的记录。所以我想它应该是这样的:
def get_context_data(self, *args, **kwargs):
...
context['price_data'] = get_object_or_404(CreateContext, pk=CreateContext.price_time_serie(pk=self.kwargs['pk']))
...