Reputation: 355
I want to make a form with some choises and display it. But in output html I didn't see form itself. Here are my files:
views.py:
from django.http import HttpResponse, Http404
from django.shortcuts import render_to_response
from tasks.todoes.models import Note, Resource,/
File, Worker, Client, Task, ProblemByWorker, ProblemByUser, Categories
from tasks.forms import NewTicketForm
def new_ticket2(request):
method = request.method
if request.method == 'POST':
form = NewTicketForm(request.POST)
if form.is_valid():
pass
else:
form = NewTicketForm()
return render_to_response('new_ticket2.html', {'form':form, 'method':method})
forms.py
from django import forms
from tasks.todoes.models import Note, Resource, File, Worker, Client, Task,/
ProblemByWorker, ProblemByUser, Categories
class NewTicketForm(forms.Form):
name = forms.CharField(max_length=140)
pbus = forms.ChoiceField(choices = ProblemByUser.objects.all())
description = forms.CharField(widget=forms.Textarea)
clients = forms.ChoiceField(choices = Client.objects.all())
priority = forms.ChoiceField(choices = ('1','2','3','4','5'))
category = forms.ChoiceField(choices = Categories.objects.all())
start_date = forms.DateTimeField()
due_date = forms.DateTimeField()
workers = forms.ChoiceField(choices = Worker.objects.all())
percentage = forms.DecimalField(min_value=0, max_value=100)
new_ticket2.html
<html>
<head>
<title>New Ticket</title>
</head>
<body>
{% if form.errors %}
{{form.errors}}
{% endif %}
<p>{{ method }}</p>
<form action="" method="post">
<table>
{{ form.as_table }}
</table>
<input type="submit">
</form>
</body>
in my output I see only "GET" and submit button:
<html><head>
<title>New Ticket</title>
...</head>
<body>
<p>GET</p>
<form action="" method="post">
<table>
</table>
<input type="submit">
</form>
...</body></html>
why so?
Upvotes: 1
Views: 521
Reputation: 5884
You specify choices incorectly. Read the docs about choices
attribute of ChoiceField and about ModelChoiceField.
class NewTicketForm(forms.Form):
PRIORITY_CHOICES = (
('1','1'),
('2','2')
)
name = forms.CharField(max_length=140)
pbus = forms.ModelChoiceField(queryset = ProblemByUser.objects.all())
description = forms.CharField(widget=forms.Textarea)
clients = forms.ModelChoiceField(queryset = Client.objects.all())
priority = forms.ChoiceField(choices = PRIORITY_CHOICES)
category = forms.ModelChoiceField(queryset = Categories.objects.all())
start_date = forms.DateTimeField()
due_date = forms.DateTimeField()
workers = forms.ModelChoiceField(queryset = Worker.objects.all())
percentage = forms.DecimalField(min_value=0, max_value=100)
It seems like improperly configured ChoiceFields
fail silently. Hope, someone on stackoverflow will explain to us why.
Upvotes: 3