首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django中的动态长度表单处理和数据采集

Django中的动态长度表单处理和数据采集
EN

Stack Overflow用户
提问于 2022-04-26 18:12:53
回答 1查看 60关注 0票数 0

我有一种形式,其中输入字段的数量不断变化,并取决于我传递的参数。这意味着我不能进入forms.py并创建一个Form类,因为这要求我事先定义输入参数。

这是我如何定义形式的方法。

代码语言:javascript
复制
<!-- Table to Display the original sentence and take the input of the translation -->
    <table class="table">
        <thead class="thead-dark">
          <tr>
            <th scope="col">#</th>
            <th scope="col">Original Sentence</th>
            <th scope="col">Translation</th>
          </tr>
        </thead>
        <form name="translated-text-form" class="form-control" action="" method="post">
            {% csrf_token %}
            <tbody>

                {% for sentence in sentences %}
                <tr>
                    <th scope="row">{{ forloop.counter }}</th>
                    <td>{{ sentence.original_sentence }}</td>
                    <td><input type="text" name="translated-{{sentence.id}}" value="{{ sentence.translated_sentence }}" /></td>
                </tr>
                {% endfor %}

                <tr>
                    <td colspan="2">
                        <br>
                        <p style="text-align: center;">
                            <input class="btn btn-secondary" type="submit" value="Save Translations"/>
                        </p>
                    </td>
                </tr>
            </tbody>
        </form>
      </table>

这是表单输出

用户将在最右边的列中添加一些文本,我希望在我的Sentence模型中使用特定的sentence.id保存该文本。这就是我的模型

代码语言:javascript
复制
class Sentence(models.Model): 
    """ Model which holds the details of a sentence. A sentence is a part of a Wikipedia article which is tokenized.

    Fields:
        project_id: The ID of the project to which the sentence belongs 
        original_sentence: The original sentence tokenized from from the Wikipedia article.
        translated_sentence: The translated sentence in the target language.
    """

    # Define the sentence model fields 
    project_id = models.ForeignKey(Project, on_delete=models.CASCADE)
    original_sentence = models.CharField(max_length=5000)
    translated_sentence = models.CharField(max_length=5000, default="No Translation Found")

我知道我会怎么处理这个问题。在我的views.py中,我希望运行一个for-循环,并从命名为translated-{{sentence.id}}的表单中收集数据。但是,我无法使POST请求处理程序能够直接从表单中收集数据并根据句子的Sentence模型将其保存在id模型中。我需要帮助,在我的意见书面请求处理程序。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-27 07:53:33

我找到了一个完全不使用forms.py的修复程序。这就是post请求处理程序的样子。

代码语言:javascript
复制
    # Handle the post request
    if request.method == 'POST':

        # Get the project details from the database
        all_sentence_data = Sentence.objects.filter(project_id=pk)
        sentence_ids = [sentence.id for sentence in all_sentence_data]

        # Iterate through all the input fields in the form 
        for i in range(len(sentence_ids)): 
            
            # Get the translated text from the form
            try: 
                translated_text = request.POST[f"translated-{str(sentence_ids[i])}"]

                if translated_text: 
                    
                    # Update the Sentence object with the translated text
                    Sentence.objects.filter(id=sentence_ids[i]).update(translated_sentence=translated_text)
            
            except: 
                continue

        messages.success(request, "Your translations have been saved successfully!")
        return redirect('/translation/' + str(pk)+'/')
    
    else: 
        return redirect('/translation/' + str(pk)+'/')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72018663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档