Как я могу исправить эту ошибку: UnboundLocalError в / templateName / локальная переменная 'Name', на которую ссылаются до назначения? - PullRequest
0 голосов
/ 07 февраля 2019

Я получил эту ошибку:

UnboundLocalError в / home / локальной переменной 'topic_name', на которую ссылается до назначения

Я пыталсяпереместить вещи в моем views.py, но это не работает.И я попытался изменить URL в home / results, но это не сработало.Это мой views.py

from django.shortcuts import render, render_to_response, redirect
from django.views.generic import TemplateView, CreateView
from home.models import Topic, Image, Question, Answer
from home.forms import QuizMultiForm



class QuizView(TemplateView):
    template_name = 'index.html'
    def get(self, request):
        form = QuizMultiForm()
        return render (request, self.template_name, {'form': form})

    def post(self, request):
        form = QuizMultiForm(request.POST)
        if form.is_valid():
            topic_name = form.cleaned_data['topics']['topic_name']
            question_type = form.cleaned_data['questions']['question_type']
            question_description = form.cleaned_data['questions']['question_description']
            question_answer = form.cleaned_data['questions']['question_description']
            question_answer = form.cleaned_data['questions']['question_description']

        args = {'form': form, 'topic_name': topic_name, 'question_type': question_type, 'question_description': question_description, 'question_answer': question_answer,'question_image': question_image}
        return render (request, 'results.html', args)

А это мой forms.py

from django import forms
from betterforms.multiform import MultiModelForm
from .models import Topic, Image, Question, Answer
from .choices import questions_type_choices, question_topic_name_choices

class TopicForm(forms.ModelForm):
    topic_name      =   forms.ChoiceField(
                    choices=question_topic_name_choices,
                    widget = forms.Select(
                    attrs = {'class': 'home-select-one'}
                        ))

    class Meta:
        model = Topic
        fields = ['topic_name',]
        def __str__(self):
            return self.fields


class QuestionForm(forms.ModelForm):
    question_type =   forms.ChoiceField(
                    choices= questions_type_choices,
                    widget = forms.Select(
                    attrs = {'class': 'home-select-two'},
                        ))
    question_answer = forms.CharField(
                    max_length=50,
                    widget  = forms.HiddenInput()
                        )
    question_image = forms.CharField(
                    max_length=50,
                    widget  = forms.HiddenInput()
                        )
    question_description = forms.CharField(
                    max_length=50,
                    widget  = forms.HiddenInput()
                        )
    class Meta:
        model = Question
        fields = ['question_type', 'question_description', 'question_answer', 'question_image']
        def __str__(self):
            return self.fields


class QuizMultiForm(MultiModelForm):
    form_classes    =   {
                'topics':TopicForm,
                'questions':QuestionForm
    }
    def save(self, commit=True):
        objects = super(QuizMultiForm, self).save(commit=False)

        if commit:
            topic_name = objects['topic_name']
            topic_name.save()
            question_type = objects['question_type']
            question_type.topic_name = topic_name
            question_type.save()
        return objects

Это мой шаблон, где я хочу показать результаты

        {% extends 'base.html' %}
        {% block content %}
                <table>
                  <thead>
                    <tr>
                      <th>Topic Number : # {{ topic_name }}</th>
                      <th>Question Type: {{ question_type }}</th>
                    </tr> 
                  </thead>
                  <tbody>
                    <tr>
                        <td>The Question:{{ question_description }}</td>
                        <td>The Question Image: {{ question_image }}</td>
                    </tr>
                    <!-- <tr>
                      <td>The Answer:{{ answer_description }}</td>
                      <td>The Answer Image:{{ answer_image }}</td>
                    </tr> -->
                  </tbody>
                </table>
          {% endblock content %}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...