我有一个视图create_rating
,在我提交表单后,我希望它在视图rating_upload
上进行处理,然后我想重定向回create_rating
视图。似乎无法使用它,我的最新代码如下。我想当我点击submit
页面上的create-rating
时,它应该video_id
发送给rating_upload
,然后我可以将其发送回create_rating
作为一个论点。文档也显示了这一点。我尝试了几件事,最新的错误就是我所展示的......
网址:
urlpatterns = [
url(r'^upload', UploadVideo.as_view(), name='upload'),
url(r'^(?P<pk>[0-9]+)/$', VideoView.as_view(), name='videoview'),
url(r'^(?P<video_id>\d+)/create_rating', create_rating, name='create_rating'),
url(r'^(?P<video_id>\d+)/rating_upload', rating_upload, name='rating_upload'),
url(r'^(?P<video_id>\d+)/rating_uploaded', rating_upload, name='rating_upload')
]
观点:
def create_rating(request, video_id):
vid = get_object_or_404(Video, pk=video_id)
past_ratings = vid.rating.order_by('date_created')[:5]
template = loader.get_template('create_rating.html')
context = {
'vid': vid, 'past_ratings': past_ratings
}
return HttpResponse(template.render(context, request))
def rating_upload(request, video_id):
template = loader.get_template('rating_upload.html')
rated_video = Video.objects.get(pk=video_id)
context = {
'rated_video': rated_video
}
return HttpResponseRedirect(reverse('create_rating', video_id))
模板,create_rating.html
:
<p>{{ vid.title }}</p>
<form action="{% url 'rating_upload' vid.pk %}" method="post">
{% csrf_token %}
<input type="text" name="rate_comment">
<input type="submit" value="Rate Video">
最新错误:
Request Method: POST
Request URL: http://127.0.0.1:8000/video/32/rating_uploaded
Django Version: 1.10.5
Python Version: 2.7.10
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'video']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/Users/RyanHelling/virtualenvs/env1/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/Users/RyanHelling/virtualenvs/env1/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/Users/RyanHelling/virtualenvs/env1/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/RyanHelling/PycharmProjects/flash2/video/views.py" in rating_upload
63. return HttpResponseRedirect(reverse('create_rating', video_id))
Exception Type: TypeError at /video/32/rating_uploaded
Exception Value: an integer is required
答案 0 :(得分:6)
尝试
return HttpResponseRedirect(reverse('create_rating', args=(video_id,)))
而不是
return HttpResponseRedirect(reverse('create_rating', video_id))
Documentation建议将你的args作为元组传递。