我正在开发一个拥有postgresql后端的django应用程序。我有一个python脚本,它经历了一个最终启动多个Web表单的过程,一个表单对应于需要修改的数据库中的表中的每个记录。该脚本迭代ID列表以确定需要修改哪些记录并一次性启动表单。我注意到一些表单将接受并保存更改,而某些表单不保存更改。我还注意到最初保存更改的表单,如果我重新加载表单并尝试进行其他更改,则不会保存。我不确定有什么问题?使用django的新手。这是html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'update/css/forms.css' %}" />
</head>
<body>
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
<div><label class="page_header">{{title}}<label><div>
<form method="POST" action="">
<div><fieldset class="fieldset">
<legend class="legend">Create Map Index Record</legend>
{% csrf_token %}
{% for field in form_mapindex.hidden_fields %}
{{ field }}
<br>
{% endfor %}
{% for field in form_mapindex.visible_fields %}
<label class="formlabel">{{ field.label_tag }}
<br>
{{ field }}
<br>
{% endfor %}
<br><button class="button" type="submit">Save Record</button>
</fieldset></div>
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-danger">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<div class="alert alert-danger">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endif %}
</form>
</body>
</html>
这里是forms.py:
from django import forms
from .models import Mapindex
class MapIndexForm(forms.ModelForm):
class Meta:
model = Mapindex
fields = ['objectid', 'sheetlabel', 'image_path_filename', 'county', 'route', 'map_sheet_desc', 'drawingdate',
'index_map_filepath', 'grantor_box_filepath', 'control_map_filepath', 'info_sheet_filepath', 'mapdatum']
widgets = {'objectid': forms.HiddenInput(),
'drawingdate': forms.SelectDateWidget(years=range(1900, 2021))}
labels = {
'sheetlabel': 'Sheet Label',
'image_path_filename': 'PDF Filepath',
'county': 'County',
'route': 'Route',
'map_sheet_desc': 'Map Sheet Description',
'drawingdate': 'Drawing Date',
'index_map_filepath': 'Index Map Filepath',
'grantor_box_filepath': 'Grantor Box Filepath',
'control_map_filepath': 'Control Map Filepath',
'info_sheet_filepath': 'Info Sheet Filepath',
'mapdatum': 'Map Datum'
}
这是views.py:
from django.http import request, HttpResponse
from django.shortcuts import render, get_object_or_404, redirect, render_to_response
from .models import Mapindex as MapIndexModel
from .forms import MapIndexForm
from django.contrib import messages
def mapindex_edit(request, row_id):
record = get_object_or_404(MapIndexModel, pk=row_id)
record.refresh_from_db()
form_mapindex = MapIndexForm(request.POST or None, instance=record)
if request.POST:
if form_mapindex.is_valid():
print 'form is valid'
form_mapindex.save()
messages.success(request, 'Record Saved!')
else:
print 'form is not valid'
return render(request, 'edit.html', {
'title': 'Map Index Update Form',
'form_mapindex': form_mapindex
})
这是一个启动表单的python脚本片段:
for oid in new_oid_list:
url = 'http://sv04gis.ct.dot.ca.gov/mapindex/update/' + str(oid)
webbrowser.open(url)
答案 0 :(得分:0)
您需要对视图进行一些更改
def mapindex_edit(request, row_id):
record = get_object_or_404(MapIndexModel, pk=row_id)
record.refresh_from_db()
if request.method == 'POST':
form_mapindex = MapIndexForm(request.POST or None, instance=record)
if form_mapindex.is_valid():
form_mapindex.save()
messages.success(request, 'Record Saved!')
else:
form_mapindex = MapIndexForm()
return render(request, 'edit.html', { 'title': 'Map Index Update Form', 'form_mapindex': form_mapindex })