我在Django的视图中使用HTML页面以及在视图中使用其他功能,以从HTML页面的表单字段中获取数据,并将它们通过Django发送到Models数据库之一。这些都是在本地主机上完成的。我基于this解决方案正在做的事情。当我对条目进行硬编码并将其从视图发送到模型时,就没有问题了。当我尝试从HTML字段中获取数据然后发送时,我得到一个代码200,但是登录到管理面板并尝试访问该数据库会导致localhost服务器崩溃。下面是我的代码:
查看用于获取表条目并将其发送到模型的功能
def addCharacter(sUserID, sPlayerName, sRace, sPlayerClass, sStr, sCon, sDex, sInt, sWis, sCha):
from pathfinder.models import characterTable
c = characterTable(userID = sUserID, playerName = sPlayerName, race = sRace, playerClass = sPlayerClass, strength = sStr, constitution = sCon, dexterity = sDex, intelligence = sInt, wisdom = sWis, charisma = sCha)
c.save()
#this function courtesy of Mahshid Zeinaly on stackoverflow https://stackoverflow.com/a/19761466/12352379
def request_page(request):
if(request.GET.get('mybtn')):
userID = 'testUser'
addCharacter(userID, string(request.GET.get('characterName')), string(request.GET.get('race')), string(request.GET.get('class')), string(request.GET.get('characterName')), string(request.GET.get('strength')), string(request.GET.get('dexterity')), string(request.GET.get('constitution')), string(request.GET.get('intelligence')), string(request.GET.get('wisdom')), string(request.GET.get('charisma')))
视图中的HTML:
def characterCreator(request):
html ='''
<!-- see index.html for in depth comments, this is just a reimplementation of that at the moment -->
<html lang="en">
<head>
<link href = "/static/css/simple-sidebar.css" rel = "stylesheet">
<link href = "/static/css/bootstrap.min.css" rel = "stylesheet">
<link href = "/static/css/bootstrap.css" rel = "stylesheet">
<link href = "/static/css/bootstrap.css.map" rel = "stylesheet">
<link href = "/static/css/bootstrap.min.css.map" rel = "stylesheet">
<link href = "/static/css/bootstrap-grid.css" rel = "stylesheet">
<link href = "/static/css/bootstrap-grid.css.map" rel = "stylesheet">
<link href = "/static/css/bootstrap-grid.min.css" rel = "stylesheet">
<link href = "/static/css/bootstrap-grid.min.css.map" rel = "stylesheet">
<link href = "/static/css/bootstrap-reboot.css" rel = "stylesheet">
<link href = "/static/css/bootstrap-reboot.css.map" rel = "stylesheet">
<link href = "/static/css/bootstrap-reboot.min.css" rel = "stylesheet">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Character Creator</title> <!-- Placeholder-->
<!-- Bootstrap core CSS -->
<!-- Custom styles for this template -->
<link href="css/simple-sidebar.css" rel="stylesheet">
</head>
<body>
<div class="d-flex" id="wrapper">
<!-- Sidebar -->
<div class="bg-light border-right" id="sidebar-wrapper">
<div class="sidebar-heading"> Options: </div>
<div class="list-group list-group-flush">
<a href="http://localhost:8000/" class="list-group-item list-group-item-action bg-light">Home</a>
<a href="http://localhost:8000/characterCreator/" class="list-group-item list-group-item-action bg-dark">Character Creator</a>
<a href="http://localhost:8000/battleSim/" class="list-group-item list-group-item-action bg-light">Battle Simulator</a>
<a href="http://localhost:8000/beginnersGuide/" class="list-group-item list-group-item-action bg-light">Beginner's Guide</a>
<a href="http://localhost:8000/info/" class="list-group-item list-group-item-action bg-light">Info</a>
</div>
</div>
<!-- /#sidebar-wrapper -->
<!-- Page Content -->
<div id="page-content-wrapper">
<nav class="navbar navbar-expand-lg navbar-light bg-light border-bottom">
<!-- Menu Toggle Button -->
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto mt-2 mt-lg-0">
<li class="nav-item">
<a class="nav-link" href="http://localhost:8000/login/">Login</a>
</li>
</ul>
</div>
</nav>
<div class="container-fluid">
<h1 class="mt-4">Character Creator Stuff Here</h1>
<div id = "divBox">
<div id = "formArea">
<form name = "characterForm" id = "characterForm" method = "get" action = "#">
Character Name:<br>
<input type="text" name="characterName" id ="characterName">
<br>
Race:<br>
<select name = "race" id = "race">
<option value = "human"> Human </option>
<option value = "elf"> Elf </option>
<option value = "dwarf"> Dwarf </option>
<option value = "gnome"> Gnome </option>
<option value = "halfling"> Halfling </option>
<option value = "halfElf"> Half-Elf </option>
<option value = "halfOrc"> Half-Orc </option>
</select>
<br>
Class:<br>
<select name = "class" id = "class" onchange="changePic()">
<option value = "fighter"> Fighter </option>
<option value = "rogue"> Rogue </option>
<option value = "wizard"> Wizard </option>
</select>
<br>
Strength:<br>
<input type = "number" name = "strength">
<br>
Dexterity:<br>
<input type = "number" name = "dexterity">
<br>
Constitution:<br>
<input type = "number" name = "constitution">
<br>
Intelligence:<br>
<input type = "number" name = "intelligence">
<br>
Wisdom:<br>
<input type = "number" name = "wisdom">
<br>
Charisma:<br>
<input type = "number" name = "charisma">
<br>
<br><br>
<input type="submit" class="btn" value="Click" name="mybtn">
<br><br>
<!-- <input type="submit" value="Submit" onclick="makeJSON()"> -->
</form>
<button type="button" name="button" onclick="makeJSON()">Submit</button>
</div>
<div id = "pictureBox">
<img id = "characterPic" src = ""> </img>
</div>
</div>
<!--<p>The starting state of the menu will appear collapsed on smaller screens, and will appear non-collapsed on larger screens. When toggled using the button below, the menu will change.</p>
<p>Make sure to keep all page content within the <code>#page-content-wrapper</code>. The top navbar is optional, and just for demonstration. Just create an element with the <code>#menu-toggle</code> ID which will toggle the menu when clicked.</p> -->
</div>
</div>
<!-- /#page-content-wrapper -->
</div>
<!-- /#wrapper -->
<!-- Bootstrap core JavaScript -->
<!-- Menu Toggle Script -->
<script>
$("#menu-toggle").click(function(e) {
e.preventDefault();
$("#wrapper").toggleClass("toggled");
});
</script>
</body>
</html>
'''
return HttpResponse(html)
这是当我导航到characterCreator页面,输入数据,提交数据并登录到管理面板以检查表是否已更改时在Powershell中的控制台日志:
PS J:\School\Full Stack\nov19Branch\Django\Real Project\fullStack> python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
November 26, 2019 - 19:50:05
Django version 2.2.7, using settings 'fullStack.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[26/Nov/2019 19:50:11] "GET / HTTP/1.1" 200 4131
[26/Nov/2019 19:50:11] "GET /static/css/bootstrap.min.css.map HTTP/1.1" 304 0
[26/Nov/2019 19:50:11] "GET /static/css/bootstrap-grid.css HTTP/1.1" 304 0
[26/Nov/2019 19:50:11] "GET /static/css/bootstrap.min.css HTTP/1.1" 304 0
[26/Nov/2019 19:50:11] "GET /static/css/bootstrap.css.map HTTP/1.1" 304 0
[26/Nov/2019 19:50:11] "GET /static/css/bootstrap.css HTTP/1.1" 304 0
[26/Nov/2019 19:50:11] "GET /static/css/bootstrap-grid.css.map HTTP/1.1" 304 0
[26/Nov/2019 19:50:11] "GET /static/css/bootstrap-grid.min.css HTTP/1.1" 304 0
[26/Nov/2019 19:50:11] "GET /static/css/bootstrap-reboot.css HTTP/1.1" 304 0
[26/Nov/2019 19:50:11] "GET /static/css/bootstrap-grid.min.css.map HTTP/1.1" 304 0
[26/Nov/2019 19:50:11] "GET /static/css/bootstrap-reboot.css.map HTTP/1.1" 304 0
[26/Nov/2019 19:50:11] "GET /static/css/bootstrap-reboot.min.css HTTP/1.1" 304 0
Not Found: /css/simple-sidebar.css
[26/Nov/2019 19:50:11] "GET /css/simple-sidebar.css HTTP/1.1" 404 3603
[26/Nov/2019 19:50:13] "GET /characterCreator/ HTTP/1.1" 200 6118
Not Found: /characterCreator/css/simple-sidebar.css
[26/Nov/2019 19:50:13] "GET /characterCreator/css/simple-sidebar.css HTTP/1.1" 404 4681
[26/Nov/2019 19:50:22] "GET /characterCreator/?characterName=test3&race=human&class=fighter&strength=1&dexterity=2&constitution=3&intelligence=4&wisdom=5&charisma=6&mybtn=Click HTTP/1.1" 200 6118
Not Found: /characterCreator/css/simple-sidebar.css
[26/Nov/2019 19:50:22] "GET /characterCreator/css/simple-sidebar.css HTTP/1.1" 404 4681
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 56340)
Traceback (most recent call last):
File "C:\Users\Matt\AppData\Local\Programs\Python\Python38\lib\socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "C:\Users\Matt\AppData\Local\Programs\Python\Python38\lib\socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Users\Matt\AppData\Local\Programs\Python\Python38\lib\socketserver.py", line 720, in __init__
self.handle()
File "C:\Users\Matt\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\servers\basehttp.py", line 171, in handle
self.handle_one_request()
File "C:\Users\Matt\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\servers\basehttp.py", line 179, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "C:\Users\Matt\AppData\Local\Programs\Python\Python38\lib\socket.py", line 669, in readinto
return self._sock.recv_into(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
----------------------------------------
[26/Nov/2019 19:50:27] "GET /admin/ HTTP/1.1" 200 4599
[26/Nov/2019 19:50:27] "GET /static/admin/css/dashboard.css HTTP/1.1" 200 412
[26/Nov/2019 19:50:27] "GET /static/admin/css/base.css HTTP/1.1" 200 16378
[26/Nov/2019 19:50:27] "GET /static/admin/css/responsive.css HTTP/1.1" 200 17944
[26/Nov/2019 19:50:28] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[26/Nov/2019 19:50:28] "GET /static/admin/img/icon-changelink.svg HTTP/1.1" 200 380
[26/Nov/2019 19:50:28] "GET /static/admin/img/icon-addlink.svg HTTP/1.1" 200 331
[26/Nov/2019 19:50:28] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 85692
[26/Nov/2019 19:50:28] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 85876
[26/Nov/2019 19:50:28] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 86184
[26/Nov/2019 19:50:30] "GET /admin/pathfinder/charactertable/ HTTP/1.1" 200 4915
[26/Nov/2019 19:50:30] "GET /static/admin/js/core.js HTTP/1.1" 304 0
[26/Nov/2019 19:50:30] "GET /static/admin/js/admin/RelatedObjectLookups.js HTTP/1.1" 304 0
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 56343)
[26/Nov/2019 19:50:30] "GET /static/admin/js/vendor/jquery/jquery.js HTTP/1.1" 304 0
Traceback (most recent call last):
File "C:\Users\Matt\AppData\Local\Programs\Python\Python38\lib\socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
[26/Nov/2019 19:50:30] "GET /admin/jsi18n/ HTTP/1.1" 200 3223
File "C:\Users\Matt\AppData\Local\Programs\Python\Python38\lib\socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Users\Matt\AppData\Local\Programs\Python\Python38\lib\socketserver.py", line 720, in __init__
self.handle()
[26/Nov/2019 19:50:30] "GET /static/admin/js/actions.js HTTP/1.1" 304 0
File "C:\Users\Matt\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\servers\basehttp.py", line 171, in handle
self.handle_one_request()
File "C:\Users\Matt\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\servers\basehttp.py", line 179, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
[26/Nov/2019 19:50:30] "GET /static/admin/js/jquery.init.js HTTP/1.1" 200 363
File "C:\Users\Matt\AppData\Local\Programs\Python\Python38\lib\socket.py", line 669, in readinto
return self._sock.recv_into(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
----------------------------------------
[26/Nov/2019 19:50:30] "GET /static/admin/js/urlify.js HTTP/1.1" 304 0
[26/Nov/2019 19:50:30] "GET /static/admin/css/changelists.css HTTP/1.1" 200 6170
[26/Nov/2019 19:50:30] "GET /static/admin/js/prepopulate.js HTTP/1.1" 304 0
[26/Nov/2019 19:50:30] "GET /static/admin/js/vendor/xregexp/xregexp.js HTTP/1.1" 304 0
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 56344)
Traceback (most recent call last):
File "C:\Users\Matt\AppData\Local\Programs\Python\Python38\lib\socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "C:\Users\Matt\AppData\Local\Programs\Python\Python38\lib\socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Users\Matt\AppData\Local\Programs\Python\Python38\lib\socketserver.py", line 720, in __init__
self.handle()
File "C:\Users\Matt\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\servers\basehttp.py", line 171, in handle
self.handle_one_request()
File "C:\Users\Matt\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\servers\basehttp.py", line 179, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "C:\Users\Matt\AppData\Local\Programs\Python\Python38\lib\socket.py", line 669, in readinto
return self._sock.recv_into(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
----------------------------------------
[26/Nov/2019 19:50:30] "GET /static/admin/img/tooltag-add.svg HTTP/1.1" 200 331
我不确定是什么原因引起的问题,无论是我的Python函数还是一些无关的内部服务器问题。我不明白为什么它适用于硬编码表项,但不适用于此
当我浏览网页时,[26 / Nov / 2019 19:50:22]“ GET / characterCreator /?characterName = test3&race = human&class = fighter&strength = 1&dexterity = 2&constitution = 3&intelligence = 4&wisdom = 5&charisma = 6&mybtn = Click HTTP / 1.1” 200
返回了代码200。我的http请求可能有问题吗?