Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 614
ratrace123 Avatar asked Feb 18 '17 05:02

ratrace123


People also ask

How do I reverse in Django?

reverse() If you need to use something similar to the url template tag in your code, Django provides the following function: reverse (viewname, urlconf=None, args=None, kwargs=None, current_app=None)

What is HttpResponseRedirect reverse?

HttpResponseRedirect is a subclass of HttpResponse (source code) in the Django web framework that returns the HTTP 302 status code, indicating the URL resource was found but temporarily moved to a different URL. This class is most frequently used as a return object from a Django view.

What is reverse match in Django?

The NoReverseMatch error is saying that Django cannot find a matching url pattern for the url you've provided in any of your installed app's urls. The NoReverseMatch exception is raised by django. core. urlresolvers when a matching URL in your URLconf cannot be identified based on the parameters supplied.

What is the difference between reverse and redirect in Django?

The most basic difference between the two is : Redirect Method will redirect you to a specific route in General. Reverse Method will return the complete URL to that route as a String.


1 Answers

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.

like image 80
mxle Avatar answered Oct 19 '22 18:10

mxle