2017-10-30 3 views
0

발권 시스템을 만들려고합니다. 내가 원하는 것은 상태 드롭 다운 목록을 변경할 때 데이터베이스의 티켓 상태를 업데이트해야한다는 것입니다. 또한 같은 페이지에서 업데이트하고보고 싶습니다. 가능한 모든 통찰력?django : onsubmit 드롭 다운시 데이터베이스 업데이트

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> 

답변

2

뭔가를 수행해야합니다

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 파일)에

views.py

는 :

<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), 
.... 

참고 : You'l 다른 필요 이 각 티켓에 대한 티켓 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 %} 

+2

이것은 매우 자세하고 철저한 대답입니다. – noes1s

+0

감사합니다. 나는 그것이 받아 들여지지 않은 이유를 궁금하게 생각한다? –

+0

좋은 예입니다. 또한 OP, 사용자가 로그인되어 있고 컨텐츠를 편집 할 수있는 적절한 권한이 있는지 확인할 수 있습니다 :) – ppython

1

당신이 요구하는 것은 전적으로 가능하다.

아이디어 :
한 가지 방법은 (AJAX는 GET 또는 POST)이 필드를 업데이트하는 자바 스크립트 함수 호출을 사용하는 것입니다. 이 스크립트는 동일한보기 또는 새보기에서 관리하는 URL을 요청합니다.
그러면보기가 요청을 처리하고 구문 분석 할 데이터를 반환하고 선택적으로 변경 사항이 성공적인지 확인하거나 오류를 표시합니다. 실제로

:
당신은 URL 매개 변수, 예 A jQuery Post에 대한에 하나를 사용할 수 있습니다 view_sheet (또는 내가 말했듯이, 이러한 목적을위한 새로운보기).
request.is_ajax(), a test on the request으로 재생할 수 있습니다. jQuery 소식에서 올 때 사실입니다.

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

하는이 시도 :

0

내가 뭘 원하는 나는 상태 드롭 다운 목록을 변경할 때 데이터베이스이 대신

에서 티켓의 상태를 업데이트해야합니다입니다 :

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