上下文不在django App中呈现

时间:2017-12-14 07:17:00

标签: django django-forms django-views

我正在开车渲染一个不在我的HTML中呈现的上下文比例,我可以设法看到错误。我在检查/控制台中没有任何错误,在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

HTML:

{% 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>

1 个答案:

答案 0 :(得分:0)

except方法中有一个全包get子句。这是一个非常非常糟糕的主意;您正在捕捉并隐藏 get_multiple_scale方法中发生的任何错误。可能有些东西出了问题,但你的代码却无法分辨出什么。

删除try / except。

你的表单的init方法中有一个类似的;在那里它更没意义,因为你最终将{分配给self.scales,这是你在get_multiple_scales中迭代的东西。这里有一个非常奇怪的循环定义,你当然不应该这样。