我想添加一个按钮来“验证”用户的下一步注册。
工作人员必须在用户详细信息页面中“验证”用户。
所以我用模板创建了一个 DetailView 视图,其中包含该用户的所有详细信息。 但是我想知道是否可以在 DetailView 函数中编写代码以允许用户在单击“验证”时增加他的 step_registration 变量?
这是我的 DetailView 函数:
views.py
class StudentDetailView(DetailView, LoginRequiredMixin):
model = Student
login_url = login
student_detail.html
{% block content %}
<h1>Etudiant: {{ student.user.firstname }} {{ student.user.firstname }}</h1>
<ul>
{% if student.photo %}
<li> Photo :<img src="{{ student.photo.url }}", width = 250, height = 300>"> </li>
{% endif %}
<li> Etat de l'inscription : {{ student.step_registration }}</li>
<li> Date de naissance : {{ student.birthdate }} </li>
</ul>
<button class="btn btn-secondary"> Validate ! </button>
{% endblock %}
步骤注册变量被称为“step_registration”,所以当工作人员点击“validate”时,我想做student.step_registration += 1
答案 0 :(得分:1)
要使按钮仅对工作人员可见(您也可以添加逻辑以仅在特定步骤显示):
{% if user.is_staff %}
<button class="btn btn-secondary" href="/validate_student/student.id"> Validate ! </button>
{% endif %}
添加视图并将其链接到 urls.py(您可以添加一些逻辑/检查或将步骤设置为某个值):
from django.contrib.admin.views.decorators import staff_member_required
@staff_member_required
def validate_student(request, id):
student = Student.objects.get(id)
student.registration_step += 1
student.save()
return ... # redirect or success page