0

我在views.py 中使用以下代码

def when(request):
    log.debug("test....")

    reportform = ReportForm()
    if request.method == 'POST':
        if reportform.is_valid():
            log.debug("test:%s",reportform)
            reportform = reportform.save()

    return render_to_response('incident/when.html',{'newreport_menu': True,
    'form': reportform },context_instance=RequestContext(request))

我的模板是

{{ form.manual_date_time }}

    <p id=ir-nextbutn><a href="{% url incident.views.media %}"> 
    <form action="/member/media/" method="POST"> {% csrf_token %}
    {% include "buttons/next.html" %}
      </form>

模型.py

class Report(models.Model):
   user = models.ForeignKey(User, null=False)
    incident_number = models.CharField('Incident Number', max_length=100)
    device_id = models.CharField('Device Id', max_length=100)
    app_uuid = models.CharField('Unique App Id', max_length=100)
    created_date_time = models.DateTimeField('Created')
    manual_date_time = models.DateTimeField('Another time', null=True, blank=True)
    sent_date_time = models.DateTimeField('Sent')
    incident_description = models.TextField('Incident description', null=True, blank=True)

在这里,我将日期时间字段保存在数据库中。如果用户单击下一步按钮保存并转到下一页,则在字段中输入时间和日期后。但它不存储数据。我使用的是 MySql db。

4

2 回答 2

0

您没有将任何数据应用于表单,因此它无效。

def when(request):

    if request.method == 'POST':
        # Form with POST'ed data
        reportform = ReportForm(
            data=request.POST,
        )

        if reportform.is_valid():
            log.debug("test:%s",reportform)
            reportform = reportform.save()
            # You should redirect to a different page at this point
            # This will avoid double POST'ing and saving
            redirect_to = request.POST.get(
                'next',
                reverse('name-of-url-defined-in-your-urls.py')
            )
            return HttpResponseRedirect(redirect_to)
        else:
            # Check the errors, they *should* appear when the form is rendered
            pass
    else:
        # Blank form, no data
        reportform = ReportForm()

    return render_to_response(
        'incident/when.html',
        {
            'newreport_menu': True, 
            'form': reportform,
        },
        context_instance=RequestContext(request)
    )

更新:

此外,您的表单不包含在form标签中。你的“下一步”按钮需要是一个inputbuttontype="submit"

<form action="/member/media/" method="POST"> {% csrf_token %}
    <input name="next" type="hidden" value="{{ url_to_redirect_to }}"/>
    {{ form }}
    <input type="submit" name="save" value="save"/>
        OR
    <button type="submit">Save</button>
</form>

边注:

您可能可以添加auto_now_add=Truecreated_date_time以避免您必须自己设置它并停止它被编辑并出现在ModelForms

created_date_time = models.DateTimeField('Created', auto_now_add=True,)
于 2013-05-02T11:58:22.753 回答
0

在我看来,您没有在表单中填充用户字段。Report 模型将 user 作为非空外键,因此这可能会给您带来麻烦。您需要显式添加表单未填充的字段,然后保存:

if reportform.is_valid():
    log.debug("test:%s",reportform)
    report = reportform.save(commit=False)
    report.user = request.user  #assuming a login_required decorator
    report.incident_number = ...
    ....
    report.save()

因此,我们使用表单提供的字段从表单创建报表对象。由于报告对象仍然不完整,我们现在不提交。然后我们添加剩余的字段并保存。

于 2013-05-02T11:47:27.777 回答