0

I read the following thread: Django Multiple Choice Field / Checkbox Select Multiple

But I somehow miss something important as I can't succeed in displaying the checkboxes in my template. However, the name of the field does appear in the template but that's all, after the field name, it's all white and blank.

Curiously, in the thread I read, the author didn't wrote a list of tuple. That's why I think the problem could lie in the models.py

Here is my models.py

from django.db import models
from user.models import User

class RegionChoices(models.Model):
    REGION_CHOICES = (
        ('London', 'Londres'),
        ('Paris', 'Paris'),
        ('Berlin', 'Berlin'),
    )
    region = models.CharField(max_length=30, choices=REGION_CHOICES)

    def __str__(self):
        return self.region    

class Offer(models.Model):
    publisher = models.ForeignKey(User)
    content = models.TextField()
    region_choices = models.ManyToManyField(RegionChoices)

    def __str__(self):
        return self.publisher.username

forms.py

from django import forms
from django.contrib import admin
from django.conf import settings

from offers.models import Offer, RegionChoices

class SendOfferForm(forms.ModelForm):
    region_choices = forms.ModelMultipleChoiceField(queryset=RegionChoices.objects.all(), widget=forms.CheckboxSelectMultiple)
    class Meta:
        model = Offer
        exclude = ['publisher']

offer.html

<form action="{% url "send_offer" %}" method='POST' class='sendofferform'>
    {{ form.errors }}
    {{ form.non_field_errors }}
    {% csrf_token %}
    {{ offerform.as_p }}
</form>

views.py

    if offerform.is_valid():        
        sent = True
        offer = offerform.save(commit=False)
        offer.publisher = User.objects.get(id=logged_user.id)
        offer.save()
        offerform.save_m2m()
    else:
        print(offerform.errors)
Community
  • 1
  • 1
Cobblepot
  • 59
  • 10
  • 1
    This will show you checkboxes for all the instances of RegionChoices in your database. But have you ever actually created any instances of RegionChoices? – Daniel Roseman Aug 10 '15 at 18:08
  • No that's right, I haven't but I thought instances of RegionChoices would have been loaded by the list of tuple REGION_CHOICES :-/ How am I supposed to instance regions in a proper way? – Cobblepot Aug 10 '15 at 18:24

2 Answers2

1

From your code sounds like you want to limit the choices of region your project can have. I think you should create an admin for RegionChoices first. In there you could create entrances of RegionChoices you like. Follow the django docs if you are not sure how to create an admin interface for a model https://docs.djangoproject.com/en/1.8/ref/contrib/admin/

ps: You might want to do unique=True on region field in RegionChoices. Otherwise you might create duplicate entries of the same region by accident.

Shang Wang
  • 24,909
  • 20
  • 73
  • 94
0

Okay, I realize I had to load data in the model RegionChoices. I loaded the data in the admin part of my website and now, it works perfectly.

Cobblepot
  • 59
  • 10