简体   繁体   English

提交无效表单时的 Django 错误(VariableDoesNotExist)

[英]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:表格.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:网址.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:视图.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,
        'from@email.com' + '',
        ['to@email.com']
        )
        email.send()
        return super(FeedbackView, self).form_valid(form, **kwargs)

I'm using Django 1.9 and Python 3.5.我正在使用 Django 1.9 和 Python 3.5。

Your FeedbackView view uses the app/nyt_hus_detail.html template, but you are not including nythus in the template context.您的FeedbackView视图使用app/nyt_hus_detail.html模板,但您没有在模板上下文中包含nythus

This causes a KeyError when the template tries to use nythus as an argument in a filter, for example the line:这会导致一个KeyError当模板试图使用nythus如在过滤器的参数,例如线:

<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 .您可以通过覆盖get_object_or_404nythus添加到模板上下文中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM