Hello Respected community
I am having severe troubles in assembling a working form that should update a record in a Postgres database.
In the form and before the user clicks submit - i would like to present the user with a drop-down choices widget for Regions. I cant figure out how to make the html template to show the selection drop-down box correctly.
models.py:
class SeatsModel(models.Model):
SUPPORTED_REGIONSS = (
('NA', 'North-America'),
('LATAM', 'Latin-America'),
('APAC', 'Asia, Pacific'),
('EMEA', 'Europe, Middle-East and Africa'),
)
id = models.IntegerField(primary_key=id)
account_id = models.IntegerField
seat = models.CharField(max_length=1024)
agency = models.CharField(max_length=1024)
agency_region = models.CharField(max_length=5, choices=SUPPORTED_REGIONSS)
is_holding_grp = models.BooleanField()
insert_time = models.DateTimeField()
update_time = models.DateTimeField(auto_now=True)
class Meta:
db_table = 'dim_seats'
forms.py:
class seatform(forms.ModelForm):
agency_region = forms.ChoiceField(widget=forms.Select(), choices=SeatsModel.SUPPORTED_REGIONSS)
def __init__(self, *args, **kwargs):
super(seatform, self).__init__(*args, **kwargs)
self.fields['agency_region'] = forms.ChoiceField(choices=SUPPORTED_REGIONS)
class Meta:
model=SeatsModel
fields = ['agency', 'agency_region', 'is_holding_grp']
'''
views.py
def updateseat(request, id):
updateseat=SeatsModel.objects.get(id=id)
form=seatform(request.POST, instance=updateseat)
if form.is_valid():
form.save()
messages.success(request,'Updated Successfully!')
return render(request,'editseat.html',{"SeatsModel":updateseat})
else:
#messages.error(request, 'Invalid values, Update Failed!')
messages.error(request,form.errors)
return render(request,'editseat.html',{"SeatsModel":updateseat})
my template (updateseat.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Peer39 - Usage Entity Mapping</title>
</head>
<body>
<center>
<h1>Edit Usage Entities</h1>
<h2>Seat: <b style="color: #1e7e34">{{SeatsModel.seat}}</b></h2>
<h3>ID: <b style="color: #1e7e34">{{SeatsModel.id}}</b></h3>
<hr/>
{% comment %}<form method="post" id="regionsForm" data-regioon-url="{% url 'ajax_load_regions' %}">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>{% endcomment %}
<form method="POST" action="/UpdateSeat/{{SeatsModel.id}}">
{% csrf_token %}
<table border="1">
<tr>
<td>Seat ID</td>
<td><input type="text" name="id" value="{{SeatsModel.id}}" style="color:#888;" readonly></td>
</tr>
<tr>
<td>Seat Name</td>
<td><input type="text" name="seat" value="{{SeatsModel.seat}}" style="color:#888;" readonly></td>
</tr>
<tr>
<td>Agency Name</td>
<td><input type="text" name="agency" value="{{SeatsModel.agency}}"></td>
</tr>
<tr>
<td>Agency Region no</td>
<td>
<select>
<option value="{{ SeatsModel.agency_region }}">{{ SeatsModel.get_agency_region_display }}</option>
{% for key, value in seatform.agency_region %}
<option value="{{ key }}">{{ value }}</option>
{% endfor %}
</select>
</td>
</tr>
<tr>
<td>Is Holding Group</td>
<td><input type="checkbox" name="is_holding_grp" value="{{SeatsModel.is_holding_grp}}"></td>
</tr>
<tr>
<td><input type="submit" value="Click to Update Seat!"></td>
<td>
{% if messages %}
{% for mess in messages %}
{% if mess.tags == "error"%}
<b style="color: crimson">{{ mess }}</b>
{% else %}
<b style="color: forestgreen">{{ mess }}</b>
{% endif %}
{% endfor %}
{% endif %}
</td>
</tr>
</table>
<hr/>
<a href="{% url 'showseatsview' %}">Go To Seats Page</a>
</form>
</center>
</body>
</html>
I will appreciate much if someone can guide on what am i doing wrong.
Thanks !!
Boaz