如何获取Django模板的类变量

时间:2022-03-30 21:16:53

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.

我刚刚开始创建Django应用程序。我在models.py中有一个类,它有一个类变量CATEGORY_CHOICES,其中包含选项字段的值。

#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.

我想为类Entry的creting instanes制作表单。我有选择菜单的问题,我想从CATEGORY_CHOICES中输入值,但我无法弄清楚如何。

<!-- 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.

这是一个4元素列表,因此{%for Entry.CATEGORY_CHOICES%}应该保存单个2元素列表(第一个('c1','选择1')然后('c2','选择2')等)进入变量选择。在那里,我使用自定义过滤器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. 如何获取Django模板的类变量

我没有错误,但在最终形式中,选择菜单中没有选项。

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

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

class EntryForm(forms.ModelForm):

    class Meta:
        model = Entry
        fields = ('date','time','category','value',)

1 个解决方案

#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

它将为您呈现整个表单。您可以在官方文档中阅读更多相关信息:在Django中构建表单

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

或者,如果您想自己构建表单,则需要将Coices发送到您视图中的模板:

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.

然后使用key entry_choices在模板中访问它。

#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

它将为您呈现整个表单。您可以在官方文档中阅读更多相关信息:在Django中构建表单

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

或者,如果您想自己构建表单,则需要将Coices发送到您视图中的模板:

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.

然后使用key entry_choices在模板中访问它。