Django表单提交发送GET而非POST请求的问题求助
问题描述
在Django项目中点击表单的「Guardar」保存按钮时,系统发送的是GET请求而非POST请求。查阅表单处理与POST方法相关资料后仍未找到问题根源,以下是项目相关代码文件及终端日志,请求排查。
相关代码及日志
subir_noticia.html
{% extends "homepage/base.html" %} {% block title %}Estacion Obrera{% endblock %} {% block content %} <form action="" method="POST" enctype="multipart/form-data"\> {% csrf_token %} {{ form.as_p }} <input type="submit" value="Guardar" class="btn btn-dark"\> </form\> {% %}
views.py
def crear_noticia(request): if request.method == 'POST': form = NoticiaForm(request.POST, request.FILES) if form.is_valid(): form.save() return redirect('ver_noticias_pendietes.html') else: form = NoticiaForm() return render(request, 'administrar_noticias/subir_noticia.html', {'form': form})
终端日志
[15/Jul/2024 23:50:30\] "GET /administrar_noticias/subir_noticia/?busqueda=&csrfmiddlewaretoken=6JWbqf0Dgo1AExf6P27MzAPlXc06iyvFquFaZ4ztBJSsa0Z1qjK1F5BXjV1Wg88A&titulo=Prueba&subtitulo=pruebita&categoria=hjkhk&cuerpo_noticia=asda&pie_de_foto=ada&imagen=WhatsApp+Image+2024-07-08+at+23.23.38+%281%29.jpeg&autor=asda HTTP/1.1" 200 5097
urls.py
urlpatterns = [ path("subir_noticia/", views.crear_noticia, name='subir_noticia'), path("ver_noticias_pendientes/", views.ver_noticias_pendientes, name='ver_noticias_pendientes'), ]
forms.py
class NoticiaForm(forms.ModelForm): class Meta: model = Noticia fields = ['titulo', 'subtitulo', 'categoria', 'cuerpo_noticia', 'pie_de_foto', 'imagen','autor'] def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs)
models.py
class Noticia(models.Model): ESTADOS = ( ('pendiente', 'Pendiente'), ('publicado', 'Publicado'), ('oculto', 'Oculto'), ) categoria = models.CharField(max_length=50, unique=True) titulo = models.CharField(max_length=255, unique=True) subtitulo = models.CharField(max_length=255) cuerpo_noticia = models.TextField() autor = models.CharField(max_length=255) imagen = models.ImageField(upload_to='imagenes/noticias/') pie_de_foto = models.CharField(max_length=255) estado = models.CharField(max_length=10, choices=ESTADOS, default='pendiente') fecha = models.DateField(auto_now_add=True) def __str__(self): return self.titulo
问题排查及修复方案
从终端日志可见请求为GET且参数附在URL后,结合代码分析,核心问题出在模板语法错误:
表单标签语法错误:模板中
<form>和</form>标签末尾多了多余的反斜杠\>,导致浏览器无法正确解析method="POST"属性,最终默认使用GET提交。
修复:移除标签末尾的反斜杠,改为正确的<form action="" method="POST" enctype="multipart/form-data">和</form>。模板闭合标签缺失:模板最后一行
{% %}是不完整的标签,应改为{% endblock %},缺失闭合标签可能导致页面渲染异常,间接影响表单功能。视图重定向错误:
redirect('ver_noticias_pendietes.html')拼写错误,应使用urls.py中定义的名称ver_noticias_pendientes而非带.html的文件名(此问题不影响请求方法,但需修复避免后续跳转错误)。
修复后的模板代码:
{% extends "homepage/base.html" %} {% block title %}Estacion Obrera{% endblock %} {% block content %} <form action="" method="POST" enctype="multipart/form-data"> {% csrf_token %} {{ form.as_p }} <input type="submit" value="Guardar" class="btn btn-dark"> </form> {% endblock %}
修复后的视图重定向代码:
return redirect('ver_noticias_pendientes')
若修复后仍为GET请求,需检查base.html中是否存在干扰表单提交的JavaScript代码(如阻止默认提交事件),或是否存在表单嵌套的情况(HTML不允许表单嵌套)。
内容的提问来源于stack exchange,提问作者Sofia
相关产品推荐
相关产品推荐

