我正在尝试使用此处找到的示例 - http://www.django-rest-framework.org/topics/html-and-forms/
我真的从示例中剪切/粘贴将其更改为我自己的模型 -
我通过base.html中的以下代码访问表单 -
{% include 'dashboard/widget_detail.html' %}
widget_details.html
{% load rest_framework %}
<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>
views.py
class WidgetDetail(APIView):
renderer_classes = [TemplateHTMLRenderer]
template_name = 'dashboard/widget_detail.html'
def get(self, request, pk):
widget= get_object_or_404(Widget, pk=pk)
serializer = WidgetSerializer(widget)
return Response({'serializer': serializer, 'widget': widget})
def post(self, request, pk):
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')
尝试加载我的base.html的网址时,我得到以下回溯 -
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
37. 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. 2 pattern(s) tried: [u'api/widgets/(?P<pk>[^/.]+)\\.(?P<format>[a-z0-9]+)/?$', u'api/widgets/(?P<pk>[^/.]+)/$']
我不知道我错过了什么/做错了什么。
我可以访问http://localhost/api/widgets/5
没问题(例如)
更新 - 如果我从表单中删除PK值 -
<form action="{% url 'api:widget-detail' %}" method="POST">
{% csrf_token %}
{% render_form serializer %}
<input type="submit" value="Save">
</form>
我收到以下错误 -
'str' object has no attribute 'data'
这是有道理的,因为我还没有数据。我想要的只是一个空白表单,使用我的序列化程序作为后端来填写和“保存”。这有多难?!
包括我的urls.py,它来自 -
router = routers.DefaultRouter()
router.register(r'widgets', views.WidgetViewSet)
urlpatterns = [
url(r'^', include(router.urls)),
]
答案 0 :(得分:1)
错误消息为您提供线索;它说它使用空字符串作为pk
参数。这意味着由于某种原因,widget.pk
正在评估为空。