标签:post ali ret erb float creat 函数 增加 mod
1、路由配置
url(r'^(\w+)/(\w+)/add$', views.add_obj, name='add_obj')
2、视图函数
def add_obj(request, app_name, model_name):
"""增加新对象 """
admin_class = site.enable_admins[app_name][model_name]
dynamic_form = create_model_form(admin_class, add=True)
if request.method == 'GET':
form_obj = dynamic_form()
return render(request, 'kingadmin/add_obj.html', locals())
elif request.method == 'POST':
form_obj = dynamic_form(data=request.POST)
if form_obj.is_valid():
form_obj.save()
return redirect('/kingadmin/%s/%s' % (app_name, model_name))
errors = form_obj.errors
return render(request, 'kingadmin/add_obj.html', locals())
3、前端页面
{% extends 'kingadmin/change_info.html' %}
{% block readonly_fields_display %}
{% endblock %}
{% block delete_button %}
{% endblock %}
4、页面新增按钮
<a href="{% url 'add_obj' app_name model_name %}" style="float: right">
<button type="submit" class="btn btn-default form_btn">ADD {% get_names admin_class %}
<span class="glyphicon glyphicon-plus"></span></button>
</a>
@register.simple_tag
def get_names(admin_class):
"""获取表名,有中文则获取中文显示"""
model_class = admin_class.model
if model_class._meta.verbose_name:
return model_class._meta.verbose_name
return model_class._meta.model_name
标签:post ali ret erb float creat 函数 增加 mod
原文地址:https://www.cnblogs.com/fqh202/p/9461716.html