Django-不使用表单或模型表单时在模板中呈现模型选项

Django-不使用表单或模型表单时在模板中呈现模型选项,django,django-templates,forms,Django,Django Templates,Forms,我创建了一个模型,其中一个字段有选择。我在模板中创建了一个select表单,但没有显示选项。我不使用表单或模型表单有几个原因。但我的理解是,我应该能够使用模型中的选项来完成这项工作,在模板中构建表单,并使用对象管理器保存信息。如何获得填充表单的选项 Models.py class NewRating(models.Model): EXCELLENT = 'EX' GOOD = 'GD' FAIR = 'FR' BAD = 'BD' RATING_CHOICES = ( (EXCELLE

我创建了一个模型,其中一个字段有选择。我在模板中创建了一个select表单,但没有显示选项。我不使用表单或模型表单有几个原因。但我的理解是,我应该能够使用模型中的选项来完成这项工作,在模板中构建表单,并使用对象管理器保存信息。如何获得填充表单的选项

Models.py

class NewRating(models.Model):

EXCELLENT = 'EX'
GOOD = 'GD'
FAIR = 'FR'
BAD = 'BD'
RATING_CHOICES = (
    (EXCELLENT, 'Excellent'),
    (GOOD, 'Good'),
    (FAIR, 'Fair'),
    (BAD, 'Bad')
)
function = models.ForeignKey(Function, related_name='frating')
timeline = models.ForeignKey(Timeline, related_name='trating')
rating = models.CharField(max_length=25,choices=RATING_CHOICES)
Views.py

def f_page(request, Function_id):
assignments = Function.objects.get(id=Function_id)
time = Timeline.objects.all()
ratings = NewRating.objects.all()

context = {
    'assignments': assignments,
    'time' : time,
    'ratings' : ratings,
}
return render (request, 'project/pager.html', context)
HTML


{时间百分比中的额定值为%}
{%csrf_令牌%}
{{rated.segment}
从列表中选择
{评级中的评级为%}
{{rating.choices}}
{%endfor%}
{%endfor%}
正在使用{%进行分级,分级为%} {{rating.choices}}
{%endfor%}不起作用。如果我正在构建自己的表单,我可以在模型中设置选项吗?如果是,这不是渲染,我做错了什么

如果您严格不愿意使用
Form
ModelForm
,则需要通过
enum
进行可移植的选择。 您可以查看我的示例代码,了解如何使用枚举实现选择

完成此操作后,您必须对上下文和模板进行一些调整。 你可能想看看。

试试这个

{% for rating in ratings %}
<select name="ratings">
    <option value="">Choose From List</option>
    {% for x, y in rating._meta.get_field('rating').choices %}
        <option value="{{ x }}">{% if rating.rating == x %} selected{% endif %}> 
            {{ y }}
        </option>
    {% endfor %}
</select>
{% endfor %}

最简单、最简单且100%的工作方法是:

获取对象
[比如说'my_Model_with_choices']
使用
my_Model_with_choices=yourModel.objects.first()
可以使用
my_Model_with_choices.\u meta.Get_字段('your_foreign_key_variable_name')。选项

def f_page(request, Function_id):
     assignments = Function.objects.get(id=Function_id)
     ratings = NewRating.RATING_CHOICES

context = {
    'ratings' : ratings,
}
return render (request, 'project/pager.html', context)
内部HTML模板:

<select class="custom-select col-md-5" name="ratings" id="ratings" required>
    <option disabled selected value="">Ethnic Group</option>
    {% for value, ratings_group in ratings  %}
    <option value='{{value}}'>{{ratings_group}}</option>
    {% endfor %}
</select>

民族
{%表示值,评级组位于评级%}
{{u群}}
{%endfor%}

选项
不是您的
新评级
型号的属性。您应该在模板中显示
{{rating.rating}}
,但它不起作用。向下滚动时没有选择。你让事情变得比需要的困难得多。没有什么好的理由不使用表单。
def f_page(request, Function_id):
     assignments = Function.objects.get(id=Function_id)
     ratings = NewRating.RATING_CHOICES

context = {
    'ratings' : ratings,
}
return render (request, 'project/pager.html', context)
<select class="custom-select col-md-5" name="ratings" id="ratings" required>
    <option disabled selected value="">Ethnic Group</option>
    {% for value, ratings_group in ratings  %}
    <option value='{{value}}'>{{ratings_group}}</option>
    {% endfor %}
</select>