Django:向模板contex添加变量

2024-10-03 09:07:43 发布

您现在位置:Python中文网/ 问答频道 /正文

如果提交的数据无效,我对表格有问题。我的ErrorView视图使用app/feriehus_detail.html模板,但我没有在模板上下文中包括price_data。在

当模板试图使用price_data作为筛选器中的参数时,这似乎会导致KeyError。在

我不知道如何将它添加到模板上下文中?任何帮助都将不胜感激。在

我使用的是python3.5和django1.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>

模板:

^{pr2}$

在表单.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()

在网址.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'),

在视图.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'}]

Tags: selfdivformdatagetobjectcontexttemplate
1条回答
网友
1楼 · 发布于 2024-10-03 09:07:43

正如上面的评论所说,我不知道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']))
    ...

相关问题 更多 >