0

I am attempting to take data from an HTML page form hosted on Django's localhost development server and send it to a database in Models. I'm attempting to follow this solution, but I'm not sure how to link the function to the reference to the HTML in views.py. Here's my views.py currently:

# djangotemplates/example/views.py
from django.shortcuts import render
from django.views.generic import TemplateView # Import TemplateView
from django.http import HttpResponse
from pathfinder.models import characterTable

def addCharacter(sUserID, sPlayerName, sRace, sPlayerClass, sStr, sCon, sDex, sInt, sWis, sCha):
    c = characterTable()
    c.userID=sUserID
    c.playerName = sPlayerName
    #... rest of fields go here
    c.save()

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')))


# Add the two views we have been talking about  all this time :)
class HomePageView(TemplateView):
    template_name = "index.html"

class AboutPageView(TemplateView):
    template_name = "about.html"

And here is the HTML, in my templates folder:


<!-- djangotemplates/example/templates/index.html-->

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Welcome Home</title>
</head>
<body>

  <a href="{% url 'home' %}">Go Home</a>
  <a href="{% url 'about' %}">About This Site</a>

  <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>
</form>

</body>
</html>

My issue is that if I hardcode addCharacter to fire with dummy parameters right after

template_name = "index.html"

in views.py, it adds the dummy parameters just fine, I just can't seem to figure out how to get my form button to fire off the python functions in views.py when I click it. This has held me up for quite a while, and after hours upon hours of sifting through documentation, examples, and answered questions, I still can't figure it out.

EDIT: my characterTable in models.py:


from django.db import models

class characterTable(models.Model):
    userID = models.CharField(max_length = 32)
    playerName = models.CharField(max_length = 32)
    race = models.CharField(max_length = 32)
    playerClass = models.CharField(max_length = 32)
    strength = models.CharField(max_length = 2)
    dexterity = models.CharField(max_length = 2)
    constitution = models.CharField(max_length = 2)
    intelligence = models.CharField(max_length = 2)
    wisdom = models.CharField(max_length = 2)
    charisma = models.CharField(max_length = 2)

    def __str__(self):
        return (self.userID + ": " + self.playerName )
nunzio
  • 51
  • 1
  • 2
  • 7

1 Answers1

0

You do need to change your form method to POST as you are submitting data. After that, you should implement post method within your view as TemplateView inherits from View, it works just fine.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Welcome Home</title>
</head>
<body>

  <a href="{% url 'home' %}">Go Home</a>
  <a href="{% url 'about' %}">About This Site</a>

  <form name = "characterForm" id = "characterForm" method="POST">
    {% csrf_token %}
  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>
</form>

</body>
</html>

views.py

...
class HomePageView(TemplateView):
    template_name = "index.html"

    def post(self, request, *args, **kwargs):
        userID = 'testUser'
        addCharacter(
            userID,
            str(request.POST.get('characterName')),
            str(request.POST.get('race')),
            str(request.POST.get('class')),
            str(request.POST.get('strength')),
            str(request.POST.get('dexterity')),
            str(request.POST.get('constitution')),
            str(request.POST.get('intelligence')),
            str(request.POST.get('wisdom')),
            str(request.POST.get('charisma'))
        )

        return render(request, self.template_name, {})

P.S. Please consider using Django Forms, it will save you a lot of time and effort. You can find how to implement it here

Eliakin Costa
  • 930
  • 6
  • 16