0

I am using an HTML page in Django's Views as well as other helper functions in Views to take data from form fields in the HTML page and send them to one of the Models databases through Django.

This is all done off of my local host. I'm basing what I'm doing off of this solution.

When I hard code an entry and send it from views to models, there are no issues.

When I try to take data from the HTML field and then send it, I get a code 200, but when I log into the admin panel and try to access the database causes the local host server to crash, and the data is not entered into the table. Below is my code:

Views Functions for getting and sending table entry to Models


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 in Views:


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)

And here is the console log that I get in powershell when I navigate to the characterCreator page, enter data, submit the data, and log into the admin panel to check if the table has changed:


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

I'm not sure what's causing the issue, whether it is my Python functions or some internal server issue that's unrelated. I don't understand why it works with a hardcoded table entry, but not with this, as this

[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

was returned with code 200 when I go through entry through the webpage. Could it be an issue with my http request?

Tms91
  • 3,456
  • 6
  • 40
  • 74
nunzio
  • 51
  • 1
  • 2
  • 7
  • Try changing the line `` to `` in your html page. I read in an earlier post that a similar issue was solved doing this. Also, why do you have two lines in your html page defining the button? There should only be one. This might also be the issue in your code. – Mehran Nov 27 '19 at 19:08
  • Not related to your problem, but you definitely shouldn't be using a GET request to create items in the db. It should be a POST. – Daniel Roseman Nov 27 '19 at 19:26
  • So I messed around with a few things, and concluded that leaving the form button as type "submit" still provides a string printout in the console, but having it as type "button" causes it to do nothing whatsoever. I ironed out all the annoying 404s with it looking for CSS forms that aren't there. My addCharacter function is only working at the moment if it is called in the characterCreator (function housing the page) with hardcoded parameters and does not seem to work properly when it is called by request_page, which poses many new questions – nunzio Nov 27 '19 at 21:20

1 Answers1

0

Maybe try it:

def addCharacter(sUserID, sPlayerName, sRace, sPlayerClass, sStr, sCon, sDex, sInt, sWis, sCha):
    c = characterTable()
    c.userID = userID 
    [...]
    c.save()
Maddie Graham
  • 2,019
  • 4
  • 25
  • 51