反向搜索“ edit”,未找到任何参数。尝试了1种模式:['employees \\ / edit \\ /(?P <pk> [0-9] +)\\ / $']

时间:2019-07-09 07:09:02

标签: django django-models django-forms django-urls

构建一个Employee应用程序,使用表单更新Employee之后,它仅显示此错误,并且URL似乎正确,因此我可以说出错误的来源

我已经在表单中对所有的URL模式,视图和URL进行了交叉检查,还尝试使用this question中的解决方案,这给了我更大的错误

urls.py

urlpatterns = [

    path('edit/<int:pk>/', views.edit, name = 'edit'),       
]

views.py

@login_required(login_url='/accounts/login')
def edit(request, pk):
    employ = get_object_or_404(Employee, id=pk)
    logging.info(type(employ))
    departments = Department.objects.all()



    context = {
        'employ': employ,
        'departments':departments

    }

    if request.method == "POST":
        first_name = request.POST['first_name']
        last_name = request.POST['last_name']
        name = last_name +' '+first_name
        employee_id = request.POST['employee_id']
        email = request.POST['email']
        department = Department.objects.get(dept_name = request.POST['department'])
        address = request.POST['address']
        employment_type = request.POST['employment_type']
        employment_status = request.POST['employment_status']
        role = request.POST['role']
        marital_status = request.POST['marital_status']
        gender = request.POST['gender']
        join_date = request.POST['join_date']
        end_date = None if len(request.POST['end_date']) ==0 else request.POST['end_date']
        location = request.POST['location']
        credentials = request.POST['credentials']
        passport = request.POST['passport']
        hod = request.POST['hod']
        phone_number = request.POST['phone_number']
        date_of_birth = request.POST['date_of_birth']
        date_added = datetime.now()



        if Employee.objects.filter(employee_id = employee_id).exists() or Employee.objects.filter(email = email).exists():
            messages.error(request, 'That ID/Email is Taken')
            return redirect('edit')
        else:
            employee = Employee(first_name='first_name',last_name='last_name',email='email', 
            employee_id='employee_id',department='department',address='address',employment_type='employment_type',
            employment_status='employment_status',role='role',marital_status='marital_status',gender='gender',join_date='join_date',
            end_date='end_date',location='location',credentials='credentials',passport='passport',hod='hod',
            phone_number='phone_number',date_added='date_added',date_of_birth='date_of_birth')
            employee.save()
            messages.success(request, 'Employee Created')
            return redirect('all')



    return render(request, 'employees/edit.html', context, employ)

