对于循环填充 ID 在 URL Django 中



我的模板上有一个按钮,上面写着检查进度,该按钮的作用是根据该用户的用户ID将您带到该用户的另一个页面。

/points/student_progress/1000 <-- id

我要完成的:

当我的 for 循环运行以填充模板上的学生姓名时,我希望它也填充按钮上的学生 ID。/点/student_progress/{{student_id}} .这是我的代码。我如何在 ?

Views.py

@login_required
def K8_Points_Classroom(request):
#context_from_k8_points = request.session['k8_points_context'] 
if request.method == 'POST':
form = K8Points_ClassroomForm(request.POST)
if form.is_valid():
form.save(commit=False)
form.save()
class_name = form.cleaned_data.get('class_name')
getstudents = Student.objects.filter(class_name = class_name)
students = getstudents.all()
form = K8Points_ClassroomForm()
context = {'form': form ,'students' : students, 'class_name': class_name,}
messages.success(request,("Points were successfully added for student !"))
return render(request, 'points/k8_points_classroom.html', context )   
if not form.is_valid():
messages.warning(request,(form._errors))
class_name = request.POST.get('class_name')[0]
getstudents = Student.objects.filter(class_name = class_name)
students = getstudents.all()
context = {'form': form, 'students': students,}
return render(request, 'points/k8_points_classroom.html', context )      
else:
form = K8Points_ClassroomForm()
return render(request, 'points/k8_points_classroom.html', context)   

@login_required
def K8_Points(request):
if request.method == 'POST':
form = K8PointsForm(request.POST)
if form.is_valid():
form.save(commit=False)
class_name = form.cleaned_data.get('class_name')
getstudents = Student.objects.filter(class_name = class_name)
students = getstudents.all()
form = K8Points_ClassroomForm()
context = {'form': form ,'students' : students, 'class_name': class_name,}
# request.session['k8_points_context'] = context
return render(request,'points/k8_points_classroom.html', context)
else:
return HttpResponseBadRequest("Bad Request")        
else:
form = K8PointsForm()
return render(request, 'points/k8_points.html', {'form': form} )   

def Student_Progress(request, studentpsid):
studentid = Student.objects.get(studentpsid=studentpsid)
if K8Points.objects.filter(student_name=studentpsid).exists():
return render(request, 'points/student_progress.html', {'studentid': studentid} ) 
else:
return HttpResponseBadRequest("Student doesn't exist !")  

网页模板


{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% crispy K8Points_ClassroomForm %}
{% load static %}
{% block content %}

<br>
<h2>{% load static %}
<img src="{% static 'forms/star.png' %}" alt="chain" height="62" width="62">  {{class_name}}</h2>
<br>
<br>
<script> </script>
<form action="/points/k8_points_classroom" method="POST">
{% csrf_token %}
<!-- Start Date -->
<div class="container">
<div class="container">
<div class='row'>
<div class="col-4">
<p> Recording Data as User : {{user.username}} </p>
</div>
</div>
<div class='row'>
<div class = "col-2">
<input type="button" onclick="window.location.href = '/points/k8_points';" value="Exit Classroom"/>
</div>
</div>
<br>
<div class='row'>
<div class = "col-2">
{{form.date|as_crispy_field }}
</div>
<div class = "col-2">
{{form.week_of|as_crispy_field }}
</div>
<div class = "col-2">
{{form.day|as_crispy_field }}
</div>
</div>
</div>
</form>
<div class="jumbotron" align="middle">
<img src="{% static 'forms/levelup.png' %}" alt="levelup" height="120" width= "120">
<h1>My Students</h1>
<!-- Line Break -->
<hr style="border: 1px solid black;"/>
<!-- Line Break -->
<div class="row mb-3">
{% for i in students%}
<div class="col-md-4 themed-grid-col"><h2>{{i.student_name}}</h2> 
<p align="left"> Today's Score: {{total}}</p>
<h4>
<button type="button" class="btn btn-primary  " data-toggle="modal"
data-target="#PointsBox{{student.pk}}"><i class="fas fa-level-up-alt"></i> Level Up
</button>
<a href="#" class="btn btn-success" role="button"><i class="fas fa-chart-line"></i> Check Progress</a>
</h4>
<div id="PointsBox{{student.pk}}" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<img src="{% static 'forms/star.png' %}" align="left" alt="chain" height="42"
width="42">
<h4 class="modal-title">Points Confirmation </h4>
<button type="button" class="close" data-dismiss="modal"> &times;</button>
</div>
<div class="modal-body">
<h6>
<div class="modal-body">Please add the selected points for the current
student.</div>
</h6>

<form action="/points/k8_points_classroom" method="POST">
{% csrf_token %} 
<div class="form-row" align='left'> 
<div class="col-7">
{{form.class_name|as_crispy_field }} 
{{form.student_name|as_crispy_field }} 
{{form.time_frame|as_crispy_field }}
</div>
</div>
<div class="form-row">
<div class="col-3" align='left'>
{{form.behavior|as_crispy_field }}
{{form.academic|as_crispy_field }}
<button type="submit" class="btn btn-success" ><i
class="fas fa-star"></i> Level Up
</button>
</div>
</div>
</div>
<div class="modal-foot"></div>
</div>
</div>
</div>
</div>
</form>
{% endfor %}    
<script> 
const class_name = "{{class_name}}";
var classnamedropdown = document.getElementById('id_class_name');
for (i = 0; i < classnamedropdown.options.length; i++) {
// if(classnamedropdown.options[i].text == "Mr. Neo 8th Grade Science")  
if(classnamedropdown.options[i].text == class_name)
{
console.log(classnamedropdown.options[i].text)
$("#id_class_name").val(classnamedropdown.options[i].value)
}
}
</script>   

{% endblock %}

检查进度

只需要在循环中添加 {{i.studentpsid}}。

最新更新