gpt4 book ai didi

python - 尝试从数据库更新现有行时,Django 表单未验证

转载 作者:行者123 更新时间:2023-11-30 22:18:32 25 4
gpt4 key购买 nike

我正在开发我的第一个 Django 项目。但我收到以下错误:

编辑文件模板

                <form method="POST" action="{% url 'edit_file' file.id %}">
{% csrf_token %}

{{ form.errors }}
{{ form.non_field_errors }}

{% for hidden_field in form.hidden_fields %}
{{ hidden_field.errors }}
{{ hidden_field }}
{% endfor %}

<div class="form-group row">
<label for="id_name" class="col-sm-3 col-form-label"> File Name </label>
<div class="col-sm-4">
{% render_field form.name|add_class:"form-control" %}
</div>
</div>

<div class="form-group row">
<label class="col-sm-3 col-form-label">File Path</label>
<div class="col-sm-4">
{% render_field form.directory_path|add_class:"form-control" %}
</div>
</div>

<div class="form-group">
{% render_field form.script_code|add_class:"form-control" %}
<pre id="id_script_code" style="height: 40pc;">{{ form.script_code }}</pre>
</div>

<button type="submit" class="btn btn-success mr-2">Save Changes</button>
<a href="{% url 'list_files_from_version' file.version_id %}" class="btn btn-light">Back</a>

</form>

Views.py

def edit_file(request, id):
instance = get_object_or_404(File, id=id)
if request.method == "POST":
form = EditFileForm(request.POST, instance=instance)
if form.is_valid():
print('Form validation => True')
form.save()
return HttpResponse('<h1> database updated! </h1>')
else:
print('Form validation => False')
file = File.objects.latest('id')
context = {'file': file, "form": form}
return render(request, 'edit_file.html', context)
else:
instance = get_object_or_404(File, id=id)
form = EditFileForm(request.POST or None, instance=instance)
file = File.objects.latest('id')
context = {'file': file, "form": form}
return render(request, 'edit_file.html', context)

forms.py

class EditFileForm(ModelForm):
# field_order = ['field_1', 'field_2']

class Meta:
print("forms.py 1")
model = File
fields = ('name', 'script_code', 'directory_path','version')

def clean(self):
print("forms.py 2")
cleaned_data = super(EditFileForm, self).clean()
name = cleaned_data.get('name')
print("cleaned data: ", cleaned_data)

型号:

版本id指向包含多个文件的版本。

  class File(models.Model):
# Incrementing ID (created automatically)
name = models.CharField(max_length=40)
script_code = models.TextField() # max juiste manier?
directory_path = models.CharField(max_length=200)
version = models.ForeignKey('Version', on_delete=models.CASCADE)

class Meta(object):
db_table = 'file' # table name


class Version(models.Model):
# Incrementing ID (created automatically)
version_number = models.CharField(max_length=20)
pending_update = models.BooleanField(default=False)
creation_date = models.DateTimeField(auto_now_add=True, null=True, editable=False)
modification_date = models.DateTimeField(auto_now_add=True, null=True)
connecthor = models.ForeignKey('ConnecThor', on_delete=models.CASCADE)

def __str__(self):
return self.connecthor_id

问题:

form.is_valid() 不断失败。我的 View 返回一个错误。*版本:此字段是必填字段。但我不知道如何解决此问题。用户应该只能更新 5 个数据字段中的 3 个。所以没有理由在模板中显示PK或FK。

最佳答案

您已将版本包含在表单的字段列表中,但您没有在模板中输出它,因此无法提供它。由于模型字段未指定blank=True,因此它是必填字段,因此会出现错误。

如果您不希望用户能够修改此字段,则应将其从元下的字段列表中删除。

关于python - 尝试从数据库更新现有行时,Django 表单未验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49350896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com