Reputation: 317
I have been reading the django book and the django documentation, but still can figure it out.
i have this 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
this 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})
this html
:
<html>
<body>
<form action="" method="get">
<div class="field">
{{ form.numbers }}
</div>
<input type="submit" value="Submit">
</form>
</body>
</html>
Ans this urls.py
:
from django.conf.urls import patterns, include, url
from testApp.views import menuForm
urlpatterns = patterns('',
url(r'^hello/$', menuForm ),
)
All i get when i run the server is a page only with the submit button, and no form.number wich is supossed to be a select menu.
I tried this views.py
:
def menuForm (request):
z = list (Zonas.objects.all())
numbers = forms.ChoiceField(choices=z)
return render_to_response('hello.html', {'numbers':numbers})
But the result is the same...
Any hints? Should i use a diferent return?
Upvotes: 0
Views: 2513
Reputation: 29794
You have several errors in your applications. Let's do the following:
You will need the following files in your testApp
folder: models.py
, views.py
, forms.py
. Now, inside models.py
you will define model Zonas
(pretty much as you have done)
#models.py
from django.db import models
class Zonas(models.Model):
name = models.CharField(max_length=30)
def __unicode__(self):
return self.name
Now, inside the forms.py
file you will have this:
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
Then, your 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})
There you have your form processed and everything.
Next, your hello.html
:
<form action="" method="post">
{% csrf_token %}
<div class="field">
{{ form.as_p }}
</div>
<input type="submit" value="Submit">
</form>
</body>
All of this combined should work. You had some concept errors in your app definition, I hope this could help as a guide.
Upvotes: 1
Reputation: 11259
You are trying to access {{ form.numbers }}
when you never pass a form
variable to the template. You would need to access numbers
directly with {{ numbers }}
.
Also you aren't quite using forms correctly. Check this out https://docs.djangoproject.com/en/dev/topics/forms/
Create a menu form that contains a ModelChoiceField
class MenuForm(Form):
zonas = forms.ModelChoiceField(queryset=Zonas.objects.all())
Now use that form in your view
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,
})
Now you can 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>
Upvotes: 2
Reputation: 473853
How about using ModelChoiceField
? This code works for me.
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>
Upvotes: 1