Wessi
Wessi

Reputation: 1802

Django error on invalid form submit (VariableDoesNotExist)

I have a form that works fine with valid data but gives me a strange error with invalid data. I can't figure out what's causing it.

Full traceback:

Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/nyt_hus/9/feedback/

Template error:
In template C:\website\Conference\app\templates\app\nyt_hus_detail.html, error at line 57
   Failed lookup for key [%s] in %r   47 :                         </tr>
   48 :                     </thead>
   49 :                     <tbody>
   50 :                         <tr style="font-weight:bold; font-style:normal;">
   51 :                             <td class="rotated-label fed" rowspan="6">
   52 :                                 <span style="top:75%;">Anbefalinger</span>
   53 :                             </td>
   54 :                             <th>Gennemsnitspris pr. uge</th>
   55 :                             <td class="text-center">{{ nythus.price_rec_avg}} kr</td>
   56 :                             <td class="text-center">{{ nythus.price_comp_avg}} kr</td>
   57 :                             <td class="text-center"> {{ nythus.price_rec_avg|percentage_dif:nythus.price_comp_avg }} </td>
   58 :                         </tr>
   59 :                         <tr>
   60 :                             <th style="font-weight: normal;">Medianpris</th>
   61 :                             <td class="text-center">{{ nythus.price_rec_median }} kr</td>
   62 :                             <td class="text-center">{{ nythus.price_comp_median }} kr</td>
   63 :                             <td class="text-center">{{ nythus.price_rec_median|percentage_dif:nythus.price_comp_median }}</td>
   64 :                         </tr>

Traceback:

File "C:\Anaconda3\lib\site-packages\django\template\base.py" in _resolve_lookup
  883.                     current = current[bit]

File "C:\Anaconda3\lib\site-packages\django\template\context.py" in __getitem__
  77.         raise KeyError(key)

During handling of the above exception ('nythus'), another exception occurred:

File "C:\Anaconda3\lib\site-packages\django\template\base.py" in _resolve_lookup
  889.                         if isinstance(current, BaseContext) and getattr(type(current), bit):

During handling of the above exception (type object 'RequestContext' has no attribute 'nythus'), another exception occurred:

File "C:\Anaconda3\lib\site-packages\django\template\base.py" in _resolve_lookup
  898.                             current = current[int(bit)]

During handling of the above exception (invalid literal for int() with base 10: 'nythus'), another exception occurred:

File "C:\Anaconda3\lib\site-packages\django\core\handlers\base.py" in get_response
  174.                     response = self.process_exception_by_middleware(e, request)

File "C:\Anaconda3\lib\site-packages\django\core\handlers\base.py" in get_response
  172.                     response = response.render()

File "C:\Anaconda3\lib\site-packages\django\template\response.py" in render
  160.             self.content = self.rendered_content

File "C:\Anaconda3\lib\site-packages\django\template\response.py" in rendered_content
  137.         content = template.render(context, self._request)

File "C:\Anaconda3\lib\site-packages\django\template\backends\django.py" in render
  95.             return self.template.render(context)

File "C:\Anaconda3\lib\site-packages\django\template\base.py" in render
  206.                     return self._render(context)

File "C:\Anaconda3\lib\site-packages\django\template\base.py" in _render
  197.         return self.nodelist.render(context)

File "C:\Anaconda3\lib\site-packages\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Anaconda3\lib\site-packages\django\template\loader_tags.py" in render
  173.         return compiled_parent._render(context)

File "C:\Anaconda3\lib\site-packages\django\template\base.py" in _render
  197.         return self.nodelist.render(context)

File "C:\Anaconda3\lib\site-packages\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Anaconda3\lib\site-packages\django\template\loader_tags.py" in render
  69.                 result = block.nodelist.render(context)

File "C:\Anaconda3\lib\site-packages\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Anaconda3\lib\site-packages\django\template\base.py" in render
  1043.             output = self.filter_expression.resolve(context)

File "C:\Anaconda3\lib\site-packages\django\template\base.py" in resolve
  730.                     arg_vals.append(arg.resolve(context))

File "C:\Anaconda3\lib\site-packages\django\template\base.py" in resolve
  850.             value = self._resolve_lookup(context)

File "C:\Anaconda3\lib\site-packages\django\template\base.py" in _resolve_lookup
  905.                                                        (bit, current))  # missing attribute

Exception Type: VariableDoesNotExist at /nyt_hus/9/feedback/
Exception Value: Failed lookup for key [nythus] in "[{'None': None, 'True': True, 'False': False}, {}, {}, {'form': <ErrorForm bound=True, valid=False, fields=(content)>, 'view': <app.views.FeedbackView object at 0x00000238D00E5390>}]"

template:

<form role="form" action="{% url 'feedback' pk=nythus.id %}" method="post">
    {% csrf_token %}
    {{ form.content }}
    <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'^nyt_hus/(?P<pk>\d+)/$', views.NytHusDetail.as_view(), name='nyt_hus_detail'),
url(r'^nyt_hus/(?P<pk>\d+)/feedback/$', views.FeedbackView.as_view(), name='feedback'),

views.py:

class NytHusDetail(DetailView, FormMixin):
    model = NytHus
    template_name = "app/nyt_hus_detail.html"
    form_class = ErrorForm

class FeedbackView(FormView):
    form_class = ErrorForm
    template_name = 'app/nyt_hus_detail.html'

    def get_success_url(self, **kwargs):
        return reverse_lazy('nyt_hus_detail', kwargs={'pk': self.kwargs['pk']})

    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)

I'm using Django 1.9 and Python 3.5.

Upvotes: 1

Views: 661

Answers (1)

Alasdair
Alasdair

Reputation: 308829

Your FeedbackView view uses the app/nyt_hus_detail.html template, but you are not including nythus in the template context.

This causes a KeyError when the template tries to use nythus as an argument in a filter, for example the line:

<td class="text-center"> {{ nythus.price_rec_avg|percentage_dif:nythus.price_comp_avg }} </td>

You can add nythus to the template context by overriding get_object_or_404.

from django.shortcuts import get_object_or_404

class FeedbackView(FormView):
    ...
    def get_context_data(self, **kwargs):
        context = super(FeedbackView, self).get_context_data(**kwargs)
        context['nythus'] = get_object_or_404(NytHus, pk=self.kwargs['pk'])
        return context

Upvotes: 1

Related Questions