1

我想用一些选择制作一个表格并显示它。但是在输出 html 中我没有看到表单本身。这是我的文件:

视图.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})

表格.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>

在我的输出中,我只看到“GET”和提交按钮:

<html><head>
    <title>New Ticket</title>
...</head>
<body>

    <p>GET</p>
    <form action="" method="post">
        <table>

        </table>
        <input type="submit">
    </form>
...</body></html>

为什么这样?

4

1 回答 1

3

您错误地指定了选项。阅读有关ChoiceFieldchoices属性和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)

似乎配置不正确ChoiceFields会默默地失败。希望stackoverflow上的人会向我们解释原因。

于 2012-10-14T12:55:44.147 回答