django:在onsubmit下拉列表上更新数据库

时间:2017-10-30 13:19:58

标签: django

我正在尝试创建一个票务系统。我想要做的是当我更改状态下拉列表时,它应该更新数据库中故障单的状态。此外,我想在同一页面更新和查看。任何见解,如果可能的话?

forms.py

class ViewForm(forms.Form):
  statuses = [
    ('In Progress', 'In Progress'),
    ('On Hold', 'On Hold'),
    ('Done', 'Done'),
    ('ForQA', 'ForQA'),
    ('QAPassed', 'QAPassed'),
    ('QARevs', 'QARevs'),
  ]
  status = forms.ChoiceField(label='Status', required=True, choices=statuses, widget=forms.Select(attrs={'onchange': 'actionform.submit();'}))

views.py

def view_sheet(request, project_id):
   project = Project.objects.get(pk=project_id)
   tickets = Ticket.objects.filter(project=project_id) 
   form = ViewForm()
   context = {
     'project': project,
     'tickets': tickets,
     'form': form,
   }
   return render(request, 'project/sheet/view.html', context)  

view.html

<div class="tracker-sheet">
<div class="project-name">
  <h1>{{project.name}}</h1>
</div>
<div class="actions">
  <a href="{% url 'add_ticket' project.id %}">
    <button type="submit">New Ticket</button>
  </a>
</div >    
<div class="tracker-table">
  <table>
    <tr>
      <th>Total Worked</th>
      <th>Status</th>
      <th>Status Date</th>
      <th>Received</th>
      <th>Due Date</th>
      <th>Agent</th>
      <th>Sub Type</th>
      <th>CID</th>
      <th>Link</th>
      <th>Task Description</th>
      <th>Server</th>
      <th>Qty</th>
      <th>Info</th>
    </tr>
    {% for ticket in tickets %}
      <tr>
        <td><span class="table-constant"></span></td>            
        {% for field in form %}
        <td>{{field}}</td> <!-- Status dropdown list -->
        {% endfor %}
        <td><span class="table-constant">{{ticket.status_date}}</span></td>
        </form>
        <td><span class="table-constant">{{ticket.received}}</span></td>
        <td><span class="table-constant">{{ticket.due_date}}</span></td>
        <td><span class="table-constant"></span></td>
        <td><span class="table-constant">{{ticket.sub_type}}</span></td>
        <td><span class="table-vary"></span></td>
        <td><span class="table-constant">{{ticket.link}}</span></td>
        <td><input type="submit" value="{{ticket.task_description}}"</td>
        <td><span class="table-constant"></span></td>
        <td><span class="table-constant">{{ticket.qty}}</span></td>
        <td></td>          
      </tr>
    {% endfor %}
  </table>
</div>

3 个答案:

答案 0 :(得分:2)

这样的事情应该这样做:

views.py

def update_status(request, ticket_id):
    ticket = get_object_or_404(Ticket, pk=ticket_id)
    status = request.GET.get(status)
    if status:
        ticket.status = status
        ticket.save()
    else:
        raise Http404
    return HttpResponse({'ticket_id': ticket.id, 'status': ticket.status, content_type='application/json')

在模板(或远程js文件)中:

<script>
$(document).ready(function(){
    $(".statuses").change(function(){
        $.ajax({url: "update_status/" + $(this).data("ticket_id) + "/?status=" $(this).val(), success: function(result){
            console.log(result);
        }});
    });
});
</script>

urls.py:

....
url(r'^update_status/(?P<ticket_id>[\d]+)$', update_status),
....

注意:您需要为每个trs提供不同的故障单ID,因此我要为每个data-ticket_id = {{ ticket.id }}添加select。这意味着你的{{ field }}必须得到更多声明。

类似的东西:

<select id="status_{{ticket.id}}" class="statuses" data-ticket_id="{{ ticket.id }}" />
{% for k, v in statuses %}
    <option value="{{ k }}">{{ v }}</option>
{% endfor %}

答案 1 :(得分:1)

你所要求的是完全可能的。

这个想法:
一种方法是使用javascript函数调用来更新此字段(AJAX GET或POST)。此脚本将请求由同一视图或新视图管理的URL 然后视图将处理请求,返回一些数据供您解析,并可选择确认更改成功,或显示错误。

在实践中:
你可以使用例如jQuery Post,其中URL参数是view_sheet的一个(或者就像我说的那样,只是为了这个目的的新视图)。
在你的视图中,你可以使用request.is_ajax(), a test on the request,当它来自你的jQuery帖子时会是真的。

答案 2 :(得分:0)

  

我想要做的是当我更改状态下拉列表时应该这样做   更新数据库中故障单的状态

而不是:

status = forms.ChoiceField(label='Status', required=True, choices=statuses, widget=forms.Select(attrs={'onchange': 'actionform.submit();'}))

试试这个:

status = forms.ChoiceField(label='Status', required=True, choices=statuses, widget=forms.Select(attrs={'onChange':'this.form.submit()'}))