我在主urlconf中有这个URL:
url(r'^confirm-invite/(?P<token>[\w-]+)/$', confirm_invite, name="confirm_invite_page"),
这是它连接的(WIP版本)视图:
def confirm_invite(req, token):
template_name = 'invites/confirm_invite.html'
if req.method == 'POST':
form = ActivationForm(req.POST)
if form.is_valid():
try:
auth_user = authenticate(username=user.username, password=form.cleaned_data['activation_code'])
except:
return HttpResponse('didnt authenticate')
if auth_user is None:
return HttpResponse('auth_user is none')
login(req, auth_user)
return HttpResponse('you activated your account')
else:
return HttpResponse('form error foo')
else:
invite = get_object_or_404(Invite, token=token)
user = invite.user
if user.is_active == True:
return HttpResponse('user is already active')
else:
form = ActivationForm()
return render(req, template_name, {'form': form})
这是HTML的片段:
<form action="{% url 'confirm_invite_page' token %}" method="post">{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Activate">
</form>
由于某些原因,HTML中的反向网址{% url 'confirm_invite_page' token %}
会抛出匹配问题
以下是完整的追溯:
Traceback:
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/nir/nirla/nirla/apps/invites/views.py" in confirm_invite
78. return render(req, template_name, {'form': form})
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render
53. return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
169. return t.render(context_instance)
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/template/base.py" in render
140. return self._render(context)
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/template/loader_tags.py" in render
123. return compiled_parent._render(context)
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/template/loader_tags.py" in render
62. result = block.nodelist.render(context)
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/template/defaulttags.py" in render
447. six.reraise(*exc_info)
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/template/defaulttags.py" in render
433. url = reverse(view_name, args=args, kwargs=kwargs, current_app=context.current_app)
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse
532. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
452. (lookup_view_s, args, kwargs, len(patterns), patterns))
Exception Type: NoReverseMatch at /confirm-invite/6f0ce05c8404470bb996f20c8271e363/
Exception Value: Reverse for 'confirm_invite_page' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['confirm-invite/(?P<token>[\\w-]+)/$']
我认为这是正确设置的,考虑到文档,匹配的问题是什么?
答案 0 :(得分:3)
您需要将最后一行更改为
return render(req, template_name, {'form': form, 'token': token})
因为否则,模板没有此变量。
答案 1 :(得分:0)
“令牌”是否?它似乎基于堆栈跟踪。您需要确保将变量设置为某个值,否则url模板标记将失败,因为它将查找匹配,其中url中没有参数,即使您显然也是如此。