这是我的第一个django应用程序,我正在使用一个模型来更新表中的现有记录。我一直在做一些测试并且在添加新记录时没有任何问题,但是在编辑现有记录时,我的表单似乎表现得如预期(我没有错误),但更改不会被保存。我错过了什么?几天来我一直在努力,任何帮助都会受到最高的赞赏。
models.py:
class Mapindex(models.Model):
COUNTY_CHOICES = (
('ALA', 'Alameda'),
('CC', 'Contra Costa'),
('MRN', 'Marin'),
('NAP', 'Napa'),
('SCL', 'Santa Clara'),
('SF', 'San Francisco'),
('SM', 'San Mateo'),
('SOL', 'Solano'),
('SON', 'Sonoma'),
)
ROUTE_CHOICES = (
('1', '1'),
('4', '4'),
('8', '8'),
('9', '9'),
('12', '12'),
('13', '13'),
('14', '14'),
('17', '17'),
('21', '21'),
('24', '24'),
('25', '25'),
('29', '29'),
('32', '32'),
('35', '35'),
('37', '37'),
('49', '49'),
('52', '52'),
('61', '61'),
('77', '77'),
('80', '80'),
('82', '82'),
('84', '84'),
('85', '85'),
('87', '87'),
('92', '92'),
('93', '93'),
('100', '100'),
('101', '101'),
('102', '102'),
('104', '104'),
('112', '112'),
('113', '113'),
('114', '114'),
('116', '116'),
('117', '117'),
('121', '121'),
('123', '123'),
('128', '128'),
('130', '130'),
('131', '131'),
('141', '141'),
('152', '152'),
('156', '156'),
('160', '160'),
('185', '185'),
('220', '220'),
('221', '221'),
('230', '230'),
('237', '237'),
('238', '238'),
('242', '242'),
('260', '260'),
('262', '262'),
('280', '280'),
('281', '281'),
('380', '380'),
('480', '480'),
('505', '505'),
('580', '580'),
('680', '680'),
('780', '780'),
('880', '880'),
('980', '980')
)
MAP_DATUMS_CHOICES = (
('CCS27Z2E', 'CCS27Z2E'),
('CCS27Z2M', 'CCS27Z2M'),
('CCS27Z3E', 'CCS27Z3E'),
('CCS27Z3M', 'CCS27Z3M'),
('CCS27Z4E', 'CCS27Z4E'),
('CCS83Z2E', 'CCS83Z2E'),
('CCS83Z2M', 'CCS83Z2M'),
('CCS83Z3E', 'CCS83Z3E'),
('CCS83Z3M', 'CCS83Z3M'),
('HARN83Z2E', 'HARN83Z2E'),
('HARN83Z2M', 'HARN83Z2M'),
('HARN83Z3E', 'HARN83Z3E'),
('HARN83Z3M', 'HARN83Z3M'),
('LOCAL_E', 'LOCAL_E'),
('LOCAL_M', 'LOCAL_M'),
)
objectid = models.IntegerField(unique=True, primary_key=True)
sheetlabel = models.CharField(max_length=8000, blank=True, null=True)
county = models.CharField(max_length=8000, choices=COUNTY_CHOICES)
route = models.CharField(max_length=8000, choices=ROUTE_CHOICES)
pmb = models.CharField(max_length=50, null=True)
pme = models.CharField(max_length=50, null=True)
map_sheet_desc = models.CharField(max_length=8000, blank=True, null=True)
hangingfilegroup = models.CharField(max_length=8000, blank=True, null=True)
maptype = models.CharField(max_length=8000, blank=True, null=True)
index_map_filepath = models.CharField(max_length=8000, blank=True, null=True)
grantor_box_filepath = models.CharField(max_length=8000, blank=True, null=True)
status = models.CharField(max_length=8000, blank=True, null=True)
publish = models.CharField(max_length=8000, blank=True, null=True)
mapdatum = models.CharField(max_length=8000, choices=MAP_DATUMS_CHOICES)
geo_referenced_datum = models.CharField(max_length=8000, blank=True, null=True)
mapscale = models.CharField(max_length=8000, blank=True, null=True)
mapyear = models.CharField(max_length=8000, blank=True, null=True)
drawingdate = models.CharField(max_length=8000, blank=True, null=True)
image_path_filename = models.CharField(max_length=8000, blank=True, null=True)
control_map_filepath = models.CharField(max_length=8000, blank=True, null=True)
info_sheet_filepath = models.CharField(max_length=8000, blank=True, null=True)
image_web_path = models.CharField(max_length=300, blank=True, null=True)
image_web_path_public = models.CharField(max_length=300, blank=True, null=True)
grantor_box_web_path = models.CharField(max_length=300, blank=True, null=True)
grantor_box_web_path_public = models.CharField(max_length=300, blank=True, null=True)
longitude = models.DecimalField(max_digits=38, decimal_places=8, blank=True, null=True)
latitude = models.DecimalField(max_digits=38, decimal_places=8, blank=True, null=True)
survey_info_link = models.CharField(max_length=300, blank=True, null=True)
survey_index_link = models.CharField(max_length=300, blank=True, null=True)
control_pdf_link = models.CharField(max_length=300, blank=True, null=True)
is_name = models.CharField(max_length=100, blank=True, null=True)
is_name_control = models.CharField(max_length=100, blank=True, null=True)
error_report_path = models.CharField(max_length=300, blank=True, null=True)
imagefunctions = models.CharField(max_length=300, blank=True, null=True)
controlimagefunctions = models.CharField(max_length=300, blank=True, null=True)
centroid_postmile = models.CharField(max_length=50, blank=True, null=True)
class Meta:
# managed = False
db_table = 'mapindex'
forms.py:
from django import forms
from .models import Mapindex
class MapIndexForm(forms.ModelForm):
class Meta:
model = Mapindex
fields = ['objectid', 'image_path_filename', 'county', 'route', 'pmb', 'pme', 'map_sheet_desc', 'drawingdate',
'index_map_filepath', 'grantor_box_filepath', 'control_map_filepath', 'info_sheet_filepath', 'mapdatum']
widgets = {'objectid': forms.HiddenInput()}
labels = {
'image_path_filename': 'PDF Filepath',
'county': 'County',
'route': 'Route',
'pmb': 'Begin Postmile',
'pme': 'End Postmile',
'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
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)
form_mapindex = MapIndexForm(request.POST or None, instance=record)
return render(request, 'edit.html', {
'title': 'Map Index Update Form',
'form_mapindex': form_mapindex
})
if request.method == 'POST' and form_mapindex.isvalid():
form_mapindex.save()
return redirect(home)
context = {'form_mapindex': record}
return render(request, '/update/')
def saved(request):
msg = 'Record saved!'
return HttpResponse(msg)
urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.saved, name='saved'),
url(r'(?P<row_id>[0-9]+)/$', views.mapindex_edit , name='edit'),
]
和模板:
<!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">
</head>
<body>
<table>
<thead><th>{{title}}</th>
</table>
<form method="POST" action="/update/">
<fieldset>
<legend>Create Map Index Record</legend>
{% csrf_token %}
<!-- {{form_mapindex}} -->
{% for field in form_mapindex.hidden_fields %}
{{ field }}
<br>
{% endfor %}
{% for field in form_mapindex.visible_fields %}
{{ field.label_tag }}
<br>
{{ field }}
<br>
{% endfor %}
<br><button type="submit" onclick="window.close()">Save Record</button>
</fieldset>
{% 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>
答案 0 :(得分:1)
因为您的视图在到达保存表单的代码之前返回。
与任何函数一样,一旦返回命中,就不再执行该函数中的代码。将该返回移动到函数的末尾。
答案 1 :(得分:1)
def mapindex_edit(request, row_id):
record = get_object_or_404(MapIndexModel, pk=row_id)
form_mapindex = MapIndexForm(request.POST or None, instance=record)
if request.POST:
if form_mapindex.is_valid():
form_mapindex.save()
return redirect(home)
return render(request, 'edit.html', {
'title': 'Map Index Update Form',
'form_mapindex': form_mapindex
})
应该修复它。
如果表单无效,它将到达第二个return语句并再次显示表单以及错误(模板看起来很快就能显示出来)