我想使用其余框架 api 来呈现来自列表方法的 json 数据和来自模板中帖子的表单。我一直在 rest-framework 文档中搜索渲染器类、Html 渲染和表单渲染,但没有找到解决方案。这是相关代码
//models.py
class Collaborator(models.Model):
user = models.OneToOneField(User,
related_name='collaborator',
on_delete=models.CASCADE
)
area = models.ForeignKey(Area,
verbose_name=_('Área del colaborador'),
related_name='collaborator'
)
position = models.ForeignKey(Position,
verbose_name=_('Cargo del Colaborador'),
related_name='collaborator'
)
photo = models.ImageField(_('Fotografía del colaborador'),
null=True,
blank=True,
help_text=_('Fotografía del Colaborador'),
)
birth_date = models.DateField(_('Fecha de Nacimiento'),
null=True,
blank=True
)
join_date = models.DateField(_('Fecha de Ingreso'),
null=True,
blank=True
)
payment = models.DecimalField(_('Sueldo'),
max_digits=8,
decimal_places=4,
null=True,
blank=True
)
fire_reason = models.TextField(_('Razón de despido / renuncia'),
blank=True,
null=True,
help_text=_('Detallar la razón de por qué salió el colaborador.')
)
class Meta:
ordering = (
'id',
'user',
'area',
'birth_date',
'join_date',
'payment',
)
verbose_name = _('Colaborador')
verbose_name_plural = _('Colaboradores')
def __unicode__(self): # __unicode__ on Python 2
return '%s %s' % (self.user.first_name,
self.user.last_name
)
//serializers.py
class UserSerializer(ModelSerializer):
class Meta:
model = User
fields = (
'username',
'password',
'first_name',
'last_name',
'email',
'groups',
'is_active'
)
class CollaboratorSerializer(ModelSerializer):
user = UserSerializer(many=False)
class Meta:
model = Collaborator
fields = (
'id',
'user',
'photo',
'area',
'position',
'birth_date',
'payment',
'join_date',
'fire_reason',
)
position = StringRelatedField(many=False)
area = StringRelatedField(many=False)
def create(self, validated_data):
user_data = validated_data.pop('user')
user = User.objects.create(**validated_data)
User.objects.create(user=user, **user_data)
return user
//views (rest framework view)
class CollaboratorViewSet(viewsets.ModelViewSet):
"""
A viewset for viewing and editing user instances.
"""
serializer_class = CollaboratorSerializer
queryset = Collaborator.objects.all()
//urls.py
urlpatterns = [
url(r'^collaborators-list/$',
CollaboratorViewSet.as_view({'get':'list','post':'create','delete':'destroy','put':'update'}),
name='collaborators-list'
),
]
这是回溯错误
Traceback (most recent call last):
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\core\handlers\exception.py", line 42, in inner
response = get_response(request)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\core\handlers\base.py", line 217, in _get_response
response = self.process_exception_by_middleware(e, request)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\core\handlers\base.py", line 215, in _get_response
response = response.render()
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\template\response.py", line 109, in render
self.content = self.rendered_content
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\rest_framework\response.py", line 72, in rendered_content
ret = renderer.render(self.data, accepted_media_type, context)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\rest_framework\renderers.py", line 176, in render
return template_render(template, context, request=request)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\rest_framework\compat.py", line 332, in template_render
return template.render(context, request=request)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\template\backends\django.py", line 64, in render
context = make_context(context, request, autoescape=self.backend.engine.autoescape)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\template\context.py", line 267, in make_context
context.push(original_context)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\template\context.py", line 59, in push
return ContextDict(self, *dicts, **kwargs)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\template\context.py", line 18, in __init__
super(ContextDict, self).__init__(*args, **kwargs)
ValueError: dictionary update sequence element #0 has length 9; 2 is required
[27/Apr/2017 08:45:30] "GET /es/gdh/collaborators-list/ HTTP/1.1" 500 107315
编辑 2
从 url {'get':'list','post':'create','delete':'destroy','put':'update'} 中删除它,我收到此错误。
Traceback (most recent call last):
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\core\handlers\exception.py", line 42, in inner
response = get_response(request)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\utils\deprecation.py", line 138, in __call__
response = self.process_response(request, response)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\middleware\clickjacking.py", line 32, in process_response
if response.get('X-Frame-Options') is not None:
AttributeError: 'function' object has no attribute 'get'
[27/Apr/2017 09:47:21] "GET /es/gdh/collaborators-list/ HTTP/1.1" 500 63775