0

视图.py

def when(request,pk=id):

    if request.method == 'POST':

        reportform = ReportForm(data=request.POST)

        if reportform.is_valid():
            log.debug("test:%s",reportform)
            report = Report.objects.get(pk=id)
            reportform=ReportForm(instance=report)
            report = reportform.save(commit=False)
            report.user = request.user
            report.save()
            return redirect('/member/media/')
    else:
        report=Report.objects.get(pk=id)
        reportform = ReportForm(instance=report)
    return render_to_response('incident/when.html',{
                  'newreport_menu': True, 
                  'form': reportform,
                   },
                   context_instance=RequestContext(request))

网址.py

(r'^when/(?P<id>\w+)/$', 'when'),

模型.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',auto_now=True)
    manual_date = models.DateField('manual date', null=True, blank=True)
    manual_time = models.TimeField('manual time',null=True, blank=True)

上面的代码用于执行编辑操作。我遇到了很多错误,例如:"when() got an unexpected keyword argument 'id'"

追溯

TypeError at /member/when/
when() takes exactly 2 arguments (1 given)
Request Method: GET
Request URL:    http://192.168.100.10/member/when/
Django Version: 1.3.7
Exception Type: TypeError
Exception Value:    
when() takes exactly 2 arguments (1 given)
Exception Location: /usr/lib/python2.7/site-packages/django/core/handlers/base.py in get_response, line 111
Python Executable:  /usr/bin/python
Python Version: 2.7.0
Python Path:    
['/root/Projects/ir',
 '/usr/lib/python2.7/site-packages/distribute-0.6.28-py2.7.egg',
 '/usr/lib/python2.7/site-packages/parse-1.6.1-py2.7.egg',
 '/usr/lib/python2.7/site-packages/django_testscenarios-0.7.2-py2.7.egg',
 '/usr/lib/python2.7/site-packages/django_testproject-0.1.1-py2.7.egg',
 '/usr/lib/python2.7/site-packages/extras-0.0.3-py2.7.egg',
 '/usr/lib/python2.7/site-packages/mechanize-0.2.5-py2.7.egg',
 '/usr/lib/python2.7/site-packages/wsgi_intercept-0.5.1-py2.7.egg',
 '/usr/lib/python2.7/site-packages/Jinja2-2.6-py2.7.egg',
 '/usr/lib/python2.7/site-packages/Pygments-1.6-py2.7.egg',
 '/usr/lib/python2.7/site-packages/Cython-0.18-py2.7-linux-i686.egg',
 '/usr/lib/python27.zip',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7/site-packages',
 '/usr/lib/python2.7/site-packages/PIL',
 '/usr/lib/python2.7/site-packages/gst-0.10',
 '/usr/lib/python2.7/site-packages/gtk-2.0',
 '/usr/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info',
 '/usr/lib/python2.7/site-packages/webkit-1.0']
Server time:    Sat, 4 May 2013 16:29:44 +1000
Traceback Switch to copy-and-paste view

/usr/lib/python2.7/site-packages/django/core/handlers/base.py in get_response
                        response = callback(request, *callback_args, **callback_kwargs) 

模板.html

<form action="/member/when/" method="POST"> {% csrf_token %}

{{ form.manual_time.errors }}
{{ form.manual_date }} {{ form.manual_time }}
<p id=ir-nextbutn><a href="{% url incident.views.media %}"> 
{% include "buttons/next.html" %}</form>

谁能告诉我我做错了什么?

4

1 回答 1

0

when函数应该有名为idnot的参数pk

所以改变

def when(request,pk=id):

def when(request,id):

Report此外,当给定的 id 不存在时,您需要适当地处理大小写。


编辑:

您还需要传递id给模板,以便表单 url 是合适的。此外,针对其他问题更新您的视图,如下所示。

def when(request,id):
    try:
        report = Report.objects.get(pk=id)
    except ObjectDoesNotExists:
        #handle when report does not exists

    if request.method == 'POST':

        reportform = ReportForm(data=request.POST, instance=report)

        if reportform.is_valid():
            log.debug("test:%s",reportform)

            report = reportform.save(commit=False)
            report.user = request.user
            report.save()
            return redirect('/member/media/')
    else:
        reportform = ReportForm(instance=report)
    return render_to_response('incident/when.html',{
                  'newreport_menu': True, 
                  'form': reportform,
                  'report_id': id,        #pass id too
                   },
                   context_instance=RequestContext(request))

模板:

<form action="/member/when/{{report_id}}" method="POST"> {% csrf_token %}
...
于 2013-05-04T06:19:14.827 回答