1

Question 1: Adding into Klassen... this works but in the html the richting and the leraar is a list where you can pick one but it show the full description like naam and omschrijving. I only want that it shows the naam. image add klas

Question 2: When I try to edit, it gives me an input of klas.richting.naam. But When I try to update it it gives ma an error: image edit error Even when I doesn't change anything.

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>
EnzoTrompeneers
  • 1,031
  • 1
  • 14
  • 30

1 Answers1

1

For the first question you should modify:

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)

to

class Richtingen(models.Model):
    naam = models.CharField(max_length=100)
    omschrijving = models.TextField(max_length=2000)
    def __unicode__(self):
        return u'%s' % (self.naam)

For the second one you should change the form and provide Richtingen instance as it show in error:

<label for="richting">Richting:</label> <input type="text" name="richting" id="richting" value='{{klas.richting}}' placeholder='{{klas.richting}}' /><br>
Alexander Tyapkov
  • 4,837
  • 5
  • 39
  • 65
  • For question 1: It worked but can you explain me because on another page I use 'omschrijving' and with deleting in the function unicode is still works for omschrijving to on the other page. Just wondering how does that come? – EnzoTrompeneers Jan 19 '17 at 01:11
  • it is just a method with returns result with system encoding and it is up to you what will be returned. The change in this method shouldn't influence any functionality. What about second form? – Alexander Tyapkov Jan 19 '17 at 01:18
  • For question 2: changing klas.richting to klas.richting.naam worked ! – EnzoTrompeneers Jan 19 '17 at 01:19
  • Normally a better practice is to create form and pass it with context to the page where it will be rendered. In your case you create the form but don't use it in the template! Check django tutorial for more info https://docs.djangoproject.com/en/1.10/topics/forms/ – Alexander Tyapkov Jan 19 '17 at 01:26
  • @EnzoTrompeneers if everything worked please mark my answer as helpful. – Alexander Tyapkov Jan 19 '17 at 01:41
  • @AlexanderTyapkov I feel my question is similar to this, but I can't figure it out. http://stackoverflow.com/questions/43671458/django-crud-update-object-with-many-to-one-relationship-to-user. Can you explain instances in forms? – Jeff Tilton Apr 29 '17 at 17:42