Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
django应用程序中未呈现上下文_Django_Django Forms_Django Views - Fatal编程技术网

django应用程序中未呈现上下文

django应用程序中未呈现上下文,django,django-forms,django-views,Django,Django Forms,Django Views,我开车去渲染一个没有在我的HTML中渲染的上下文比例,我可以设法看到错误。我在inspect/控制台和Atom终端中都没有得到任何错误 我正在开发一个调查应用程序,使用0-100%的比例(使用JavaScript) 但由于某些原因,它没有呈现 这是我的密码: views.py class SurveyDetail(View): def get(self, request, *args, **kwargs): survey = get_object_or_404(Surv

我开车去渲染一个没有在我的HTML中渲染的上下文比例,我可以设法看到错误。我在inspect/控制台和Atom终端中都没有得到任何错误

我正在开发一个调查应用程序,使用0-100%的比例(使用JavaScript) 但由于某些原因,它没有呈现

这是我的密码: views.py

class SurveyDetail(View):

    def get(self, request, *args, **kwargs):
        survey = get_object_or_404(Survey, is_published=True, id=kwargs['id'])
        if survey.template is not None and len(survey.template) > 4:
            template_name = survey.template
        else:
            if survey.display_by_question:
                template_name = 'survey/survey.html'
            else:
                template_name = 'survey/one_page_survey.html'
        if survey.need_logged_user and not request.user.is_authenticated():
            return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
        categories = Category.objects.filter(survey=survey).order_by('order')
        form = ResponseForm(survey=survey, user=request.user,
                            step=kwargs.get('step', 0))
        #try:
        get_scale = form.get_multiple_scale()
        #except:
        #    get_scale = None
        context = {
            'response_form': form,
            'survey': survey,
            'categories': categories,
            'scales': get_scale
        }

        return render(request, template_name, context) 
form.py:

class ResponseForm(models.ModelForm):

    WIDGETS = {
        Question.TEXT: forms.Textarea,
        Question.SHORT_TEXT: forms.TextInput,
        Question.RADIO: forms.RadioSelect,
        Question.SELECT: forms.Select,
        Question.SELECT_IMAGE: ImageSelectWidget,
        Question.SELECT_MULTIPLE: forms.CheckboxSelectMultiple,
        Question.SCALE: forms.TextInput,
    }

    class Meta(object):
        model = Response
        fields = ()

    def __init__(self, *args, **kwargs):
        """ Expects a survey object to be passed in initially """
        self.survey = kwargs.pop('survey')
        self.user = kwargs.pop('user')
        try:
            self.step = int(kwargs.pop('step'))
        except KeyError:
            self.step = None
        super(ResponseForm, self).__init__(*args, **kwargs)
        self.uuid = uuid.uuid4().hex
        self.steps_count = len(self.survey.questions.all())
        # add a field for each survey question, corresponding to the question
        # type as appropriate.
        data = kwargs.get('data')
        for i, question in enumerate(self.survey.questions.all()):
            is_current_step = i != self.step and self.step is not None
            if self.survey.display_by_question and is_current_step:
                continue
            else:
                try:
                    self.scales = question.get_multiple_scales()
                except:
                    self.scales = None
                self.add_question(question, data)

    def get_multiple_scale(self):
        mscale = []
        for items in self.scales:
            index, question = items
            tag = "<p class='tagged'>{}</p>".format(question)
            mscale.append(tag)
        return mscale
类响应窗体(models.ModelForm):
小部件={
问题.TEXT:forms.TEXT区域,
Question.SHORT_TEXT:forms.TextInput,
问题.收音机:forms.RadioSelect,
问题。选择:表格。选择,
问题.选择\u图像:ImageSelectWidget,
Question.SELECT\u MULTIPLE:forms.CheckboxSelectMultiple,
问题.SCALE:forms.TextInput,
}
类元(对象):
模型=响应
字段=()
定义初始化(self,*args,**kwargs):
“”“需要最初传入一个测量对象”“”
self.survey=kwargs.pop('survey'))
self.user=kwargs.pop(“用户”)
尝试:
self.step=int(kwargs.pop('step'))
除KeyError外:
self.step=None
超级(响应窗体,自我)。\uuuuu初始化(*args,**kwargs)
self.uuid=uuid.uuid4().hex
self.steps\u count=len(self.survey.questions.all())
#为每个调查问题添加一个字段,与该问题相对应
#根据需要键入。
data=kwargs.get('data')
对于我,枚举中的问题(self.survey.questions.all()):
_current_step=i!=self.step和self.step不是无
如果self.survey.display\u by\u question并且是当前步骤:
持续
其他:
尝试:
self.scales=question.get_multiple_scales()
除:
self.scales=无
添加问题(问题、数据)
def get_多重刻度(自):
mscale=[]
对于自评量表中的项目:
索引,问题=项目
tag=“

