Django 中的动态长度表单处理和数据收集
Dynamic Length Form handling and Data Collection in Django
我有一个表单,其中输入字段的数量不断变化,并且取决于我传递的参数。这意味着我无法转到 forms.py 并创建 Form
class,因为这需要我事先定义输入参数。
这是我定义表单的方式。
<!-- 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
保存该文本。这是我的模型的样子
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}}
的表单中收集数据。但是,我无法制作可以直接从表单收集数据并将其保存在基于句子 id
的 Sentence
模型中的 POST 请求处理程序。我需要帮助在我的视图中编写请求处理程序。
我找到了一个根本不使用 forms.py
的修复程序。这就是 post 请求处理程序的样子。
# 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)+'/')
我有一个表单,其中输入字段的数量不断变化,并且取决于我传递的参数。这意味着我无法转到 forms.py 并创建 Form
class,因为这需要我事先定义输入参数。
这是我定义表单的方式。
<!-- 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
保存该文本。这是我的模型的样子
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}}
的表单中收集数据。但是,我无法制作可以直接从表单收集数据并将其保存在基于句子 id
的 Sentence
模型中的 POST 请求处理程序。我需要帮助在我的视图中编写请求处理程序。
我找到了一个根本不使用 forms.py
的修复程序。这就是 post 请求处理程序的样子。
# 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)+'/')