Добавить новое обновление удалить редактировать не работает

В этом проекте не работает добавление нового, редактирование, удаление и обновление. Код правильный, но когда я нажимаю кнопку submit, update, он не работает. Пожалуйста, подскажите, где я ошибся. Пожалуйста, скажите мне.

urls.py:

from django.contrib import admin
from django.urls import path
from master import views
from django.conf.urls.static import static
from mysite6 import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    path('control/', views.controlindex),  
    path('controladdnew',views.controladdnew),  
    path('controledit/<int:id>', views.controledit),  
    path('controlupdate/<int:id>', views.controlupdate),  
    path('controldelete/<int:id>', views.controldestroy),  
]+static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)+static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

views.py:

from django.shortcuts import render,redirect
from django.http import HttpResponseRedirect
from master.forms import ControlmachineForm
from master.models import Controlmachine

# Create your views here.
def control(request):  
    if request.method == 'POST':
        form = ControlmachineForm(request.POST, request.FILES)  
        if form.is_valid():
            handle_uploaded_file(request.FILES['control_uploadfile'])
            model_instance = form.save()
            model_instance.save()
    else:
        form = ControlmachineForm()
    controlmachiness = Controlmachine.objects.all()
    return render(request,'master/control_uploadfile.html',{'form':form,'controlmachiness':controlmachiness})

def handle_uploaded_file(f):  
    with open('master/static/upload/'+f.name, 'wb+') as destination:  
        for chunk in f.chunks():  
            destination.write(chunk)

def controladdnew(request):  
    if request.method == "POST":  
        form = ControlmachineForm(request.POST)  
        if form.is_valid():  
            try:  
                form.save()  
                return HttpResponseRedirect('/control/')  
            except:  
                pass 
    else:  
        form = ControlmachineForm()  
    return render(request,'master/control_index.html',{'form':form})

def controlindex(request):  
    controlmachines = Controlmachine.objects.all()  
    return render(request,"master/control_show.html",{'controlmachines':controlmachines})

def controledit(request, id):  
    controlmachine = Controlmachine.objects.get(id=id)  
    return render(request,'master/control_edit.html', {'controlmachine':controlmachine})

def controlupdate(request, id):  
    controlmachine = Controlmachine.objects.get(id=id)  
    form = ControlmachineForm(request.POST, instance = controlmachine)  
    if form.is_valid():  
        form.save()  
        return HttpResponseRedirect('/control/')  
    return render(request, 'master/control_edit.html', {'controlmachine': controlmachine})

def controldestroy(request, id):  
    controlmachine = Controlmachine.objects.get(id=id)  
    controlmachine.delete()  
    return redirect('/control')

models.py:

from django.db import models

# Create your models here.
class Controlmachine(models.Model):
    machine_name = models.CharField(max_length=200)
    operation_no = models.IntegerField()
    control_uploadfile = models.FileField(upload_to='documents/')


    def __str__(self):
        return self.machine_name

forms.py:

from django import forms
from master.models import Controlmachine

class ControlmachineForm(forms.ModelForm):  
    class Meta:  
        model = Controlmachine  
        fields = ['machine_name', 'operation_no', 'control_uploadfile']
        widgets = { 'machine_name': forms.TextInput(attrs={ 'class': 'form-control' }), 
            'operation_no': forms.TextInput(attrs={ 'class': 'form-control' }),
            'control_uploadfile': forms.ClearableFileInput(attrs={ 'class': 'form-control' }),
        }

Шаблоны: control_base.html:

<!DOCTYPE html>  
<html lang="en">  
<head>  
<meta charset="UTF-8">  
<title>{% block title %}{% endblock %}</title>
{% load static %}  
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
 
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap4.min.js"></script>
</head> 
<body>
<div class="container">
 <div class="row">
    {% block content %}{% endblock %}
 </div>
</div>
<script>
$(document).ready(function() {
    $('#bootstrapdatatable').DataTable({     
      "aLengthMenu": [[3, 5, 10, 25, -1], [3, 5, 10, 25, "All"]],
        "iDisplayLength": 3
       } 
    );
} );
</script>
</body> 
</html>

control_edit.html:

{% extends "master/control_base.html" %}
 
{% block title %}Add New Control File{% endblock title %}
 
