[英]Drop down menu not showing with Django
I am a beginner in Django, I tried making a drop down menu and select one option.我是 Django 的初学者,我尝试制作一个下拉菜单并选择一个选项。 I am not able to do it.
我做不到。 Please could you help my out with it.
请你能帮我解决这个问题吗?
forms.py表格.py
from django import forms
from .models import website, PresetList
class PresetListForm(forms.ModelForm):
class Meta:
model = PresetList
exclude=[]
models.py模型.py
from django.db import models
from django.utils.encoding import smart_unicode
from django import forms
class PresetList(models.Model):
VIEWS = (
('1', 'X'),
('2', 'Y'),
)
query_choice = forms.ChoiceField(choices=VIEWS)
#code
views.py视图.py
def my_view(request):
preset_form = PresetListForm()
return render_to_response('signup.html',{'preset_form': preset_form},RequestContext(request))
signup.html注册.html
<html>
<head>
</head>
<body>
<form action="" method="POST"> {% csrf_token %} {{ preset_form.as_p }} </form>
</body>
</html>
Probably I figured out what is wrong with your code.可能我发现你的代码有什么问题。 You are using
forms.ChoiceField()
to define model field what is not correct.您正在使用
forms.ChoiceField()
来定义不正确的模型字段。 You should use models.CharField()
instead:您应该使用
models.CharField()
代替:
query_choice = models.CharField(max_length=1, choices=VIEWS)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.