views.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))
urls.py
(r'^when/(?P<id>\w+)/$', 'when'),
models.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)
template.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>
谁能告诉我我做错了什么?
答案 0 :(得分:0)
when
函数的参数应为id
而不是pk
。
所以改变
def when(request,pk=id):
到
def when(request,id):
此外,当具有给定id的Report
不存在时,您需要适当地处理案例。
编辑:
您还需要将id
传递给模板,以便表单网址合适。另外,请更新您的观点,如下所示。
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 %}
...