将两个视图重定向到一个具有不同url id的模板的正确方法Django

uqcuzwp8  于 2023-03-31  发布在  Go
关注(0)|答案(1)|浏览(93)

我有两个看法:一个用于显示已分配用户的列表,另一个用于分配新用户的post方法。
第二种观点:从usecase_assigned表中删除用户并重定向到同一页。
我在一个模板中显示所有内容,然后按删除按钮它执行另一个视图并重定向到同一页面(UsecaseDetails.html):第一个视图有用例的ID,我如何从第二个视图中删除并重定向到第一个视图,它有用例ID?
我的模特:

class User(models.Model):
   user_email = models.CharField(primary_key=True, max_length=100)
   user_role_id = models.CharField(max_length=20)
   user_password = models.CharField(max_length=20)
   user_name = models.CharField(max_length=100)

class Usecase(models.Model):
   usecase_id = models.CharField(primary_key=True, max_length=20)
   usecase_name = models.CharField(max_length=256)
   user_email = models.ForeignKey('User', models.DO_NOTHING, db_column='user_email')
   usecase_type = models.ForeignKey('UsecaseType', models.DO_NOTHING)

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)

我的看法:

@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
   user_assigned = UsecaseAssign.objects.values_list('user_email').filter(usecase_id=ucid) #to show list of users working on uc
   print(user_assigned)

   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, "user_assigned": user_assigned}
   return render(request, 'UsecaseDetails.html', context)   

@user_login_required
def unassign_emp(request, eid):
   if request.method=='POST' and 'unassign_emp' in request.POST:
       unassign_emp = UsecaseAssign.objects.get(user_email=eid)
       unassign_emp.delete()
       messages.success(request, "User was unassigned with Usecase!")
   return redirect('usecase-details') #my question is in here how to pass ucid (usecase_id)

我的模板:

{% for result in usecase_details %}
<div class="card card-body shadow-sm mb-4 mb-lg-0">
    <form class="mb-card" action="{% url 'usecase-details' ucid=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 %}
                <p>{{user|join:', '}}</p>
                {% endfor %}
                {% for unassign in user_assigned %}
                <form action="{% url 'unassign_emp' unassign %}" method="POST" style="display: inline !important;">
                    {% csrf_token %}
                    <input type="submit" class="btn btn-primary" name="unassign_emp" value="Delete">
                  </form>
                  {% endfor %}
            </div>
            </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, name="unassign_emp"),
8cdiaqws

8cdiaqws1#

@user_login_required
def unassign_emp(request, eid):
   if request.method=='POST' and 'unassign_emp' in request.POST:
       unassign_emp = UsecaseAssign.objects.get(user_email=eid)
       unassign_emp.delete()
       messages.success(request, "User was unassigned with Usecase!")
   return redirect('usecase-details', kwargs={'ucid':unassign_emp.usecase_id})

您可以传递kwargs参数进行重定向。更多信息:https://docs.djangoproject.com/en/4.1/topics/http/shortcuts/#django.shortcuts.redirect

相关问题