我试图以递归方式调用视图。以下是我的代码。
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'),
]
我有两个问题 我继续犯这个错误。它确实进入了视图。我打印了一个柜台进行检查。
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$']
这是追溯:
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$']
感谢。
答案 0 :(得分:0)
第一点:问题出在模板中:
<form action=" {% url 'post_form_upload' %} " method='post'>
你的urls.py中的 post_form_upload
需要一个非空的参数,但是你并没有在上面提供这个参数。
第二点:从技术上讲,这不是递归。您只是将用户重定向到下一个URL,该URL由相同的视图功能处理。这是正确的方法,没有任何问题。