问题1:添加到Klassen ...这有效,但在html中,richting和leraar是一个列表,你可以选择一个,但它显示完整的描述,如naam和omschrijving。当我只想要它显示naam。 image add klas
问题2:当我尝试编辑时,它会给我一个klas.richting.naam的输入。但是当我尝试更新它时,它给出了一个错误: image edit error即使我没有改变任何事情。
models.py
class Richtingen(models.Model):
naam = models.CharField(max_length=100)
omschrijving = models.TextField(max_length=2000)
def __unicode__(self):
return u'%s %s' % (self.naam, self.omschrijving)
class Leraren(models.Model):
naam = models.CharField(max_length=100)
voornaam = models.CharField(max_length=100)
foto = models.ImageField(upload_to = 'leerkrachten', default='leerkrachten/anoniem.png')
email = models.EmailField()
def __unicode__(self):
return u'%s %s %s %s' % (self.naam, self.voornaam, self.foto, self.email)
class Klassen(models.Model):
naam = models.CharField(max_length=100)
numeriekeCode = models.CharField(max_length=100)
richting = models.ForeignKey(Richtingen, on_delete=models.CASCADE)
leraar = models.ForeignKey(Leraren, on_delete=models.CASCADE)
def __unicode__(self):
return u'%s %s %s %s' % (self.naam, self.numeriekeCode, self.richting, self.leraar)
forms.py
class RichtingForm(ModelForm):
class Meta:
model = Richtingen
fields = ['naam', 'omschrijving']
class LeraarForm(ModelForm):
class Meta:
model = Leraren
fields = ['naam', 'voornaam', 'foto', 'email']
class KlasForm(ModelForm):
class Meta:
model = Klassen
fields = ['naam', 'numeriekeCode', 'richting', 'leraar']
views.py
def edit_klassen(request, id):
klas = Klassen.objects.get(id=id)
edit = True
context = {"klas": klas,
'edit': edit}
return render(request, 'main_app/klassen.html', context)
def edit_richtingen(request, id):
richting = Richtingen.objects.get(id=id)
edit = True
context = {"richting": richting,
'edit': edit}
return render(request, 'main_app/richtingen.html', context)
def edit_leraren(request, id):
leraar = Leraren.objects.get(id=id)
edit = True
context = {"leraar": leraar,
'edit': edit}
return render(request, 'main_app/leraren.html', context)
def create_richting(request):
form = RichtingForm(request.POST, request.FILES)
if form.is_valid():
form.save(commit = True)
return HttpResponseRedirect('/richtingen')
def create_leraar(request):
form = LeraarForm(request.POST, request.FILES)
edit = False
if form.is_valid():
form.save(commit = True)
return HttpResponseRedirect('/leraren')
def create_klas(request):
form = KlasForm(request.POST, request.FILES)
if form.is_valid():
form.save(commit = True)
return HttpResponseRedirect('/klassen')
def update_leraren(request, id):
Leraren.objects.get(id=id).delete()
form = LeraarForm(request.POST, request.FILES)
if form.is_valid():
form.save(commit = True)
return HttpResponseRedirect('/leraren')
def update_richtingen(request, id):
richting = Richtingen.objects.get(id=id)
richting.naam = request.POST['naam']
richting.omschrijving = request.POST['omschrijving']
richting.save()
edit = False
context = {"richting": richting,
'edit': edit}
return HttpResponseRedirect('/richtingen')
def update_klassen(request, id):
klas = Klassen.objects.get(id=id)
klas.naam = request.POST['naam']
klas.numeriekeCode = request.POST['numeriekeCode']
klas.richting = request.POST['richting']
klas.leraar = request.POST['leraar']
klas.save()
edit = False
context = {"klas": klas,
'edit': edit}
return HttpResponseRedirect('/klassen')
klassen.html
#edit
form class="all_forms" action="../update/{{klas.id}}" method="POST">
{% csrf_token %}
<label for="naam">Vak:</label> <input type="text" name="naam" id="naam" value='{{klas.naam}}' placeholder='{{klas.naam}}' /><br>
<label for="numeriekeCode">Klaslokaal:</label> <input type="text" name="numeriekeCode" id="numeriekeCode" value='{{klas.numeriekeCode}}' placeholder='{{klas.numeriekeCode}}' /><br>
<label for="richting">Richting:</label> <input type="text" name="richting" id="richting" value='{{klas.richting.naam}}' placeholder='{{klas.richting}}' /><br>
<label for="leraar">Leraar:</label> <input type="text" name="leraar" id="leraar" value='{{klas.leraar}}' placeholder='{{klas.leraar}}' /><br>
<br>
<div>
<button type="submit" name="submit">Wijzigen</button>
</div>
</form>
# create
<form class="all_forms" action="create_klas/" method="POST">
{% csrf_token %}
{{ form.as_p }}
<div>
<button type="submit" name="submit">Verzenden</button>
</div>
</form>
答案 0 :(得分:1)
对于第一个问题,您应该修改:
class Richtingen(models.Model):
naam = models.CharField(max_length=100)
omschrijving = models.TextField(max_length=2000)
def __unicode__(self):
return u'%s %s' % (self.naam, self.omschrijving)
到
class Richtingen(models.Model):
naam = models.CharField(max_length=100)
omschrijving = models.TextField(max_length=2000)
def __unicode__(self):
return u'%s' % (self.naam)
对于第二个,您应该更改表单并提供Richtingen实例,因为它显示错误:
<label for="richting">Richting:</label> <input type="text" name="richting" id="richting" value='{{klas.richting}}' placeholder='{{klas.richting}}' /><br>