我一直在阅读django书和django文档,但仍然可以弄明白。
我有model.py
:
from django.db import models
from django.forms import ModelForm
class Zonas(models.Model):
name = models.CharField(max_length=30)
def __unicode__(self):
return self.name
class ZonasForm(ModelForm):
class Meta:
model = Zonas
这view.py
:
from django import forms
from testApp.models import Zonas
from django.shortcuts import render_to_response
def menuForm (request):
z = list (Zonas.objects.all())
numbers = forms.CharField(max_length=30,
widget=forms.Select(choices=z))
return render_to_response('hello.html', {'numbers':numbers})
这html
:
<html>
<body>
<form action="" method="get">
<div class="field">
{{ form.numbers }}
</div>
<input type="submit" value="Submit">
</form>
</body>
</html>
回答urls.py
:
from django.conf.urls import patterns, include, url
from testApp.views import menuForm
urlpatterns = patterns('',
url(r'^hello/$', menuForm ),
)
当我运行服务器时,我得到的只是一个带有提交按钮的页面,并且没有任何form.number被认为是一个选择菜单。
我试过这个views.py
:
def menuForm (request):
z = list (Zonas.objects.all())
numbers = forms.ChoiceField(choices=z)
return render_to_response('hello.html', {'numbers':numbers})
但结果是一样的......
任何提示?我应该使用不同的回报吗?
答案 0 :(得分:2)
当您从未将{{ form.numbers }}
变量传递给模板时,您正尝试访问form
。您需要使用numbers
直接访问{{ numbers }}
。
此外,您还没有正确使用表单。看看https://docs.djangoproject.com/en/dev/topics/forms/
创建包含ModelChoiceField
的菜单表单class MenuForm(Form):
zonas = forms.ModelChoiceField(queryset=Zonas.objects.all())
from myapp.forms import MenuForm
def menuForm(request):
if request.method == 'POST': # If the form has been submitted...
form = MenuForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
# ...
return HttpResponseRedirect('/success/') # Redirect after POST
else:
form = MenuForm() # An unbound form
return render(request, 'hello.html', {
'form': form,
})
现在你可以use the form in your template
<html>
<body>
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
</body>
</html>
答案 1 :(得分:1)
如何使用ModelChoiceField
?这段代码适合我。
views.py:
from django import forms
from models import Zonas
from django.shortcuts import render_to_response
class NumbersForm(forms.Form):
numbers = forms.ModelChoiceField(queryset=Zonas.objects.all())
def menuForm(request):
form = NumbersForm()
return render_to_response('hello.html', {'form': form})
hello.html的:
<form action="" method="get">
<div class="field">
{{ form.as_p }}
</div>
<input type="submit" value="Submit">
</form>
</body>
</html>
答案 2 :(得分:1)
您的应用程序中有几个错误。让我们来做以下事情:
您需要testApp
文件夹中的以下文件:models.py
,views.py
,forms.py
。现在,在models.py
内你将定义模型Zonas
(就像你做的那样)
#models.py
from django.db import models
class Zonas(models.Model):
name = models.CharField(max_length=30)
def __unicode__(self):
return self.name
现在,在forms.py
文件中,您将拥有以下内容:
from testApp.models import Zonas
from django.forms import ModelForm
z = ((zona.name,zona.name) for zona in Zonas.objects.all())
class ZonasForm(ModelForm):
name = forms.CharField(max_length=30,widget=forms.Select(choices=z))
class Meta:
model = Zonas
然后,您的views.py
:
from testApp.forms import ZonasForm
from django.shortcuts import render_to_response
def menuForm (request):
if request.method = 'POST':
form = ZonasForm(request.POST)
if form.is_valid():
form.save()
else:
form = ZonasForm()
return render_to_response('hello.html', {'form':form})
你已经处理好你的表格了。
接下来,您的hello.html
:
<form action="" method="post">
{% csrf_token %}
<div class="field">
{{ form.as_p }}
</div>
<input type="submit" value="Submit">
</form>
</body>
所有这些结合应该有效。您的应用定义中存在一些概念错误,我希望这可以作为指导。