点击提交按钮时出现Runtime Error(追加斜杠)问题求助
Django表单提交触发Append Slash相关Runtime Error的问题解决
问题描述
用Python+Django开发项目时,点击表单提交按钮出现Runtime Error,报错内容涉及追加斜杠(Append Slash),自行排查未能解决,需要分析原因并给出解决方案。
相关代码
HTML表单代码
<form action="register" method="POST" onsubmit="register"> {% csrf_token %} <h3>my information</h3> <h4>Contact us today, and get reply with in 24 hours!</h4> <style> fieldset { border: medium none !important; text-emphasis-color:black; margin: 0 0 10px; min-width: 100%; padding: 0; width: 100%; } </style> <fieldset> <input placeholder="Your name" type="text" tabindex="1" style="background-color:black;" required autofocus> </fieldset><br> <fieldset> <input placeholder="Your Email Address" type="email" tabindex="2" style="background-color:black;" required> </fieldset><br> <fieldset> <input placeholder="Your Phone Number" type="tel" tabindex="3" style="background-color:black;" required> </fieldset><br> <fieldset> <label for="account">Choose a account:</label> <select name="account" id="name"> <option value="netflix">netflix</option> <option value="amazone prime">amazone</option> <option value="disney plus hotstar">hotstar</option> </select> <style> #nname { padding: 5px; color: #f7f7f8; font-size: 12px; background: black; appearance: none; } </style> </fieldset><br> <fieldset> <textarea placeholder="Type your Message Here...." tabindex="5" style="background-color:black;" required></textarea> </fieldset><br> <fieldset> <button name="submit" type="submit" id="contact-submit" style="background-color:rgb(23, 12, 12);" data-submit="...Sending" value="submit">Submit</button> </fieldset><br> </form>
views.py代码
from django.shortcuts import render,redirect from django.contrib.auth.models import User def register(request): name=request.POST['name'] email=request.POST['email'] phone_number=request.POST['phone_number'] account=request.POST['account'] message=request.POST['message'] user=User.objects.create_user(name=name) print("we wil call back you") return redirect('/')
urls.py代码
from django.urls import path from django.views import View from .import views urlpatterns = [ path('',views.index,name='index'), path('contact/', views.contact,name='contact'), path('ideagained/',views.ideagained,name='ideagained'), path('idea/',views.idea,name='idea'), path('ouridea/',views.ouridea,name='ouridea'), path('aboutpage/',views.aboutpage,name='aboutpage'), path('register/',views.register,name='register') ]
原因分析
- URL斜杠不匹配:表单
action设为register(不带斜杠),但urls.py中配置的路径是register/(带斜杠)。Django默认开启APPEND_SLASH=True,当请求不带斜杠的URL时会触发301重定向到带斜杠的版本,但POST请求重定向后会转为GET,导致POST数据丢失,进而触发Runtime Error。 - 表单元素缺少name属性:HTML中除了select外,其他input和textarea都没有设置
name属性,views.py中直接通过request.POST['name']等获取值会抛出KeyError,引发错误。 - onsubmit属性错误:表单的
onsubmit="register"指定了一个未定义的函数,会导致提交时JS报错,中断表单提交流程,间接引发异常。 - User创建方法参数错误:
User.objects.create_user()的必填参数是username,而非name,直接传name=name会导致参数错误,触发异常。 - 视图未处理GET请求:register视图直接读取
request.POST,如果用户通过GET方式访问register/,会直接抛出MultiValueDictKeyError。
解决方案
1. 修正表单action路径
使用Django的URL反向解析,避免硬编码路径,同时确保路径匹配:
<form action="{% url 'register' %}" method="POST">
2. 为表单元素添加name属性
确保每个需要提交的字段都有对应的name,和views中取值的键一致:
<input placeholder="Your name" name="name" type="text" tabindex="1" style="background-color:black;" required autofocus> <input placeholder="Your Email Address" name="email" type="email" tabindex="2" style="background-color:black;" required> <input placeholder="Your Phone Number" name="phone_number" type="tel" tabindex="3" style="background-color:black;" required> <textarea placeholder="Type your Message Here...." name="message" tabindex="5" style="background-color:black;" required></textarea>
3. 移除错误的onsubmit属性
如果没有对应的registerJS函数,直接删除onsubmit="register",避免JS报错:
<form action="{% url 'register' %}" method="POST">
4. 修正User创建代码
使用正确的参数调用create_user,并处理GET请求:
def register(request): if request.method == 'POST': name = request.POST['name'] email = request.POST['email'] phone_number = request.POST['phone_number'] account = request.POST['account'] message = request.POST['message'] # create_user需要username作为必填参数,可同时传递email等字段 user = User.objects.create_user(username=name, email=email) print("we will call back you") return redirect('/') # 处理GET请求,比如返回表单页面或重定向到首页 return redirect('index')
5. 开启DEBUG模式查看详细错误(可选)
确保settings.py中DEBUG=True,这样可以看到具体的错误栈信息,更精准定位问题,上线前记得关闭。
内容的提问来源于stack exchange,提问作者jeeva
相关产品推荐
相关产品推荐

