【发布时间】:2022-01-25 22:16:43
【问题描述】:
我正在处理员工视图。有了这个视图,我有 2 个容器。 1 用于员工姓名,另一个应在我按下姓名后立即显示信息。我试图弄清楚 3 天,但无法弄清楚有人可以帮我吗
将渲染的索引视图
def index(request):
employe = Employe.objects.all()
context = {
'employe': employe,
}
return render(request, 'managements/index.html', context)
jsonresponse 视图在这里
def employeInfo(request):
data = json.loads(request.body)
employeId = data['id_s']
action = data['action']
print(employeId)
print(action)
if action == 'get':
check = Employe.objects.get(id=employeId)
checkinfo, create = Employe.objects.get_or_create(object, id=employeId)
print('check:', check, 'checkinfo', checkinfo)
return JsonResponse('context', safe=False)
模板在这里,我为此尝试了几个 nvm 组合。
<div class="flex-container">
<div class="flex-child magenta" >
<div data-method="POST" id="label1"> Medewerker info</div>
{% csrf_token %}
{% if action %}
<ul>{{name.employe_info}}</ul>
<ul>{{surname.checkinfo}}</ul>
<ul>{{checkinfo.id}}</ul>
<ul>{{rank.id}}</ul>
<ul>{{employeInfo.name}}</ul>
<ul>{{email.id}}</ul>
<ul>{{phone.id}}</ul>
<ul>{{name.id}}</ul>
{% endif %}
<tr>
<div class="flex-child green">
<div id="label2"> Medewerke</div>
{% for profile in employe %}
<button id="hehe" data-id_s={{profile.id}} data-action="get" class=" btn-sm btn-outline-secondary info-employe">
<a>{{ profile.name}}</a></button>
{% endfor %}
</div>
</tr>
var updateBtns = document.getElementsByClassName('info-
雇员')
for( var i = 0; i < updateBtns.length; i++) {
updateBtns[i].addEventListener('click', function(){
var id_s = this.dataset.id_s
var action = this.dataset.action
console.log('id_s:', id_s, 'action:', action)
if (user === 'AnonymousUser'){
console.log('User is not authenticated')
}else{
infoUser(id_s, action)
console.log('user is authenticated')
}
})
}
function infoUser(id_s, action){
var url = '/employe_info/'
fetch(url, {
method: 'POST',
headers:{
'Content-Type':'application/json',
'X-CSRFToken': csrftoken,
},
body:JSON.stringify({'id_s': id_s, 'action': action})
})
.then((response)=>{
return response.json()
})
.then((data)=>{
console.log('data:', data)
location.reload()
})
}
srry忘了这里的js
我已经尝试了 3 天,但没有任何进展。提前谢谢你
【问题讨论】:
标签: python json django templates render