手动渲染Django表单并验证

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

我已经开始创建一个表单了。基本上,表格是二十个单词的“测试”。表单由20个文本字段组成,我想要包含单词的定义。用户将输入该单词。完成后,表单应验证数据并标记正确的内容和不正确的内容。我在django中做了很多模型,但是这个模型是不同的。此表单中的所有数据都必须作为上下文传递。 views.py

def get_test(request, username='default'):
    template_name = 'main/test.html'
    if request.method == 'POST':
        pass
    else:
        lang = Language(config('USER'), config('PASS'))
        streakinfo = lang.get_streak_info()
        uniquewords = lang.get_unique_words()
        testwords = get_test_words(uniquewords)
        wordsdict = get_word_dict(testwords)
        form = TestForm()
        context = {
            'testwords': testwords, # list of random unique test words
            'wordsdict': wordsdict, # dict of words + definitions {word: {pronounciation, definition}}
            'form': form,
        }
    return render(request, template_name, context)

forms.朋友

class TestForm(forms.Form):
    word_1 = forms.CharField(label='1', max_length=100)
    word_2 = forms.CharField(label='2', max_length=100)
    word_3 = forms.CharField(label='3', max_length=100)
    word_4 = forms.CharField(label='4', max_length=100)
    word_5 = forms.CharField(label='5', max_length=100)
    word_6 = forms.CharField(label='6', max_length=100)
    word_7 = forms.CharField(label='7', max_length=100)
    word_8 = forms.CharField(label='8', max_length=100)
    word_9 = forms.CharField(label='9', max_length=100)
    word_10 = forms.CharField(label='10', max_length=100)
    word_11 = forms.CharField(label='11', max_length=100)
    word_12 = forms.CharField(label='12', max_length=100)
    word_13 = forms.CharField(label='13', max_length=100)
    word_14 = forms.CharField(label='14', max_length=100)
    word_15 = forms.CharField(label='15', max_length=100)
    word_16 = forms.CharField(label='16', max_length=100)
    word_17 = forms.CharField(label='17', max_length=100)
    word_18 = forms.CharField(label='18', max_length=100)
    word_19 = forms.CharField(label='19', max_length=100)
    word_20 = forms.CharField(label='20', max_length=100)

