Reputation: 1802
I have problem with a form if invalid data is submitted. My ErrorView
view uses the app/feriehus_detail.html
template, but I'm not including price_data
in the template context.
This seems to cause a KeyError
when the template tries to use price_data
as an argument in a filter.
I can't figure out how to add it to the template context? Any help will be much appreciated.
I'm using Python 3.5 and Django 1.9.
Traceback:
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>
template:
<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,
'[email protected]' + '',
['[email protected]']
)
email.send()
return super(FeedbackView, self).form_valid(form, **kwargs)
Output of 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'}]
Upvotes: 2
Views: 3097
Reputation: 9359
As the comment above says, I dont know what CreateContext.price_time_serie()
is supposed to do. Until you'll explain that we can only speculate what you are trying to achieve there.
If that is a way how to obtain the primary key for CreateContext
record, then you have to add another parameter to the function, as get_object_or_404()
expected at least two parameters - the first one is the model class you are trying to get, the others are parameters for the SQL query to identify the record to get. So I guess it should be something like this:
def get_context_data(self, *args, **kwargs):
...
context['price_data'] = get_object_or_404(CreateContext, pk=CreateContext.price_time_serie(pk=self.kwargs['pk']))
...
Upvotes: 3