mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
Reorganize to use template_pack in form rendering
This commit is contained in:
parent
e558f806c0
commit
cc1c423255
|
@ -339,7 +339,6 @@ class HTMLFormRenderer(BaseRenderer):
|
||||||
"""
|
"""
|
||||||
media_type = 'text/html'
|
media_type = 'text/html'
|
||||||
format = 'form'
|
format = 'form'
|
||||||
template = 'rest_framework/form.html'
|
|
||||||
charset = 'utf-8'
|
charset = 'utf-8'
|
||||||
|
|
||||||
field_templates = ClassLookupDict({
|
field_templates = ClassLookupDict({
|
||||||
|
@ -383,26 +382,21 @@ class HTMLFormRenderer(BaseRenderer):
|
||||||
serializers.TimeField: 'time',
|
serializers.TimeField: 'time',
|
||||||
})
|
})
|
||||||
|
|
||||||
def render_field(self, field, layout=None):
|
def render_field(self, field, template_pack=None):
|
||||||
layout = layout or 'vertical'
|
|
||||||
style_type = field.style.get('type', 'default')
|
style_type = field.style.get('type', 'default')
|
||||||
if style_type == 'textarea' and layout == 'inline':
|
|
||||||
style_type = 'default'
|
|
||||||
|
|
||||||
input_type = self.input_type[field]
|
input_type = self.input_type[field]
|
||||||
if input_type == 'datetime-local' and isinstance(field.value, six.text_type):
|
if input_type == 'datetime-local' and isinstance(field.value, six.text_type):
|
||||||
field.value = field.value.rstrip('Z')
|
field.value = field.value.rstrip('Z')
|
||||||
|
|
||||||
base = self.field_templates[field][style_type]
|
base = self.field_templates[field][style_type]
|
||||||
template_name = 'rest_framework/fields/' + layout + '/' + base
|
template_name = template_pack + '/fields/' + base
|
||||||
template = loader.get_template(template_name)
|
template = loader.get_template(template_name)
|
||||||
context = Context({
|
context = Context({
|
||||||
'field': field,
|
'field': field,
|
||||||
'input_type': input_type,
|
'input_type': input_type,
|
||||||
'renderer': self,
|
'renderer': self,
|
||||||
'layout': layout
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return template.render(context)
|
return template.render(context)
|
||||||
|
|
||||||
def render(self, data, accepted_media_type=None, renderer_context=None):
|
def render(self, data, accepted_media_type=None, renderer_context=None):
|
||||||
|
@ -411,11 +405,10 @@ class HTMLFormRenderer(BaseRenderer):
|
||||||
"""
|
"""
|
||||||
renderer_context = renderer_context or {}
|
renderer_context = renderer_context or {}
|
||||||
request = renderer_context['request']
|
request = renderer_context['request']
|
||||||
|
template = loader.get_template('rest_framework/horizontal/form.html')
|
||||||
template = loader.get_template(self.template)
|
|
||||||
context = RequestContext(request, {
|
context = RequestContext(request, {
|
||||||
'form': data.serializer,
|
'form': data.serializer,
|
||||||
'layout': getattr(getattr(data, 'Meta', None), 'layout', 'horizontal'),
|
'template_pack': 'rest_framework/horizontal',
|
||||||
'renderer': self
|
'renderer': self
|
||||||
})
|
})
|
||||||
return template.render(context)
|
return template.render(context)
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
<div class="form-group">
|
|
||||||
{% if field.label %}
|
|
||||||
<label class="sr-only">{{ field.label }}</label>
|
|
||||||
{% endif %}
|
|
||||||
<textarea name="{{ field.name }}" class="form-control" {% if field.style.placeholder %}placeholder="{{ field.style.placeholder }}"{% endif %} {% if field.style.rows %}rows="{{ field.style.rows }}"{% endif %}>{% if field.value %}{{ field.value }}{% endif %}</textarea>
|
|
||||||
</div>
|
|
|
@ -1,33 +0,0 @@
|
||||||
<!-- <html>
|
|
||||||
<head>
|
|
||||||
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div class="container">
|
|
||||||
|
|
||||||
<h1>User update</h1>
|
|
||||||
<div class="well"> -->
|
|
||||||
|
|
||||||
{% load rest_framework %}
|
|
||||||
<form {% if layout == "inline" %}class="form-inline"{% elif layout == "horizontal" %}class="form-horizontal"{% endif %} role="form" action="." method="POST">
|
|
||||||
{% csrf_token %}
|
|
||||||
{% for field in form %}
|
|
||||||
{% if not field.read_only %}
|
|
||||||
{% render_field field layout=layout renderer=renderer %}
|
|
||||||
{% endif %}
|
|
||||||
{% endfor %}
|
|
||||||
<!-- form.non_field_errors -->
|
|
||||||
{% if layout == "horizontal" %}
|
|
||||||
<div class="form-group">
|
|
||||||
<div class="col-sm-offset-2 col-sm-10">
|
|
||||||
<button type="submit" class="btn btn-default">Submit</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
{% else %}
|
|
||||||
<button type="submit" class="btn btn-default">Submit</button>
|
|
||||||
{% endif %}
|
|
||||||
</form>
|
|
||||||
<!--
|
|
||||||
</div>
|
|
||||||
</div></body>
|
|
||||||
</html> -->
|
|
15
rest_framework/templates/rest_framework/horizontal/form.html
Normal file
15
rest_framework/templates/rest_framework/horizontal/form.html
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
{% load rest_framework %}
|
||||||
|
<form class="form-horizontal" role="form" action="." method="POST">
|
||||||
|
{% csrf_token %}
|
||||||
|
{% for field in form %}
|
||||||
|
{% if not field.read_only %}
|
||||||
|
{% render_field field template_pack=template_pack renderer=renderer %}
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
|
<!-- form.non_field_errors -->
|
||||||
|
<div class="form-group">
|
||||||
|
<div class="col-sm-offset-2 col-sm-10">
|
||||||
|
<button type="submit" class="btn btn-default">Submit</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<div class="form-group">
|
||||||
|
{% if field.label %}
|
||||||
|
<label class="sr-only">{{ field.label }}</label>
|
||||||
|
{% endif %}
|
||||||
|
<input name="{{ field.name }}" type="{{ input_type }}" class="form-control" {% if field.style.placeholder %}placeholder="{{ field.style.placeholder }}"{% endif %} {% if field.value %}value="{{ field.value }}"{% endif %}>
|
||||||
|
</div>
|
11
rest_framework/templates/rest_framework/inline/form.html
Normal file
11
rest_framework/templates/rest_framework/inline/form.html
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
{% load rest_framework %}
|
||||||
|
<form class="form-inline" role="form" action="." method="POST">
|
||||||
|
{% csrf_token %}
|
||||||
|
{% for field in form %}
|
||||||
|
{% if not field.read_only %}
|
||||||
|
{% render_field field template_pack=template_pack renderer=renderer %}
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
|
<!-- form.non_field_errors -->
|
||||||
|
<button type="submit" class="btn btn-default">Submit</button>
|
||||||
|
</form>
|
11
rest_framework/templates/rest_framework/vertical/form.html
Normal file
11
rest_framework/templates/rest_framework/vertical/form.html
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
{% load rest_framework %}
|
||||||
|
<form role="form" action="." method="POST">
|
||||||
|
{% csrf_token %}
|
||||||
|
{% for field in form %}
|
||||||
|
{% if not field.read_only %}
|
||||||
|
{% render_field field template_pack=template_pack renderer=renderer %}
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
|
<!-- form.non_field_errors -->
|
||||||
|
<button type="submit" class="btn btn-default">Submit</button>
|
||||||
|
</form>
|
|
@ -32,8 +32,8 @@ class_re = re.compile(r'(?<=class=["\'])(.*)(?=["\'])')
|
||||||
# And the template tags themselves...
|
# And the template tags themselves...
|
||||||
|
|
||||||
@register.simple_tag
|
@register.simple_tag
|
||||||
def render_field(field, layout=None, renderer=None):
|
def render_field(field, template_pack=None, renderer=None):
|
||||||
return renderer.render_field(field, layout)
|
return renderer.render_field(field, template_pack)
|
||||||
|
|
||||||
|
|
||||||
@register.simple_tag
|
@register.simple_tag
|
||||||
|
|
Loading…
Reference in New Issue
Block a user