edit.html


            <form method = 'POST' action="{% url  'edit' pk=employ.pk %}" class="m-b-30" enctype="multipart/form-data">

                        {% csrf_token %}

                                <div class="row">
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for='first_name' class="control-label">First Name <span class="text-danger">*</span></label>
                                            <input name='first_name' class="form-control" type="text" required value="{{employ.first_name}}">
                                        </div>
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for='last_name' class="control-label">Last Name</label>
                                            <input name='last_name' class="form-control" type="text" required value="{{employ.last_name }}">
                                        </div>
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for='employee_id' class="control-label">Employee ID <span class="text-danger">*</span></label>
                                            <input name='employee_id' class="form-control" type="number" required value="{{employ.employee_id }}">
                                        </div>
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for='email' class="control-label">Email <span class="text-danger">*</span></label>
                                            <input name='email' class="form-control" type="email" required value="{{employ.email }}">
                                        </div>
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for='phone_number' class="control-label">Phone Number</label>
                                            <input name='phone_number' class="form-control" type="text" required value="{{employ.phone_number }}">
                                        </div>
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for='gender' class="control-label">Gender</label>
                                            <select name = 'gender' class="form-control" value = "{{employ.gender}} " type="text" >
                                                <option>select gender</option>
                                                    {% if employ.gender == 'Male' %}
                                                    <option selected ='selected'>Male</option>

                                                    {% else  %}
                                                    <option selected ='selected'>Female</option>
                                                    {% endif %}
                                                <option>Male</option>
                                                <option>Female</option>
                                            </select>

                                        </div>
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for='marital_status' class="control-label">Marital Status</label>
                                            <select name='marital_status' class="form-control" value = "{{employ.marital_status}} " type="text" >
                                                {% if employ.marital_status == 'Married' %}
                                                <option selected ='selected'>Married</option>
                                                {% else %}
                                                <option selected ='selected'>Single</option>
                                                {% endif %}
                                                <option>select martial status</option>
                                                <option>Married</option>
                                                <option>Single</option>

                                                <option>Single</option>
                                            </select>

                                        </div>
                                    </div>
                                    <div class="col-sm-6">  
                                        <div class="form-group">
                                            <label for='date_of_birth' class="control-label">Date of Birth <span class="text-danger">*</span></label>
                                            <input name='date_of_birth' class="form-control" type="date" required value="{{employ.date_of_birth|date:'Y-m-d' }}">
                                        </div>
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for='address' class="control-label">Address </label>
                                            <input name='address' class="form-control" type="text" required value="{{employ.address }}">
                                        </div>
                                    </div>
                                    <div class="col-sm-6">  
                                        <div class="form-group">
                                            <label for='department' class="control-label">Department <span class="text-danger">*</span></label>
                                            <select name = 'department' type="text" class="form-control" value = "{{employ.department}} " >
                                                <option>select department</option>
                                                {% for department in departments %}
                                                    {% if employ.department == department  %}
                                                    <option selected ='selected'>{{employ.department}}</option>
                                                    {% endif %}
                                                  <option>{{department.dept_name}}</option>
                                                {% endfor %}

                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for='employment_type' class="control-label">Employment Type </label>
                                            <select id= "employment_type" name='employment_type' type="text" class="form-control" value = "{{employ.employment_type}} " >
                                                {% if employ.employment_type == 'Contract' %}
                                                <option selected ='selected'>Contract</option>
                                                {% elif employ.employment_type == 'Fulltime' %}
                                                <option selected ='selected'>Fulltime</option>
                                                {% else  %}
                                                <option selected ='selected'>Intern</option>
                                                {% endif %}

                                            <option>select employment type</option>
                                            <option>Contract</option>
                                            <option>Fulltime</option>
                                            <option>Intern</option>
                                        </select>
                                        </div>


                                    </div>
                                    <div class="col-sm-6">  
                                        <div class="form-group">
                                            <label for='join_date' class="control-label">Join Date <span class="text-danger">*</span></label>
                                            <input name='join_date' class="form-control " type="date" required value="{{employ.join_date|date:'Y-m-d' }}">
                                        </div>
                                    </div>
                                    <div class="col-sm-6" id="end_date">  
                                        <div class="form-group">
                                            <label for='end_date' class="control-label">End Date <span class="text-danger">*</span></label>
                                            <input  name='end_date' class="form-control " type="date" value="{{employ.end_date|date:'Y-m-d' }}">
                                        </div>
                                    </div>

                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for='employment_status' class="control-label">Employment Status </label>
                                            <select name='employment_status' type="text" class="form-control" value="{{employ.employment_status}}" >
                                                {% if employ.employment_status == 'Active' %}
                                                <option selected ='selected'>Active</option>

                                                {% else  %}
                                                <option selected ='selected'>Inactive</option>
                                                {% endif %}

                                            <option>select employment status</option>
                                            <option>Active</option>
                                            <option>Inactive</option>
                                        </select>
                                        </div>
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for='role' class="control-label">Role </label>
                                            <input name='role' class="form-control" type="text" required value="{{employ.role }}">
                                        </div>
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for='location' class="control-label">Work Location </label>
                                            <input name='location' class="form-control" type="text" required value="{{employ.location }}">
                                        </div>
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for='hod' class="control-label">HOD </label>
                                            <select name='hod' class="form-control"  type="text" >

                                                <option>Select HOD</option>
                                                {% for department in departments %}
                                                    {% if employ.department.lead == department.lead  %}
                                                    <option selected ='selected'>{{employ.department.lead}}</option>
                                                    {% endif %}
                                                  <option>{{department.lead}}</option>
                                                {% endfor %}

                                            </select>
                                        </div>
                                    </div>

                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for='passport' class="control-label">Upload passport </label>
                                            <input name='passport' type="file" accept="image/*"  value="{{employ.passport }}">
                                        </div>
                                    </div>

                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for='credentials' class="control-label">Add Credentials </label>
                                            <input name='credentials' type="file"  accept="image/*"  value="{{employ.credentials }}">
                                        </div>
                                    </div>
                                </div>

                                <div class="m-t-20 text-center">
                                    <button class="btn btn-primary">Update Employee</button>
                                </div>

                    </form>

它应该被提交并路由到edit.hmtl,以防在提交时出现任何错误

2 个答案:

答案 0 :(得分:1)

当您重定向到edit视图时,您需要传递主键。

return redirect('edit', pk=employ.pk)

顺便说一句,不建议直接从request.POST访问值。看看Django表单。

答案 1 :(得分:0)

没什么改变表格

<form method = 'POST' action="{% url  'edit' pk=employ.pk %}" class="m-b-30" enctype="multipart/form-data">

<form method = 'POST' action="" class="m-b-30" enctype="multipart/form-data">

并将渲染到

return render(request, 'employees/edit.html', context)