Search code examples
djangohttp-redirecthttpresponsereverse

pass argument to view with reverse django


I have a view create_rating where after I submit a form I want it to be processed on a view rating_upload and then i want to redirect back to the create_rating view. Cant seem to get it to work, my latest code below. I would think when i click submit on the create-rating page that it should send video_id to rating_upload, and from there I can just send it back to create_rating as an argument. The docs show this too. I tried several things the latest error is what i have shown..

urls:

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')
]

views:

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))

template, 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">

Latest error:

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

Solution

  • Try

    return HttpResponseRedirect(reverse('create_rating', args=(video_id,)))
    

    instead of

    return HttpResponseRedirect(reverse('create_rating', video_id))
    

    Documentation suggests passing your args as a tuple.