python - 找不到 Django 页面(404)错误我认为在表单操作中的 url 和 edit.html 中存在一些错误
问题描述
在此处输入图像描述这是我的 edit.html
<form method="POST" action="update/{{i.id}}" enctype="multipart/form-data" class="p-4 border rounded" onsubmit="myFunction()" >
{% csrf_token %}
{% comment %} <input type="hidden" name="csrfmiddlewaretoken" value="UabxqpD8HGPOu1ZSFnIHAPbMtRgWBAnVHEs8bLDx0HnxN6uhG3LyYvZShvcx1ekn"> {% endcomment %}
<div class="row form-group">
<div class="col-md-12 mb-3 mb-md-0">
<label class="text-black" for="full_name">Full Name :</label>
<input type="text" class="form-control" value ={{ i.full_name}} name="full_name" id="id_full_name" placeholder="Enter First Name">
</div>
</div>
<div class="row form-group">
<div class="col-md-12 mb-3 mb-md-0">
<label class="text-black" for="recruiter_name">Recruiter Name :</label>
<input type="text" class="form-control" value ={{ i.recruiter_name }} name="recruiter_name" id="id_recruiter_name" placeholder="Enter Recruiter Name">
</div>
</div>
{% comment %} <div class="row form-group">
<div class="col-md-12 mb-3 mb-md-0">
<label class="text-black" for="id_last_name">Last Name :</label>
<input type="text" class="form-control" name="last_name" id="id_last_name" placeholder="Enter Last Name">
</div>
</div> {% endcomment %}
<div class="row form-group">
<div class="col-md-12 mb-3 mb-md-0">
<label class="text-black" for="email">Email :</label>
<input type="email" class="form-control" value ={{i.email }} name="email" id="id_email" placeholder="Enter Email">
</div>
</div>
<div class="row form-group">
<div class="col-md-12 mb-3 mb-md-0">
<label class="text-black" for="noticeperiod">Notice Period (in Days) :</label>
<input type="text" class="form-control" value ={{i.noticeperiod }} name="noticeperiod" id="notice_period" placeholder="Notice Period">
</div>
</div>
<div class="row form-group">
<div class="col-md-12 mb-3 mb-md-0">
<label class="text-black" for="preferredlocation">Current Location :</label>
<input type="text" class="form-control" value ={{i.preferredlocation}} name="preferredlocation" id="preferred_location" placeholder="Enter Current Location">
</div>
</div>
<div class="row form-group">
<div class="col-md-12 mb-3 mb-md-0">
<label class="text-black" for="expectedlocation">Expected Location :</label>
<input type="text" class="form-control" value ={{i.expectedlocation}} name="expectedlocation" id="expected_location" placeholder="Enter Multiple Locations seperated by comma">
</div>
</div>
<div class="row form-group">
<div class="col-md-12 mb-3 mb-md-0">
<label class="text-black" for="currentctc">Current CTC (Per Annum) :</label>
<input type="text" class="form-control" value ={{i.currentctc }} name="currentctc" id="current_ctc" placeholder="Current CTC">
</div>
</div>
<div class="row form-group">
<div class="col-md-12 mb-3 mb-md-0">
<label class="text-black" for="expectedctc">Expected CTC (Per Annum) : </label>
<input type="text" class="form-control" value ={{i.expectedctc }} name="expectedctc" id="expected_ctc" placeholder="Expected CTC">
</div>
</div>
<div class="row form-group">
<div class="col-md-12 mb-3 mb-md-0">
<label class="text-black" for="status">Status : </label> </br>
<select name="status" id="status" class="form-control" value ={{i.status }}>
<option value="Shortlisted">Shortlisted</option>
<option value="Not Shortlisted">Not Shortlisted</option>
<option value="In Progress">In Progress</option>
</select>
{% comment %} <input type="text" class="form-control" name="status" id="status1" placeholder="Enter Application Status"> {% endcomment %}
</div>
</div>
<div class="row form-group">
<div class="col-md-12 mb-3 mb-md-0">
<label class="text-black" >Upload CV</label></br>
<input type="file" name="cv" id="cv1" value ={{ i.cv}} >
</div>
</div>
<div class="row form-group mb-4">
<div class="col-md-12 mb-3 mb-md-0">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="gender" id="male" value={{i.gender}}>
<label class="form-check-label" for="male">Male</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="gender" id="female" value={{i.gender}}>
<label class="form-check-label" for="female">Female</label>
</div>
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<input type="submit" value="Update" id ="submit" class="btn px-4 btn-primary text-white">
{% comment %} <a href="{% url 'jobapp:jobconfirm' %}" class="btn btn-warning btn-block btn-lg">Sign Up</a> {% endcomment %}
</div>
</div>
在提交此表单期间,url 中的表单操作可能存在一些问题我的意思是当我单击此更新按钮时,我收到页面未找到错误 404
这是我的意见.py
def delete(request, id):
i = ApplyForm.objects.get(id = id)
i.delete()
return redirect('account:dboard')
def edit(request, id):
i = ApplyForm.objects.get(id = id)
context = {
'i' : i
}
return render(request, 'account/edit.html', context)
def update(request, id):
i = ApplyForm.objects.get(id = id)
i.full_name = request.POST['full_name']
i.email = request.POST['email']
i.noticeperiod = request.POST['noticeperiod']
i.preferredlocation = request.POST['preferredlocation']
i.expectedlocation = request.POST['expectedlocation']
i.currentctc = request.POST['currentctc']
i.expectedctc= request.POST['expectedctc']
i.recruiter_name = request.POST['recruiter_name']
i.status = request.POST['status']
i.gender = request.POST['gender']
i.cv = request.POST['cv']
i.save()
return redirect('account:dboard')
和 urls.py
urlpatterns = [
path('employee/register/', views.employee_registration, name='employee-registration'),
path('employer/register/', views.employer_registration, name='employer-registration'),
path('applyform/', views.applyform, name='applyform'),
path('dboard/', views.dboard, name='dboard'),
path('export_excel/', views.export_excel),
path('delete/<id>/', views.delete, name='delete'),
path('edit/<id>/', views.edit, name='edit'),
path('update/<id>/', views.update, name='update'),
path('profile/edit/<int:id>/', views.employee_edit_profile, name='edit-profile'),
path('login/', views.user_logIn, name='login'),
path('logout/', views.user_logOut, name='logout'),
]
这是models.py
class ApplyForm(models.Model):
full_name = models.CharField(max_length=300)
email = models.EmailField(max_length=300)
noticeperiod = models.CharField(max_length = 300)
preferredlocation = models.CharField(max_length=300)
expectedlocation = models.CharField(max_length=300)
currentctc = models.CharField(max_length=300)
expectedctc = models.CharField(max_length=300)
recruiter_name = models.CharField(max_length = 300, null=True)
status = models.CharField(max_length = 300, null=True, choices= CATEGORY_CHOICES )
gender = models.CharField(max_length=10, choices=GENDER_CHOICES)
cv = models.FileField(upload_to='')
发生了什么我不知道当我单击更新按钮以更新我在edit.html 中的表单时,它显示找不到页面(404)请求方法发布。我只是想让更新按钮工作我认为表单操作有问题=/更新
解决方案
显然有两个可能的问题可能导致这种情况:
您的表单 URL 没有 ID,因此您的表单如下所示<form ...="/update/">
settings.py中有一个设置APPEND_SLASH
——可能是假的。所以你的网址看起来像 this/update/ID
而不是 this /update/ID/
。
检查它是否有 ID:打开站点并右键单击,选择显示源并检查表单元素以验证它确实有一个 ID。
设置APPEND_SLASH
为 true 或将尾部斜杠添加到表单项:action="/update/{{i.id}}/"
推荐阅读
- prestashop-1.6 - 无法在 hookActionOrderStatusUpdate 中获取用于发送电子邮件的客户电子邮件
- android - MPAndroidChart 设置当前可见X轴
- excel - Excel 平均值
- matlab - 如何根据另一个滑块更改滑块的最大值
- python - 使用 pytest 进行设置和拆卸
- r - 使用 if 语句替代我的慢速嵌套循环?
- macos-mojave - 为同一台机器上的两个应用程序实例处理 Mojave 权限(摄像头、麦克风和可访问性)
- asp.net - 无法导航到插入符号下的符号
- reactjs - TypeError:无法在 Gatsby 链接状态中读取 null 的属性“选择”
- mysql - 联合查询错误执行