0

I am trying to call a view recursively. Below is my code.

views.py

def survey_start(request):
   post = models.Post.objects.all().order_by('id')
   return HttpResponseRedirect(reverse('post_form_upload',args=(post[0].id,)))

def post_form_upload(request, id):  
   post = get_object_or_404(models.Post, id=id)

   if request.method == 'GET': 
       content = post.content 
       form = CommentModelForm(content = content)
   else:
       form = CommentModelForm(request.POST)

       if form.is_valid():
           message = form.cleaned_data['message']
           created_at = form.cleaned_data['created_at']
           post1 = models.Comment.objects.create(post = id,
                                        message = message,
                                        created_at = created_at)

           return HttpResponseRedirect(reverse('post_form_upload',
                                            args= (post.next_id,)))
   return render(request, 'survey_forms/post_form_upload.html',{
    'form':form,
    })

urls.py

urlpatterns = [ 
   url(r'^post/form_upload.html$',views.survey_start, name='survey_start'),
   url(r'^post/(?P<id>\d+)/post_form_upload.html$',views.post_form_upload,  name='post_form_upload'),
   ]

I have two questions.
1. I keep on getting this error. It does enter the view. I printed a counter to check.

Reverse for 'post_form_upload' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['survey_forms/post/(?P<id>\\d+)/post_form_upload.html$']

Here is the traceback:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/survey_forms/post/1/post_form_upload.html

Django Version: 1.8.3
Python Version: 2.7.8
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'survey',
 'dynamic_forms',
 'crispy_forms',
 'formtools',
 'survey_forms')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'dynamic_forms.middlewares.FormModelMiddleware')


Template error:
In template         /Users/Desktop/VtStudy/python/django/2dj_proto/mysurvey/survey_forms/templates/survey_forms/post_form_upload.html, error at line 1
   Reverse for 'post_form_upload' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['survey_forms/post/(?P<id>\\d+)/post_form_upload.html$']

   1 : <form action=" {% url 'post_form_upload' %} "  method='post'>

   2 : {% csrf_token %}

   3 : 

   4 : {{form.as_p}}

   5 : <input type='submit' value='Submit'/>

   6 : </form>

Traceback:
File "/Users/.virtualenvs/2dj_proto/lib/python2.7/site-    packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request,     *callback_args,     **callback_kwargs)
    File         "/Users/Desktop/VtStudy/python/django/2dj_proto/mysurvey/survey_forms/views.py" in         post_form_upload
  132.      'form':form,
    File "/Users/.virtualenvs/2dj_proto/lib/python2.7/site-    packages/django/shortcuts.py" in render
  67.             template_name, context, request=request, using=using)
    File "/Users/.virtualenvs/2dj_proto/lib/python2.7/site-    packages/django/template/loader.py" in render_to_string
  99.         return template.render(context, request)
    File "/Users/.virtualenvs/2dj_proto/lib/python2.7/site-packages/django/template/backends/django.py" in render
  74.         return self.template.render(context)
    File "/Users/.virtualenvs/2dj_proto/lib/python2.7/site-    packages/django/template/base.py" in render
  209.                     return self._render(context)
    File "/Users/.virtualenvs/2dj_proto/lib/python2.7/site-    packages/django/template/base.py" in _render
  201.         return self.nodelist.render(context)
    File "/Users/.virtualenvs/2dj_proto/lib/python2.7/site-packages/django/template/base.py" in render
  903.                 bit = self.render_node(node, context)
    File "/Users/.virtualenvs/2dj_proto/lib/python2.7/site-    packages/django/template/debug.py" in render_node
  79.             return node.render(context)
    File "/Users/.virtualenvs/2dj_proto/lib/python2.7/site-    packages/django/template/defaulttags.py" in render
  507.                         six.reraise(*exc_info)
File "/Users/.virtualenvs/2dj_proto/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  493.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/Users/.virtualenvs/2dj_proto/lib/python2.7/site-    packages/django/core/urlresolvers.py" in reverse
  579.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/Users/.virtualenvs/2dj_proto/lib/python2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
  496.                              (lookup_view_s, args, kwargs, len(patterns), patterns))

Exception Type: NoReverseMatch at /survey_forms/post/1/post_form_upload.html
Exception Value: Reverse for 'post_form_upload' with arguments '()' and     keyword arguments '{}' not found. 1 pattern(s) tried: ['survey_forms/post/(?    P<id>\\d+)/post_form_upload.html$']
  1. Each post is displayed in one single page. The user must navigate from one page to other on clicking "next". So I am calling this view post_upload_form recursively. Is this the right approach?

Thanks.

ac11
  • 927
  • 2
  • 11
  • 18

1 Answers1

0

For first point: problem is in the template:

<form action=" {% url 'post_form_upload' %} "  method='post'>

post_form_upload in your urls.py takes one non-empty argument, but you're not providing that in above line.

For second point: this is not an recursion, technically speaking. You are just redirecting user to next url, which is handled by same view function. And it is right approach, there is nothing wrong with that.

GwynBleidD
  • 20,081
  • 5
  • 46
  • 77
  • Could you suggest how I can pass an argument to templates? I tried this
    but this isn't working.
    – ac11 Aug 19 '15 at 16:04
  • I tried this
    . It worked. And in render def_form_upload() in views.py, I am passing the argument.
    – ac11 Aug 19 '15 at 20:23
  • But the redirection is not happening. Any idea why? – ac11 Aug 20 '15 at 03:04
  • My bad! Redirection is fine the problem is somewhere else. I'll figure that out. Anyways thanks. – ac11 Aug 20 '15 at 17:22