{}

”。格式(问题) mscale.append(标记) 返回mscale
HTML:

{%load bootstrap%}
{%load static%}
{%load i18n%}
{%load OVERRAY_extras%}
{响应中表单的百分比_form%}
{%如果form.field.widget.attrs.category==category.name或不是form.field.widget.attrs.category%}
{{form.label | safe}}
{%if form.field.required%}
{%endif%}
{%for form.errors%}{{error}{%endfor%}{/strong>

{%形式的字段为%}
    {{field}}
{%endfor%} {%如果在form.field.widget.attrs%中“隐藏”}
{比例为%的比例为%} {{scale | safe}} 0% 100%
{%endfor%} {%endif%} {%endif%} {%endfor%}
get
方法中有一个catch-all
子句,除了
子句。这是一个非常糟糕的想法;您正在捕获并隐藏
get\u multiple\u scale
方法中发生的任何错误。可能有什么地方出了问题,但您的代码使您无法判断出问题所在

删除该选项,然后重试/例外


在表单的init方法中有一个类似的方法;在那里,它甚至没有什么意义,因为您最终没有为
self.scales
赋值,而这正是您在
get\u multiple\u scales
中迭代的内容。这里有一个非常奇怪的循环定义,您当然不应该有这个定义。

在控制台上打印(模板名称)并检查输出,这可能是文件问题。需要跟踪和修复。输出是survey/survey.html,在该文件中有一个{%include“survey/question.html”%},其中我帖子中的html与您在此处显示的html相同?是的!相同的。。在我的HTML中,我可以看到其他上下文,如form.label,field only scale not show upHi Daniel,我删除了try/except,但控制台中仍然没有错误符号,scale没有呈现
{% load bootstrap %}
{% load static %}
{% load i18n %}
{% load survey_extras %}

<table class="table">
    <!--<thead>
      <tr>
        <th> Question  </th>
        <th> Answers </th>
      </tr>
    </thead> -->
    <tbody>
{% for form in response_form %}
    {% if form.field.widget.attrs.category == category.name or not form.field.widget.attrs.category %}
        <tr class="{% if form.errors%} danger {% endif %}">
            <td>
              <div class="question-title">
                <h4>{{ form.label|safe }}</h4>
              </div>

                {% if form.field.required %}
                    <span class="glyphicon glyphicon-asterisk" style="color:red"> </span>
                {% endif %}
                <span class="help-inline" style="color:red">
                    <strong> {% for error in  form.errors %}{{ error }}{% endfor %} </strong>
                </span> <br>

              <div class="answers">
                {% for field in form %}
                      <ul>
                        {{ field }}
                      </ul>
                  {% endfor%}
                  {% if "hidden" in form.field.widget.attrs %}
                      <br>
                      {% for scale in scales %}
                          {{ scale|safe }}
                          <div id="rate" class="scale">
                          </div>
                          <div class="scale-title">
                            <div class="container">
                              <div class="row">
                                <div class="col scaleleft">
                                  0%
                                </div>
                                <div class="col scaleright">
                                  100%
                                </div>

                              </div>

                            </div>

                          </div>
                          <br>
                      {% endfor %}
                  {% endif %}
              </div>

            </td>
        </tr>
    {% endif %}
{% endfor %}
    </tbody>
</table>