Python 如何在Django中的更新crud中解决此问题?
好吧,我已经尝试解决这个问题好几天了,在执行更新crud时,表单没有使我有效,修改的记录也没有保存。 接下来,我将向您展示我的代码在每个模块中的表现: views.py 这就是我认为的问题所在,因为表单没有使其对我有效,也没有进行更改,因此它没有将我重定向到模板mostrarcursos.htmlPython 如何在Django中的更新crud中解决此问题?,python,html,django,orm,pycharm,Python,Html,Django,Orm,Pycharm,好吧,我已经尝试解决这个问题好几天了,在执行更新crud时,表单没有使我有效,修改的记录也没有保存。 接下来,我将向您展示我的代码在每个模块中的表现: views.py 这就是我认为的问题所在,因为表单没有使其对我有效,也没有进行更改,因此它没有将我重定向到模板mostrarcursos.html def editarcurso(request, id): curso = Curso.objects.get(id=id) return render(request,'editar
def editarcurso(request, id):
curso = Curso.objects.get(id=id)
return render(request,'editarcursos.html', {'curso':curso})
def modificarcurso(request,id):
curso = Curso.objects.get(id=id)
curso = CursoForm(request.POST, instance=curso)
if curso.is_valid():
curso.save()
return redirect("/mostrarcursos")
return render(request, 'mostrarcursos.html', {'curso': curso})
forms.py
class CursoForm(forms.ModelForm):
class Meta:
model = Curso
fields=['docente','anio','paralelo','jornada']
models.py
class Curso(models.Model):
docente= models.ForeignKey(Docente,on_delete=models.CASCADE)
anio = models.IntegerField(default=1)
paralelo = models.CharField(max_length=1)
jornada = models.CharField(max_length=20)
user = models.CharField(max_length=100)
user_mod = models.CharField(max_length=100)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
estado = models.IntegerField(default=1) # 1 es activo y 2 es eliminado
class Meta:
db_table ="Cursos"
verbose_name ="Curso"
verbose_name_plural ="Cursos"
def __str__(self):
return self.paralelo + ' ' + self.jornada
url.py
from django.contrib import admin
from django.urls import path
from core import views
urlpatterns = [
path('', views.home, name="home"),
path('tareas/', views.tareas, name="tareas"),
path('login/', views.login, name="login"),
path('horarios/', views.horarios, name="horarios"),
path('examenes/', views.examenes, name="examenes"),
path('planificaciones/', views.planificaciones, name="planificaciones"),
path('creardocente/', views.creardocente, name="creardocente"),
path('mostrardocentes/', views.mostrardocentes, name="mostrardocentes"),
path('editardocentes/<int:id>', views.editardocente),
path('updatedocentes/<int:id>', views.updatedocente),
path('deletedocentes/<int:id>', views.deletedocente),
path('crearcurso/', views.crearcurso, name="crearcurso"),
path('mostrarcursos/', views.mostrarcursos, name="mostrarcursos"),
path('modificarcurso/<int:id>', views.modificarcurso,
name="modificarcurso"),
path('deletecurso/<int:id>', views.deletecurso),
path('crearpregunta/', views.crearpregunta, name="crearpregunta"),
path('crear_respuesta/', views.crear_respuesta, name="crear_respuesta"),
path('mostrarpreguntas/', views.mostrarpreguntas, name="mostrarpreguntas"),
path('creartarea/', views.creartarea, name="creartarea"),
path('mostrartarea/', views.mostrartarea, name="mostrartarea"),
path('admin/', admin.site.urls)
]
来自django.contrib导入管理
从django.url导入路径
从核心导入视图
URL模式=[
路径(“”,views.home,name=“home”),
路径('tareas/',views.tareas,name=“tareas”),
路径('login/',views.login,name=“login”),
路径('horarios/',views.horarios,name=“horarios”),
路径('examenes/',views.examenes,name=“examenes”),
路径('planifications/',views.planifications,name=“planifications”),
路径('creardocente/',views.creardocente,name=“creardocente”),
路径('mostrardocentes/',views.mostrardocentes,name=“mostrardocentes”),
路径('editardocentes/',views.editardocente),
路径('updateCentes/',views.updateCente),
路径('deletedocentes/',views.deletedocente),
路径('crearcurso/',views.crearcurso,name=“crearcurso”),
path('mostrarcursos/',views.mostrarcursos,name=“mostrarcursos”),
路径('modificarcurso/',views.modificarcurso,
name=“modificarcurso”),
路径('deletecurso/',views.deletecurso),
路径('crearpregunta/',views.crearpregunta,name=“crearpregunta”),
路径('crear_respuesta/',views.crear_respuesta,name=“crear_respuesta”),
路径('mostrarpreguntas/',views.mostrarpreguntas,name=“mostrarpreguntas”),
路径('creartarea/',views.creartarea,name=“creartarea”),
路径('mostrartarea/',views.mostrartarea,name=“mostrartarea”),
路径('admin/',admin.site.url)
]
editarcurso.html
此模板包含当我单击“编辑”并使用更新crud时显示的表单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>UPDATE</title>
</head>
<body>
<form method="POST" class="post-form" action=" /modificarcurso/{{curso.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">Id:</label>
<div class="col-sm-4">
<input type="number" name="id" id="id" required maxlength="1000" value="{{ curso.id }}"/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">DOCENTE:</label>
<div class="col-sm-4">
<input type="text" name="docente" id="docente" required maxlength="1000" value="{{ curso.docente }}"/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">AÑO:</label>
<div class="col-sm-4">
<input type="text" name="anio" id="anio" required maxlength="20" value="{{ curso.anio }}" />
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">PARALELO:</label>
<div class="col-sm-4">
<input type="text" name="paralelo" id="paralelo" required maxlength="1" value="{{ curso.paralelo }}" />
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">JORNADA:</label>
<div class="col-sm-4">
<input type="text" name="jornada" id="jornada" required maxlength="20" value="{{ curso.jornada }}" />
</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">Update</button>
</div>
</div>
</div>
</form>
</body>
</html>
更新
{%csrf_令牌%}
更新详细信息
身份证件:
文件员:
AñO:
平行:
乔纳达:
更新
mostrarcursos.html
def editarcurso(request, id):
curso = Curso.objects.get(id=id)
return render(request,'editarcursos.html', {'curso':curso})
def modificarcurso(request,id):
curso = Curso.objects.get(id=id)
curso = CursoForm(request.POST, instance=curso)
if curso.is_valid():
curso.save()
return redirect("/mostrarcursos")
return render(request, 'mostrarcursos.html', {'curso': curso})
此模板用于查看我通过表单输入的所有课程,其中我有两个要编辑和删除的URL
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>LISTA DE CURSOS</title>
{% load static %}
<link rel="stylesheet" href="{% static 'css/estilos.css' %}"/>
</head>
<body>
<table class="table table-striped table-bordered table-sm">
<thead class="thead-dark">
<tr>
<th>ID</th>
<th>DOCENTE</th>
<th>AÑO</th>
<th>PARALELO</th>
<th>JORNADA</th>
</tr>
</thead>
<tbody>
{% for curso in curso %}
<tr>
<td>{{ curso.id }}</td>
<td>{{ curso.docente }}</td>
<td>{{ curso.anio }}</td>
<td>{{ curso.paralelo }}</td>
<td>{{ curso.jornada }}</td>
<td>
<td> <a href="/modificarcurso/{{ curso.id }}">editar</a></td>
<td> <a href="/deletecurso/{{ curso.id }}">Delete</a></td>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<br>
<br>
<a href="{% url 'crearcurso' %}" class="btn btn-primary">Agregar un nuevo curso</a>
</body>
</html>
库索斯酒店
{%load static%}
身份证件
医生
阿尼奥
平行
乔纳达
{curso%中curso的%
{{curso.id}}
{{curso.docente}}
{{curso.anio}}
{{curso.paralelo}}
{{curso.jornada}}
{%endfor%}
show us html template and url.pyi刚刚更新了这个问题,如果理解不太清楚,我深表歉意。。