Reputation: 424
Very new to django. I would like to have each field in a modelform in a new row on the html page. I've tried to use but that doesn't work. How can I do this? Here is the html I have for the page that displays my form for capturing data:
<!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>
{% for record in record_list%}
<tr><td>{{record.county}}</td></tr>
<tr><td>{{record.route}}</td></tr>
<tr><td>{{record.pmb}}</td></tr>
<tr><td>{{record.pme}}</td></tr>
<tr><td>{{record.map_sheet_desc}}</td></tr>
<tr><td>{{record.drawingdate}}</td></tr>
<tr><td>{{record.index_map_filepath}}</td></tr>
<tr><td>{{record.grantor_box_filepath}}</td></tr>
<tr><td>{{record.control_map_filepath}}</td></tr>
<tr><td>{{record.info_sheet_filepath}}</td></tr>
<tr><td>{{record.mapdatum}}</td></tr>
{% endfor %}
</table>
<form method="POST" action="/update/">
<fieldset>
<legend>Create Map Index Record</legend>
{% csrf_token %}
{{form_mapindex}}
<br><button type="submit">Create Record</button>
</fieldset>
</form>
</body>
</html>
And here is the code in my views.py:
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.views.generic import View
from .models import Mapindex as MapIndexModel
from .forms import MapIndexForm
class MapIndexView(View):
template_name = 'add.html'
def get(self, request):
record_list = []
form_mapindex = MapIndexForm()
records = MapIndexModel.objects.all()[:50]
for record in records:
record_list.append({'county': record.county, 'route': record.route, 'pmb': record.pmb, 'pme': record.pme,
'map_sheet_desc': record.map_sheet_desc, 'drawingdate': record.drawingdate,
'index_map_filepath': record.index_map_filepath, 'grantor_box_filepath': record.grantor_box_filepath,
'control_map_filepath': record.control_map_filepath, 'info_sheet_filepath': record.info_sheet_filepath,
'mapdatum': record.mapdatum})
return render(request, self.template_name, {
'title': 'Map Index Update Form',
'mapindex_list': record_list,
'form_mapindex': form_mapindex
})
def post(self, request):
form_mapindex = MapIndexForm(request.POST)
if form_mapindex.is_valid():
form_mapindex.save()
return HttpResponseRedirect('/update/')
and here is forms.py:
from django import forms
from .models import Mapindex
class MapIndexForm(forms.ModelForm):
class Meta:
model = Mapindex
fields = ['county', 'route', 'pmb', 'pme', 'map_sheet_desc', 'drawingdate', 'index_map_filepath',
'grantor_box_filepath', 'control_map_filepath', 'info_sheet_filepath', 'mapdatum']
Upvotes: 0
Views: 1106
Reputation: 2963
You're returning record_list
but assigning it to mapindex_list
. Either iterate through mapindex_list
in the template or change the name in the render function to match:
return render(request, self.template_name, {
'title': 'Map Index Update Form',
'record_list': record_list,
'form_mapindex': form_mapindex
})
Upvotes: 1