1

I have just started with creating Django app. I have a class in models.py, which have a class variable CATEGORY_CHOICES with values for choices field.

#models.py
from django.db import models
from django.utils import timezone

class Entry(models.Model):

    CATEGORY_CHOICES = (
        ('c1','choice 1'),
        ('c2','choice 2'),
        ('c3','choice 3'),
        ('c4','choice 4'),
    )

    date = models.DateField(default=timezone.now)
    time = models.TimeField(default=timezone.now)
    category = models.CharField(choices=CATEGORY_CHOICES,blank=False,null=False,max_length=2)
    value = models.TextField()

I want to make form for creting instanes of class Entry. I'm having problem with the select menu, where I want to put values from CATEGORY_CHOICES but I can't figure out how.

<!-- homepage.html -->
{% load staticfiles %}
{% load app_filters %}

<HTML>

    <HEAD>

        <TITLE>Website title</TITLE>
        <link rel="stylesheet" href="{% static "css/app.css" %}">

    </HEAD>

    <BODY>

        <form method="POST" class="form-inline">{% csrf_token %}
            <input type="date" class="form-control" id="date-input">
            <input type="time" class="form-control" id="time-input">
            <select class="form-control">
            {% for choice in Entry.CATEGORY_CHOICES %}
              <option>{{ choice|get_at_index:1 }}</option>
            {% endfor %}
            </select>
            <input type="text" class="form-control" id="value-input" placeholder="Value">
            <input type="submit" value="OK">
        </form>

    </BODY>

</HTML>

It's a 4 elements list, so {% for choice in Entry.CATEGORY_CHOICES %} should be saving single 2-elements lists (first ('c1','choice 1') then ('c2','choice 2') etc) into variable choice. There I pick the second element of the list with the help od custom filter get_at_index.

#app_filters.py
from django import template

register = template.Library()

@register.filter(name='get_at_index')
def get_at_index(list, index):
    return list[index]

I get no errors, but in the final form, there are no options in select menu. enter image description here

What could be wrong and how to fix it?

#views.py
from django.shortcuts import render
from .forms import EntryForm

def home_page(request):
    form = EntryForm()
    return render(request, 'app/homepage.html', {'form':form})

And forms.py

#forms.py
from django import forms
from .models import Entry

class EntryForm(forms.ModelForm):

    class Meta:
        model = Entry
        fields = ('date','time','category','value',)
9
  • 2
    Can you please share your views.py? This is where you need to send the data down to the template. Commented Aug 26, 2015 at 8:39
  • Any particular reason for not defining the form using Django? Django can even create one for you from the model. Commented Aug 26, 2015 at 8:48
  • @Pynchia I have been unsuccesful with trying to make date field use miniature of calendar for picking up the date. I thought I shall better first make a form how I want it to look like and then try to connect it with models. Commented Aug 26, 2015 at 8:50
  • There's no need to do that. You can use django forms and in the template include a javascript pluging for the date field (e.g. jquery's datepicker, but there are thousands available) Commented Aug 26, 2015 at 8:53
  • 1
    in the template, are you sure the model definition can be reached? (i.e. the name Entry). Please answer @ChristofferKarlsson's first comment Commented Aug 26, 2015 at 8:54

1 Answer 1

1

In your view, you're passing down the form to the template, but you don't use the form in your template the way you're supposed to. To use the form you've passed down, write your form code in the template more like this:

<form method="POST" class="form-inline">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="OK">
</form>

And it will render the entire form for you. You can read more about it in the official documentation: Building a form in Django

Or, if you want to build the form yourself, then you need to send the coices down to your template in your view:

def home_page(request):
    form = EntryForm()
    return render(request, 'app/homepage.html', {
       'form': form,
       'entry_choices': Entry.CATEGORY_CHOICES
    })

And then access it in your template using the key entry_choices.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.