HTML页面中的无效变量

问题描述 投票:0回答:1

我想为调查设计特定的布局:在左侧的问题,在右侧的5个“选择”的可能选择(它们都已标准化,因此范围从“我强烈同意”到“我强烈不同意”)。这是我的代码的一部分

Models.py:

from otree.api import (
    models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer,
    Currency as c, currency_range
)

import random


def make_field_5point(label):
    return models.IntegerField(
        choices=[
            [1, ''],
            [2, ''],
            [3, ''],
            [4, ''],
            [5, '']],
        label=label,
        widget=widgets.RadioSelect,
        blank=False
    )


def make_field_5point_reversed(label):
    return models.IntegerField(
        choices=[
            [5, ''],
            [4, ''],
            [3, ''],
            [2, ''],
            [1, '']],
        label=label,
        widget=widgets.RadioSelect,
        blank=False
    )



class Constants(BaseConstants):
    name_in_url = 'survey'
    players_per_group = None
    num_rounds = 1

class Player(BasePlayer):

    Con1 = make_field_5point('works in-depth')

    Ext1 = make_field_5point('is communicative, sociable')

问题在Class Player(BasePlayer)中

Pages.py:

from django import forms
import random
from random import shuffle
from ._builtin import Page, WaitPage
from .models import Constants

class StartSurvey(Page):
    pass

class Personality(Page):
    form_model = 'player'
    form_fields = ['Con1',
                   'Ext1']

    def get_form_fields(self):
        fields = self.form_fields
        random.shuffle(fields)

class End(Page):
    pass
        return fields

page_sequence = [
    StartSurvey,
    Personality,
    End,]

HTML

{% extends "global/Page.html" %} {% load otree static %} {% block title %} {% endblock %} {% block content %}

Please select your stance:
{% for field in form %} {% for choice in field %} {% endfor %} {% endfor %}
                        I strongly disagree     I disagree  Neither agree nor disagree  I agree     I strongly agree

{{ field.label }}               {{ choice }}
{% next_button %} {% endblock %}

如果我想参加调查,则会收到以下错误消息:无效的变量:field.label。错误出现在第7行,带有{{field.labe}}和{{choice}}

但是我不明白为什么,因为我在函数中定义了它们...

追踪:

Traceback:

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\base.py" in _resolve_lookup
  829.                     current = current[bit]

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\context.py" in __getitem__
  83.         raise KeyError(key)

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

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\base.py" in _resolve_lookup
  835.                         if isinstance(current, BaseContext) and getattr(type(current), bit):

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

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\base.py" in _resolve_lookup
  843.                             current = current[int(bit)]

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

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\base.py" in resolve
  671.                 obj = self.var.resolve(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\base.py" in resolve
  796.             value = self._resolve_lookup(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\base.py" in _resolve_lookup
  850.                                                        (bit, current))  # missing attribute

During handling of the above exception (Failed lookup for key [field] in [{'True': True, 'False': False, 'None': None}, {}, {}, {'form': <PlayerForm bound=False, valid=False, fields=(Agr2;Ext3;Ope1;Neu3_rev;Agr3;Con1;Con2_rev;Ext2_rev;Ope3;Ext1;Neu2;Con3;Agr1_rev;Ope2;Neu1)>, 'view': <survey.pages.Personality object at 0x000001FC923D1978>, 'object': <Player  1>, 'player': <Player  1>, 'group': <Group pk=173>, 'subsession': <Subsession pk=173>, 'session': <Session pk=650>, 'participant': <Participant pk=1675>, 'Constants': <class 'survey.models.Constants'>, 'timer_text': 'Time left to complete this page:', 'javascript_vars': dict_items([])}]), another exception occurred:

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\otree\views\abstract.py" in dispatch
  284.                     response.render()

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\response.py" in render
  106.             self.content = self.rendered_content

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\response.py" in rendered_content
  83.         content = template.render(context, self._request)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\backends\django.py" in render
  61.             return self.template.render(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\base.py" in render
  171.                     return self._render(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\base.py" in _render
  163.         return self.nodelist.render(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\base.py" in render
  937.                 bit = node.render_annotated(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\otree\strict_templates.py" in render_annotated
  95.             return self.render(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\loader_tags.py" in render
  150.             return compiled_parent._render(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\base.py" in _render
  163.         return self.nodelist.render(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\base.py" in render
  937.                 bit = node.render_annotated(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\otree\strict_templates.py" in render_annotated
  95.             return self.render(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\loader_tags.py" in render
  150.             return compiled_parent._render(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\base.py" in _render
  163.         return self.nodelist.render(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\base.py" in render
  937.                 bit = node.render_annotated(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\otree\strict_templates.py" in render_annotated
  95.             return self.render(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\loader_tags.py" in render
  150.             return compiled_parent._render(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\base.py" in _render
  163.         return self.nodelist.render(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\base.py" in render
  937.                 bit = node.render_annotated(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\otree\strict_templates.py" in render_annotated
  95.             return self.render(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\loader_tags.py" in render
  62.                 result = block.nodelist.render(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\base.py" in render
  937.                 bit = node.render_annotated(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\otree\strict_templates.py" in render_annotated
  95.             return self.render(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\loader_tags.py" in render
  62.                 result = block.nodelist.render(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\base.py" in render
  937.                 bit = node.render_annotated(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\otree\strict_templates.py" in render_annotated
  95.             return self.render(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\base.py" in render
  987.             output = self.filter_expression.resolve(context)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\otree\strict_templates.py" in resolve
  40.             return original_resolve(self, context, ignore_failures=False)

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\django\template\base.py" in resolve
  679.                             return string_if_invalid % self.var

File "c:\users\wiwi-admin\appdata\local\programs\python\python37\lib\site-packages\otree_startup\settings.py" in __mod__
  219.         raise InvalidVariableError(msg) from None

Exception Type: InvalidVariableError at /p/8rgqwbh3/survey/Personality/2/
Exception Value: Invalid variable: field.label
html django error-handling html-helper survey
1个回答
0
投票

您正在field块外部使用模板变量{% for field in form %}

使用类似以下内容修改HTML模板:


{% extends "global/Page.html" %} 

{% load otree static %} 
{% block title %} {% endblock %} 

{% block content %}

Please select your stance:
{% for field in form %} 
   {% for choice in field %} 

        {{ field.label }}       <<<<<<<<<<< The 'field' variable is define here


   {% endfor %} 
{% endfor %}

{% next_button %} {% endblock %}

© www.soinside.com 2019 - 2024. All rights reserved.