我有三个模型user、usecase和usecase assign:我有两个视图重定向到同一页面,以显示用例的详细信息,并分配新用户和取消分配用户:
我正在尝试执行这个方法的逻辑:DELETE FROM usecase_assign
WHERE usecase_assign
. user_email
= 'nmbarak';
如何从模板中获取user_email并将其传递到视图中以删除它?我尝试创建两个视图,第一个用于显示用例详细信息并分配新用户,第二个用于取消分配并重定向到同一页面。
我的usecase_assign模型:
class UsecaseAssign(models.Model):
usecase_assign_date = models.DateTimeField(primary_key=True, auto_now_add=True)
usecase = models.ForeignKey(Usecase, models.DO_NOTHING)
user_email = models.ForeignKey('User', models.DO_NOTHING, db_column='user_email')
usecase_role_id = models.CharField(max_length=20)
my views:
@user_login_required
def view_usecase_details(request, ucid):
usecase_details = Usecase.objects.filter(usecase_id=ucid).all()
usecase_details = usecase_details.prefetch_related("usecaseids")
users = User.objects.all() #SELECT user_email FROM usecase_assign WHERE usecase_id LIKE 'NN245';
usecase_assigned = UsecaseAssign.objects.select_related('user_email').values_list('user_email__user_name').filter(usecase_id=ucid) #to show list of users working on uc
if request.method=='POST' and 'assignuser' in request.POST:
user_email = request.POST['user_email']
userAssignCheck = UsecaseAssign.objects.filter(user_email=user_email, usecase_id=ucid)
if userAssignCheck:
messages.error(request, "user already added!")
return HttpResponseRedirect(reverse('usecase-details', args=[ucid]))
else:
userAssignObj = UsecaseAssign.objects.create(user_email_id=user_email, usecase_id=ucid)
if userAssignObj:
messages.success(request, "User was Successfully Assigned with Usecase!")
return HttpResponseRedirect(reverse('usecase-details', args=[ucid]))
context = {'usecase_details': usecase_details, "users": User.objects.all(), 'usecase_assigned':usecase_assigned, "users": users}
return render(request, 'UsecaseDetails.html', context)
@user_login_required
def unassign_emp(request, eid):
if request.method == 'POST':
unassign_emp = UsecaseAssign.objects.filter(user_email_id=eid).delete()
return redirect('usecase-details')
我的模板:
{% for result in usecase_details %}
<div class="card card-body shadow-sm mb-4 mb-lg-0">
<form class="mb-card" action="/usecase-details/{{result.usecase_id}}" method="POST">
{% csrf_token %}
<div class="form-row mb-4">
<div class="col-lg-8 mr-f">
<label class="h5" for="user_email">Assign new users:</label>
<select name="user_email" class="custom-select my-1 mr-sm-2" id="user">
<option value="0" selected>Select</option>
{% for user in users %}
<option value="{{ user.user_email }}">{{ user.user_name }}</option>
{% endfor %}
</select>
</div>
</div>
<input type="submit" class="btn btn-primary" name="assignuser" value="Submit Changes">
</form>
<ul class="list-group list-group-flush">
<li class="list-group-item d-flex align-items-center justify-content-between px-0 border-bottom">
<div>
<label class="mb-card h5" for="user_email">Assigned users:</label>
{% for user in usecase_assigned %}
<form action="/unassign-emp/{{result.user_email}}" method="POST" style="display: inline !important;">
{% csrf_token %}
<div class="btn-group me-2 mb-2">
<button type="button" class="btn btn-outline-danger">{{user|join:', '}}</button>
<button type="submit" class="btn btn-danger" >
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-file-x-fill" viewBox="0 0 16 16">
<path d="M12 0H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2zM6.854 6.146 8 7.293l1.146-1.147a.5.5 0 1 1 .708.708L8.707 8l1.147 1.146a.5.5 0 0 1-.708.708L8 8.707 6.854 9.854a.5.5 0 0 1-.708-.708L7.293 8 6.146 6.854a.5.5 0 1 1 .708-.708z"></path>
</svg>
</button>
</form>
</div>
{% endfor %}
</form>
</div>
</li>
</ul>
</div>
{% endfor %}
网址:
path('usecase-details/<str:ucid>', av.view_usecase_details, name="usecase-details"),
path('unassign-emp/<str:eid>', av.unassign_emp),
我一直在这几天,我试图把一切都在一个视图仍然有问题的网址,我接受另一种方式来处理这一点。
1条答案
按热度按时间tjrkku2a1#
1.错误消息肯定是来自重定向,因为你需要添加你在path/url语句中使用的参数ucid:
1.在模板中使用url标签{% url... %}和name from path语句更像是“django”:
路径“unassign-emp”也是如此:
在模板中: