Please, I have a similar case I'll introduce them and wish you could help me understand and handle this case properly.
I have the simple model case:
# -*- coding: utf-8 -*-
from django.db import models
class City(models.Model):
name = models.CharField("City", max_length=100, blank=False, null=False)
state = models.CharField("State", max_length=2, blank=False, null=False)
class Neighborhood(models.Model):
name = models.CharField("Name", max_length=100, blank=False, null=False)
city = models.ForeignKey(City, blank=False, null=False)
The model forms:
from django import forms
from app.models import *
class CityForm(forms.ModelForm):
class Meta:
model = City
class NeighborhoodForm(forms.ModelForm):
class Meta:
model = Neighborhood
state = forms.CharField("State", max_length=2, required=True)
Theirs views:
City View:
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from app.forms import CityForm
from app.models import City
STATES = ["AC", "AL", "AP", "AM", "BA", "CE", "DF", "ES", "GO", "MA", "MT", "MS", "MG", "PR",
"PB", "PA", "PE", "PI", "RJ", "RN", "RS", "RO", "RR", "SC", "SE", "SP", "TO"]
def index(request):
if "submit" in request.POST:
form = CityForm(request.POST, request.FILES)
if form.is_valid():
form.save(commit=True)
elif "cancel" in request.POST:
return HttpResponseRedirect("/")
else:
form = CityForm()
cities = City.objects.all()
data = {
"form": form,
"states": STATES,
"cities": cities
}
return render_to_response("city/index.html", data, context_instance=RequestContext(request))
And the Neighborhood view:
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from app.forms import NeighborhoodForm
from app.models import Neighborhood, City
from app.city.views import STATES
def index(request):
if "submit" in request.POST:
form = NeighborhoodForm(request.POST, request.FILES)
if form.is_valid():
form.save(commit=True)
elif "cancel" in request.POST:
return HttpResponseRedirect("/")
else:
form = NeighborhoodForm()
neighborhoods = Neighborhood.objects.all()
cities = City.objects.all()
data = {
"form": form,
"states": STATES,
"cities": cities,
"neighborhoods": neighborhoods
}
return render_to_response("neighborhood/inserir.html", data, context_instance=RequestContext(request))
And finally, the template of Neighborhood:
{% extends "base.html" %}
{% block content %}
<form action="" method="post" id="neighborhoodForm" name="neighborhoodForm">
{% csrf_token %}
<div>
<label>State:</label>
<select id="state" name="state" autofocus="autofocus">
<option value=""></option>
{% for item in states %}
<option value="{{ item }}"
{% if item == form.state.value %}
selected="selected"
{% endif %}>{{ item }}</option>
{% endfor %}
</select>
</div>
<div>
<label>City:</label>
<select id="city" name="city">
<option value=""></option>
{% for item in cities %}
<option value="{{ item.id }}"
{% if item.id == form.city.value|add:0 %}
selected="selected"
{% endif %}>{{ item.name }}</option>
{% endfor %}
</select>
</div>
<div>
<label>Neighborhood Name:</label>
<input type="text" id="name" name="name" value="{{ form.name.value|default_if_none:"" }}"/>
</div>
<div>
<button type="submit" id="submit" name="submit" value="submit">Submit</button>
<button type="submit" id="cancel" name="cancel" value="cancel">Cancel</button>
</div>
</form>
<br/>
<table border="1">
<tr>
<th>Neighborhood Name</th>
<th>City</th>
<th>State</th>
</tr>
{% for item in neighborhoods %}
<tr>
<td>{{ item.name }}</td>
<td>{{ item.city.name }}</td>
<td>{{ item.city.state }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
My difficulty on in view of neighborhoods. I have the City and State fields, which are in the neighborhood template for filters.
To add a record I have no problem, but a simple post does not return the value in the "state" of NeighborhoodForm to the template again. That's because the value that was sent in the post does not find the "state" field of the form.
When you open a record for editing happen the same thing, ie, the "state" field will not be filled.
So this is my problem. Can you help me? How should I do or what I'm doing wrong?
Thanks all for the assistance given.