{% block content %}
<div class="col-md-12">
    <h4>Django CRUD (Create Read Update Delete) Example - Bootstrap Datatable</h4>  
 <form method="post" class="post-form" action="/control/controlupdate/{{controlmachine.id}}">  
   {% csrf_token %}  
  <div class="container">  
 <br>  
  <div class="form-group row">  
  <label class="col-sm-1 col-form-label"></label>  
  <div class="col-sm-4">  
  <h3>Update Details</h3>  
  </div>  
   </div>  
  <div class="form-group row">  
  <label class="col-sm-2 col-form-label">Machine ID:</label>  
  <div class="col-sm-4">  
   <input type="text" class="form-control" name="id" id="id_id" required maxlength="20" value="{{ controlmachine.id }}"/>  
  </div>  
   </div>  
   <div class="form-group row">  
  <label class="col-sm-2 col-form-label">Machine Name:</label>  
  <div class="col-sm-4">  
   <input type="text" class="form-control" name="name" id="id_name" required maxlength="100" value="{{ controlmachine.machine_name }}" />  
  </div>  
   </div>  
  <div class="form-group row">  
  <label class="col-sm-2 col-form-label">Operation Number:</label>  
  <div class="col-sm-4">  
   <input type="text" class="form-control" name="email" id="id_email" required maxlength="254" value="{{ controlmachine.operation_no }}" />  
  </div>  
   </div>  
  <div class="form-group row">  
  <label class="col-sm-2 col-form-label">Control UploadFile:</label>  
  <div class="col-sm-4">  
   <input type="file" class="form-control" name="contact" id="id_contact" required maxlength="15" value="{{ controlmachine.control_uploadfile }}" />  
  </div>  
   </div>  
  <div class="form-group row">  
  <label class="col-sm-1 col-form-label"></label>  
  <div class="col-sm-4">  
  <button type="submit" class="btn btn-success btn-lg">Update</button>  
  </div>  
   </div>  
  </div>  
 </form>  
{% endblock content %}

control_index.html:

{% extends "master/control_base.html" %}
 
{% block title %}Add New Control File{% endblock title %}
 
{% block content %}
<div class="col-md-12">
    <h4>Django CRUD (Create Read Update Delete) Example - Bootstrap Datatable</h4> 
         
 <form method="post" class="post-form" action="/controladdnew">  
   {% csrf_token %}  
  <div class="container">  
 <br>  
  <div class="form-group row">  
  <label class="col-sm-1 col-form-label"></label>  
  <div class="col-sm-4">  
  <h3>Enter Details</h3>  
  </div>  
   </div>  
   
   <div class="form-group row">  
  <label class="col-sm-2 col-form-label">Machine Name:</label>  
  <div class="col-sm-4">  
    {{ form.machine_name }}  
  </div>  
   </div>  
  <div class="form-group row">  
  <label class="col-sm-2 col-form-label">Operation Number:</label>  
  <div class="col-sm-4">  
    {{ form.operation_no }}  
  </div>  
   </div>  
  <div class="form-group row">  
  <label class="col-sm-2 col-form-label">Control UploadFile:</label>  
  <div class="col-sm-4">  
    {{ form.control_uploadfile }}  
  </div>  
   </div>  
  <div class="form-group row">  
  <label class="col-sm-1 col-form-label"></label>  
  <div class="col-sm-4">  
  <button type="submit" class="btn btn-primary">Submit</button>  
  </div>  
   </div>  
  </div>  
 </form>  
</div> 
{% endblock content %} 

control_show.html:

{% extends "master/control_base.html" %}
 
{% block title %}control Upload file{% endblock title %}
 
{% block content %}
 <div class="col-md-12">
        <h4>Django CRUD (Create Read Update Delete) Example - Bootstrap Datatable</h4> <span><a href="/controladdnew" class="btn btn-primary">Add New Record</a></span>  
        <div class="table-responsive">
  <table id="bootstrapdatatable" class="table table-striped table-bordered" width="100%">
            <thead>
                <th><input type="checkbox" id="checkall" /></th>
                <th>ID</th>
                <th>Machine Name</th>
                <th>Operation Number</th>
                <th>Edit</th>
                <th>Delete</th>
             </thead>
   <tbody>
   {% for control in controlmachines %}  
        <tr>  
        <td><input type="checkbox" class="checkthis" /></td>
        <td>{{ control.id }}</td>  
        <td>{{ control.machine_name }}</td>  
        <td>{{ control.operation_no }}</td>
        <td><p data-placement="top" data-toggle="tooltip" title="Edit"><a href="/controledit/{{ control.id }}" class="btn btn-primary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="glyphicon glyphicon-pencil"></span></a></p></td>
        <td><p data-placement="top" data-toggle="tooltip" title="Delete"><a href="/controldelete/{{ control.id }}" class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="glyphicon glyphicon-trash"></span></a></p></td>
        </tr>  
   {% endfor %}
   </tbody>
         
  </table>
        </div>
    </div>
{% endblock content %}

control_uploadfile.html:

<html>
<head>
<title>Master File Upload</title>
</head>
<body>
<p><h1>Control File</h1></p
<table border="1">
        <tr>
                <th>Machine Name</th>
                <th>Operation Number</th>
                <th>Control File</th>
        </tr>
        {% for file in controlmachiness %}
        <tr>
                <td>{{ file.machine_name }}</td>
                <td>{{ file.operation_no }}</td>
                <td> <a href="/media/{{file.control_uploadfile}}">view file</a></td>

        </tr>
        {% endfor %}
        </table>
</body>  
</html>
Вернуться на верх