我如何自定义django模板,例如管理组页面, 我尝试获取所有字段,但仅获取可用权限,而不获取所选的权限字段 请帮助我,谢谢
我想要的字段: enter image description here
然后我得到的结果是: enter image description here
表单中的某些代码:
class UserGroupForm(forms.ModelForm):
class Meta:
model = Group
fields = '__all__'
视图中的某些代码:
def save_all(request, form, template_name):
data = dict()
error = None
if request.method == 'POST':
if form.is_valid():
form.save()
data['form_is_valid'] = True
data['action'] = True
else:
data['form_is_valid'] = False
error = form.errors
context = {
'form': form,
'error': error,
}
data['html_form'] = render_to_string(template_name, context, request=request)
return JsonResponse(data)
def create(request):
if request.method == 'POST':
form = UserGroupForm(request.POST)
else:
form = UserGroupForm()
form2 = UserPermissionForm()
return save_all(request, form, 'usergroups/create.html')
模板中的某些代码:
<form action="" method="post" data-url="{% url 'userroles:create' %}" class="create-form">
{% csrf_token %}
<div class="modal-header">
<h5 class="modal-title">Create Test</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{% if error %}
<div class="alert alert-danger text-center" role="alert">
{% for field, error in error.items %}
{{ error | striptags }}
<br>
{% endfor %}
</div>
{% endif %}
<div class="form-group">
{{form}}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
Ajax中的一些代码:
$(document).ready(function(){
var ShowForm = function(){
var btn = $(this);
$.ajax({
url: btn.attr('data-url'),
type: 'get',
dataType: 'json',
beforeSend: function(){
$('#modal-test').modal('show')
django.jQuery('select.selectfilter, select.selectfilterstacked').each(function () {
var $el = $(this),
data = $el.data();
SelectFilter.init($el.attr('id'), data.fieldName, parseInt(data.isStacked, 10));
});
},
success: function(data){
django.jQuery('select.selectfilter, select.selectfilterstacked').each(function () {
var $el = $(this),
data = $el.data();
SelectFilter.init($el.attr('id'), data.fieldName, parseInt(data.isStacked, 10));
});
$('#modal-test .modal-content').html(data.html_form);
}
})
};
var SaveForm = function(){
var form = $(this);
$.ajax({
url: form.attr('data-url'),
data: form.serialize(),
type: form.attr('method'),
dataType: 'json',
success: function(data){
if(data.form_is_valid){
if(data.deleteData){
Swal.fire({
type: 'warning',
title: 'Your Data is Deleted',
showConfirmButton: false,
timer: 1500
}).then(function(){
window.location.href = ''
})
} else if(data.action) {
Swal.fire({
type: 'success',
title: 'Your action is success',
showConfirmButton: false,
timer: 1500
}).then(function(){
window.location.href = ''
})
}
$('#modal-test').modal('hide');
console.log('Yes')
} else {
$('#modal-test .modal-content').html(data.html_form)
console.log('Not')
}
}
})
return false;
}
//create
$('.show-form').click(ShowForm);
$('#modal-test').on('submit', '.create-form', SaveForm);
//update
$(document).on('click', '.show-form-update', ShowForm);
$('#modal-test').on('submit', '.update-form', SaveForm);
//delete
$(document).on('click', '.show-form-delete', ShowForm);
$('#modal-test').on('submit', '.delete-form', SaveForm);
});
答案 0 :(得分:0)
Django管理员使用filter_horizontal来显示权限。为了适应它,您必须使用public Json parseExcel(Excel excel){
List<List<String>> rows = excel.readRows();
Json json = Json.array();
int startingIndex = 4; //skip header rows
for(int i = startingIndex; i <= rows.size(); i++){
List<String> row = rows.get(i);
json.add(Json.object()
.with("orderNum", row.get(0))
.with("date", row.get(1))
//... all the other columns ...
.with("subCondition", Json.object()
.with("blue", row.get(7))
.with("red", row.get(8)));
);
}
return json;
}
小部件。
forms.py:
FilteredSelectMultiple
并向您的前端添加小部件脚本/样式(我只是从django 2.2的管理页面复制粘贴了所有脚本和样式,因此一些可能不是必需的):
create.html
from django.contrib.auth.models import Group, Permission
from django.contrib.admin.widgets import FilteredSelectMultiple
class UserGroupForm(forms.ModelForm):
class Meta:
model = Group
fields = '__all__'
widgets = {
'permissions': FilteredSelectMultiple("Permission", False, attrs={'rows':'2'}),
}
如果您使用AJAX提取html表单,请将这些样式和脚本添加到您的HTML主页面。成功调用ajax之后,请调用以下javascript。该代码取自<form action="" method="post" data-url="" class="create-form">
{% csrf_token %}
<div class="modal-header">
<h5 class="modal-title">Create Test</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{% if error %}
<div class="alert alert-danger text-center" role="alert">
{% for field, error in error.items %}
{{ error | striptags }}
<br>
{% endfor %}
</div>
{% endif %}
<div class="form-group">
{{form}}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
<link rel="stylesheet" type="text/css" href="/static/admin/css/base.css">
<link rel="stylesheet" type="text/css" href="/static/admin/css/forms.css">
<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="/static/admin/js/vendor/jquery/jquery.js"></script>
<script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>
<script type="text/javascript" src="/static/admin/js/core.js"></script>
<script type="text/javascript" src="/static/admin/js/admin/RelatedObjectLookups.js"></script>
<script type="text/javascript" src="/static/admin/js/SelectBox.js"></script>
<script type="text/javascript" src="/static/admin/js/actions.js"></script>
<script type="text/javascript" src="/static/admin/js/SelectFilter2.js"></script>
<script type="text/javascript" src="/static/admin/js/urlify.js"></script>
<script type="text/javascript" src="/static/admin/js/prepopulate.js"></script>
<script type="text/javascript" src="/static/admin/js/vendor/xregexp/xregexp.js"></script>
。
SelectFilter2.js