Django显示选择列表

时间:2018-08-27 12:53:05

标签: django django-forms

我正在尝试实施一种表单,在该表单中可以选择候选人进行审批处理。我想显示候选人列表,以便在选择过程中可以查看其数据。

models.py

class SubmissionApproval(models.Model):
    candidate=models.ForeignKey(Candidate,on_delete=models.CASCADE)
    clientProcess=models.ForeignKey(ClientProcessDetails,verbose_name="Client 
               Process",help_text="Process",on_delete=models.PROTECT)
    dateSubmitted=models.DateTimeField(auto_now_add=True)
    comments=models.CharField("Comments",max_length=200,blank=True,null=True)

    class Meta:
        verbose_name="First Stage Approval"
        unique_together=(('candidate','clientProcess'),)

    def __str__(self):
         return self.candidate.name

views.py

def submissionApprovalList(request):
    object_list=Candidate.objects.all()
    if request.method=="POST":
        fm=SubmissionApprovalFormList(request.POST)
        print("Reached Post")
        if fm.is_valid():
            print("Printing Candidates Start")
            for item in fm.cleaned_data['candidates']:
                print(item)
            print("Printing candidates End")
    else:
        fm=SubmissionApprovalFormList()
    return render(request,'itrecruitment/firstStageSubmissionList.html',{'form':fm,'object_list':object_list })

forms.py

class SubmissionApprovalFormList(forms.Form):
    candidates=forms.ModelMultipleChoiceField(
        queryset=Candidate.objects.all(), widget=forms.CheckboxSelectMultiple
    )

模板

{% extends "seekgeek/base.html" %} 

{% block content %}
<div class="row">
<div class="col-xs-12">

    <div class="box">
        <div class="box-header">
            <h3 class="box-title">Select Candidates for First Stage Submission</h3>
            <a href="{% url 'candidate:add' %}"><button type="button" class="btn btn-primary pull-right">Add Candidate </button></a>
        </div>
        <!-- /. box-header -->
        <div class="box-body table-responsive">
            <!-- form details -->
                {% load bootstrap3 %}
                {% bootstrap_css %}
                {% bootstrap_javascript %}
                {% bootstrap_messages %}
                <form method="post" enctype="multipart/form-data" >
                {% csrf_token %}        
           <table  class="table table-bordered table-hover datatable2">
            <thead>
                <tr>
                    <td><b>Select</b></td>
                    <td><b>Candidate No</b></td> 
                    <td><b>Candidate Name</b></td>
                    <td><b>Current CTC (LPA)</b></td>
                    <td><b>Expected CTC (LPA)</b></td>
                    <td><b>Experience in Yrs</b></td>                    
                    <td><b>Recruiter</b></td>
                    <td><b>Latest Status</b></td>
                    <td><b>Languages</b></td>
                    <td><b>Updated</b></td>

                </tr>
            </thead>
            <!-- ./ table head -->
            <tbody>
                {% for obj in object_list %}
                <tr>
                 <!--Select field for Form  -->   
                <td> {{ form.candidates.0 }} </td>
                 <!--Select field for Form  -->  
                <td>{{ obj.id }}</td>
                 <td><a href="{% url 'candidate:detail' obj.pk %}">{{ obj.name }}</a></td>
                 <td>{{ obj.currentCTC }}</td>
                 <td>{{ obj.expectedCTC }}</td>
                 <td>{{ obj.experience }}</td>
                 <td><a href="{{ obj.recruiter.get_absolute_url }}">{{ obj.recruiter }}</a></td>
                 <td>{% if obj.latestRecStatus %}
                     <p><a class='text-green' href="{{ obj.latestRecStatus.get_absolute_url }}">{{ obj.latestRecStatus }}</a></p>
                    <p><a class='text-yellow' href="{% url 'clients:process-detail' obj.latestRecStatus.clientProcess.id %}"> {{ obj.latestRecStatus.clientProcess }} </a></p>
                    {% else %}
                     <p>No Status</p>
                     {% endif %}
                </td>
                <td>    
                    {% for item in obj.programmingLanguages.all %}
                        <p>{{ item }}</p>
                    {% endfor %}

                </td>
                <td>{{ obj.updated }}</td>
                </tr>
                {% endfor %}
            </tbody>
            <!-- /. table body -->
           </table>
           <!-- /. table -->

           {% buttons %}
           <button type="submit" class="btn btn-primary">
             Submit
           </button>
         {% endbuttons %}

        </div>
        <!-- /. box body -->

    </div>
    <!-- /. box -->

</div>
<!-- /.col -->



</div>
<!-- /.row -->


{% endblock %}

执行此操作的正确方法是什么。目的类似于django admin中的更改列表表单

enter image description here

0 个答案:

没有答案