Conor Kelt
Conor Kelt

Reputation: 1

Django - Select field not showing Options

model.py

from django.db import models
from django import forms
class CharApp(forms.Form):
    VAMPIRE = 'VP'
    DRAUGR = 'DR'
    BAELNORN = 'BN'
    LICH = 'LH'
    SHADOW = 'SW'
    RACE_CHOICES = (
        ('VP', 'Vampire'),
        ('DR', 'Draugr'),
        ('BN', 'Baelnorn'),
        ('LH', 'Lich'),
        ('SW', 'Shadow'),
    )
    app_id = models.AutoField(primary_key=True)
    char_name = models.CharField(max_length=80, verbose_name='Character Name')
    race = forms.Form(forms.ChoiceField( choices = RACE_CHOICES))
    date_applied = models.DateTimeField(verbose_name='Date Applied')
    background = models.TextField(verbose_name='Background')
    account_id = models.IntegerField(default=1, verbose_name='Account ID')
    submitted = models.BooleanField(default=False)

create.html

    <p><label for="race">Race:</label>
        <select name="{{ form.race.choices }}">
            {% for choices in form.race.choices %}
                {{ form.race.choices }}
            {% endfor %}
        </select>

For some reason the select section of the html doesn't loop at all and shows no value at all or the text.

Upvotes: 0

Views: 4727

Answers (2)

Diego Vin&#237;cius
Diego Vin&#237;cius

Reputation: 2223

Why you dont use builtin django Select?

from django import forms
>>> CHOICES = (('1', 'First',), ('2', 'Second',))
>>> choice_field = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES)
>>> choice_field.choices
[('1', 'First'), ('2', 'Second')]
>>> choice_field.widget.choices
[('1', 'First'), ('2', 'Second')]
>>> choice_field.widget.choices = ()
>>> choice_field.choices = (('1', 'First and only',),)
>>> choice_field.widget.choices
[('1', 'First and only')]

https://docs.djangoproject.com/en/2.0/ref/forms/widgets/#widgets-inheriting-from-the-select-widget

EDIT: As im looking at your code you doing something pretty weird... you can defing your Form in your models and you settings the models fields in your form? i guess you missplacing the concepts

models.py

from django.db import models

# THIS IS YOUR MODEL, WILL BE PERSISTED IN YOUR DATABASE
class Race(models.Model):
    ... # Here you will place fields related to the race, as name, date and etc...


class CharApp(models.Model):
    VAMPIRE = 'VP'
    DRAUGR = 'DR'
    BAELNORN = 'BN'
    LICH = 'LH'
    SHADOW = 'SW'
    RACE_CHOICES = (
        ('VP', 'Vampire'),
        ('DR', 'Draugr'),
        ('BN', 'Baelnorn'),
        ('LH', 'Lich'),
        ('SW', 'Shadow'),
    )

    app_id = models.AutoField(primary_key=True)
    char_name = models.CharField(max_length=80, verbose_name='Character Name')
    race = models.ForenignKey(Race, related_name="charapp_race")
    date_applied = models.DateTimeField(verbose_name='Date Applied')
    background = models.TextField(verbose_name='Background')
    account_id = models.IntegerField(default=1, verbose_name='Account ID')
    submitted = models.BooleanField(default=False)

forms.py #create this file at your app folder

from django.forms import ModelForm
from myapp.models import Article

from .models import CharApp, RACE_CHOICES

# this is your form
class CharAppForm(ModelForm):
    # Here you can change the widget or add new fields that is not related your model form
    race = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES) #if you want to change the default form widget to this field

    class Meta:
        model = CharApp
        fields = ['__all__'] # This will get all fields from your form, or just place each field you want to display

Keep in mind to create your form instance at your view.py

from .models import CharApp
from .forms import CharAppForm

# The function that will render your html or give you responses from your backend to your frontend

def your_view(request): 
    form = ConfigAppForm() # This will render your form with no value (so when pages render)
    if request.method == "POST:
        form = ConfigAppForm(request.POST) # This will bring the data that user have filled at your html and sended to your backend
        if form.is_valid(): # Check if the form is valid and able to save
            form.save() # will save the instance of your ConfigApp at your database

In your html you can call

<!-- This will render your entire form -->
{{ form.as_p}}
<!-- Obs: If you need to add css class or something like it you can override the widget at your form.py and add this classes thought attr of your field -->

Some following guides to understand the concept of MVC (or MTV of django)

MVC: https://softwareengineering.stackexchange.com/questions/127624/what-is-mvc-really

MTV Django: https://docs.djangoproject.com/en/2.0/faq/general/

Django Views: https://docs.djangoproject.com/en/2.0/topics/http/views/

Django Models: https://docs.djangoproject.com/en/2.0/topics/db/models/

Django Models Forms: https://docs.djangoproject.com/en/2.0/topics/forms/modelforms/

EDIT

If you want to make your code works without doing anything i said before just do that

VAMPIRE = 'VP'
DRAUGR = 'DR'
BAELNORN = 'BN'
LICH = 'LH'
SHADOW = 'SW'
RACE_CHOICES = (
    ('VP', 'Vampire'),
    ('DR', 'Draugr'),
    ('BN', 'Baelnorn'),
    ('LH', 'Lich'),
    ('SW', 'Shadow'),

class CharApp(forms.Form):
    ...
    race = forms.Form(forms.ChoiceField( choices = RACE_CHOICES))

Upvotes: 1

vorujack
vorujack

Reputation: 1950

you must declear race field in form as CharField not Form. look this

race = forms.ChoiceField(choices = RACE_CHOICES, widget=forms.Select())

then in your template print options like this

{% for key, value in form.race.choices %}
    <option value="{{ key }}">{{ value }}</option>
{% endfor %}

Upvotes: 0

Related Questions