我的意思是,通过手动渲染每个字段很简单,但我不知道也从未做过的事情就是没有模型。例如,我想建立一个表,col 1有定义(我实际上不需要label=##,因为我再次通过上下文传递数据),col 2有字段。如何将发布数据绑定在一起,以便在发布结果时,col 2最可靠地检查col 1?简而言之,我如何手动呈现和验证表单并保持所有数据排成一行?如果这是一个广泛的问题,我提前道歉。

更新:

我能够将测试数据放入表单并使用以下(by hacking away at the forms.Form inheritance)渲染字段:

class TestForm(forms.Form):
    """
    Student test form
    """    
    def __init__(self, testdict, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.testdict = {} if testdict is None else testdict
        d = self.testdict
        for word in d:
            answer = word
            for key in d[word]:
                value = str(d[word][key])
                if key == 'id':
                    field_name = value
                if key == 'definition':
                    question = value
            self.fields[field_name] = forms.CharField(label=question, max_length=100)

仍然需要帮助。

django django-templates
2个回答
0
投票

是的,您可以覆盖clean方法并在其上实现您的验证,如下所示:

你已经使用表单类来了解所有字段,然后使用get实际获取通过name HTML参数在这些字段中输入的内容,然后通过变量操作数据,如果它们与您想要的内容不匹配那么提高Validationerror。然后,您将创建从这些字段到dict的所有数据的上下文,并将一个变量设置为最后返回的变量。

forms.py

class TestForm(forms.Form):
    word_1 = forms.CharField(label='1', max_length=100)
    word_2 = forms.CharField(label='2', max_length=100)
    word_3 = forms.CharField(label='3', max_length=100)
    word_4 = forms.CharField(label='4', max_length=100)
    word_5 = forms.CharField(label='5', max_length=100)
    word_6 = forms.CharField(label='6', max_length=100)
    word_7 = forms.CharField(label='7', max_length=100)
    word_8 = forms.CharField(label='8', max_length=100)
    word_9 = forms.CharField(label='9', max_length=100)
    word_10 = forms.CharField(label='10', max_length=100)
    word_11 = forms.CharField(label='11', max_length=100)
    word_12 = forms.CharField(label='12', max_length=100)
    word_13 = forms.CharField(label='13', max_length=100)
    word_14 = forms.CharField(label='14', max_length=100)
    word_15 = forms.CharField(label='15', max_length=100)
    word_16 = forms.CharField(label='16', max_length=100)
    word_17 = forms.CharField(label='17', max_length=100)
    word_18 = forms.CharField(label='18', max_length=100)
    word_19 = forms.CharField(label='19', max_length=100)
    word_20 = forms.CharField(label='20', max_length=100)


    def clean(self):

        word_1 = self.cleaned_data.get("word_1")
             #        |
             #        |         write the clean method of all fields
             #        |
             #      ----- 
             #       --- 
             #        - 

        word_20 = self.cleaned_data.get("word_20")



        if word_1 and word_2 and word_7 and word_15 != something:
            raise forms.ValidationError("Something Fishy")
            # i combined few of the word fields but you check all the fields separately also and implement your validation.

        words_context = {
            'word_1':word_1

            #     |               <--write all the context of corresponding fields
            #     |

            'word_20':word_20
        }

        return words_context

Views.py

def get_test(request, username='default'):
    template_name = 'main/test.html'
    form = TestForm()
    if request.method == 'POST':
        if form.is_valid():
            word_1 = self.cleaned_data.get("word_1")
             #        |
             #        |         write the clean method of all fields
             #        |
             #      ----- 
             #       --- 
             #        - 
            word_20 = self.cleaned_data.get("word_20")
            newtest = Test(word_1=word_1,....word_20=word_20)
            newtest.save()
            return redirect('whereever you want to redirect')
    else:
        lang = Language(config('USER'), config('PASS'))
        streakinfo = lang.get_streak_info()
        uniquewords = lang.get_unique_words()
        testwords = get_test_words(uniquewords)
        wordsdict = get_word_dict(testwords)
        form = TestForm()
        context = {
            'testwords': testwords, # list of random unique test words
            'wordsdict': wordsdict, # dict of words + definitions {word: {pronounciation, definition}}
            'form': form,
        }
    return render(request, template_name, context)

0
投票

我完成了这两种方式:其中一种包括文件写入,另一种包括模型写入。由于写入模型显然更快,我将展示:

在视图中,我认为这是非常直接的。这里的开头是当我在GET请求form = TestForm(wordsdict)上实例化表单时,我将单词字典传递给表单。 POST请求数据实际上并未存储,仅用于验证。因此,当我发布POST时,我只是正常发送POST数据。 wordsdict是一个由{answer:[question,id]}组成的字典

views.朋友

def language_test(request, username='johndoe', password=None):
    lang= Language(config('USER'), config('PASS'))
    streakinfo = lang.get_streak_info()
    context = {
        'username': username,
        'streakinfo': streakinfo,
    }
    template_name = 'tests/test.html'
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the saved answer dictionary:
        print('POSTING TEST RESULTS')
        form = TestForm(data=request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            print('PASSED')
            # redirect to a new URL:
            return redirect('main:success')
        else:  
            print('FAILED')
            if form.has_error:
                print('FORM ERROR')
            pass
    # if a GET (or any other method) we'll create a blank form
    else:
        print('GETTING NEW TEST')
        phrases = lang.get_known_phrases()
        testwords = get_test_words(phrases)
        wordsdict = get_word_dict(testwords)
        form = TestForm(wordsdict)
    context['form'] = form
    return render(request, template_name, context)

再往前走......

models.朋友

class TestAnswers(models.Model):
    phraseid = models.IntegerField(unique=True, blank=True, null=True)
    question = models.TextField(blank=True, null=True)
    answer = models.CharField(max_length=50, blank=True, null=True)

这是神奇发生的地方。我正在超越继承的__init__类的Form功能。当实例化类时,它将评估test_dict参数,该参数可能已经或可能未被视图传入。如果没有test_dict,它必须是新测试的请求,所以我清除测试模型并创建一个新的,其中随机选择的问题\答案传递给视图。如果没有传入test_dict,那么它必须是一个帖子请求,这意味着我需要验证所有答案。请参阅表单验证的clean方法。

forms.朋友

class TestForm(forms.Form):
    """
    Student test form
    """    
    def __init__(self, test_dict=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._resource_path = os.path.join(settings.BASE_DIR, 'static/json')
        self._json_path = os.path.join(self._resource_path, 'answers.json')
        self._model = TestAnswers
        i = 0
        phraseid, answer, question = [], [], []
        if test_dict is not None:
        # A form get request should resolve new form data and
        # store it in the database for comparison later on
            # clear out the answers table
            self._model.objects.all().delete()
            # create a list of model objects to bulk insert
            records = []
            for item in test_dict:
                record = self._model(
                    phraseid=test_dict[item]['id'],
                    answer=item,
                    question=test_dict[item]['definition']
                )
                phraseid.append(test_dict[item]['id'])
                question.append(test_dict[item]['definition'])
                answer.append(item)
                records.append(record)
            if records:
                # Insert the records into the TestAnswers table
                self._model.objects.bulk_create(records)
            self.test_dict = test_dict

        else:
        # A form post request should check the form data against
        # what was established during the get request
            # Get all the objects in the test table
            records = self._model.objects.all()
            # Put all the object items into their respective lists
            for r in records:
                phraseid.append(r.phraseid)
                answer.append(r.answer)
                question.append(r.question)
        for i in range(len(question)):
            # Set the form fields
            field_name = 'testword' + str(phraseid[i])
            # Print the answers for debugging
            print('id: ' + str(phraseid[i]))
            print('question: ' + question[i])
            print('answer:' + answer[i])
            self.fields[field_name] = forms.CharField(label=question[i], max_length=100)
        self.question = question
        self.phraseid = phraseid
        self.answer = answer

    def clean(self):
        # print('CLEANING DATA')
        phraseid, answer, question = [], [], []
        context = {}
        i = 0
        records = self._model.objects.all()
        for r in records:
            phraseid.append(r.phraseid)
            answer.append(r.answer)
            question.append(r.question)
        # Get and check the results
        for i in range(len(self.cleaned_data)):
            field_name = 'testword' + str(phraseid[i])
            result = self.cleaned_data.get(field_name)
            if result != answer[i]:
                self.add_error(field_name, 'Incorrect')
            context[i] = question[i]
            i += 1
        return context

enter image description here

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