对HTML&表格在这里
http://www.django-rest-framework.org/topics/html-and-forms/
我有以下名为dashboard / widget_detail.html
的模板{%load rest_framework%}
<html><body>
<h1>Profile - {{ widget.name }}</h1>
<form action="{% url 'widget-detail' pk=widget.pk %}" method="POST">
{% csrf_token %}
{% render_form serializer %}
<input type="submit" value="Save">
</form>
</body></html>
以下观点 -
class WidgetDetail(APIView):
renderer_classes = [TemplateHTMLRenderer]
template_name = 'dashboard/widget_detail.html'
def get(self, request, pk=None):
widget= get_object_or_404(Password, pk=pk)
serializer = WidgetSerializer(widget)
return Response({'serializer': serializer, 'widget': widget})
def post(self, request, pk=None):
widget= get_object_or_404(Widget, pk=pk)
serializer = WidgetSerializer(widget)
if not serializer.is_valid():
return Response({'serializer': serializer, 'widget': widget})
return redirect('widget-list')
这些是通过以下网址进行的 -
urlpatterns = [
url(r'^widgets/$', views.WidgetList.as_view()),
url(r'^widgets/(?P<pk>[0-9]+)/$', views.WidgetDetail.as_view()),
]
尝试访问该网址时,我获得了以下追溯 -
Traceback:
File "/opt/enterpass_app/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/enterpass/gui/views.py" in display_base
39. context_instance=RequestContext(request))
File "/opt/enterpass_app/lib/python2.7/site-packages/django/shortcuts.py" in render_to_response
45. using=using)
File "/opt/enterpass_app/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
115. template_name, context, context_instance, dirs, dictionary)
File "/opt/enterpass_app/lib/python2.7/site-packages/django/template/engine.py" in render_to_string
221. return t.render(context_instance)
File "/opt/enterpass_app/lib/python2.7/site-packages/django/template/base.py" in render
209. return self._render(context)
File "/opt/enterpass_app/lib/python2.7/site-packages/django/template/base.py" in _render
201. return self.nodelist.render(context)
File "/opt/enterpass_app/lib/python2.7/site-packages/django/template/base.py" in render
903. bit = self.render_node(node, context)
File "/opt/enterpass_app/lib/python2.7/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/opt/enterpass_app/lib/python2.7/site-packages/django/template/loader_tags.py" in render
159. return template.render(context)
File "/opt/enterpass_app/lib/python2.7/site-packages/django/template/base.py" in render
211. return self._render(context)
File "/opt/enterpass_app/lib/python2.7/site-packages/django/template/base.py" in _render
201. return self.nodelist.render(context)
File "/opt/enterpass_app/lib/python2.7/site-packages/django/template/base.py" in render
903. bit = self.render_node(node, context)
File "/opt/enterpass_app/lib/python2.7/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/opt/enterpass_app/lib/python2.7/site-packages/django/template/defaulttags.py" in render
507. six.reraise(*exc_info)
File "/opt/enterpass_app/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 "/opt/enterpass_app/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse
578. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/opt/enterpass_app/lib/python2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
495. (lookup_view_s, args, kwargs, len(patterns), patterns))
Exception Type: NoReverseMatch at /dashboard/
Exception Value: Reverse for 'widget-detail' with arguments '()' and keyword arguments '{u'pk': ''}' not found. 0 pattern(s) tried: []
我知道它失败了,因为它正在寻找一个pk但是我使用这个表格来发帖,因此PK不会存在。我错过了什么?
答案 0 :(得分:1)
您需要为您的网址命名:
urlpatterns = [
url(r'^widgets/$', views.WidgetList.as_view(), name="widget-list"),
url(r'^widgets/(?P<pk>[0-9]+)/$', views.WidgetDetail.as_view(), name="widget-detail"),
]