使用Django将我的HTML按钮链接到Python函数时出现问题 [英] Trouble getting my HTML button linked to a Python function using Django

查看:38
本文介绍了使用Django将我的HTML按钮链接到Python函数时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Django的localhost开发服务器上托管的HTML页面格式中获取数据,并将其发送到Models中的数据库.我正在尝试遵循解决方案,但是我不确定如何将函数链接到对HTML的引用在views.py中.这是我目前的views.py:

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"

这是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>

我的问题是,如果我对addCharacter进行硬编码,以便在

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

template_name ="index.html"

template_name = "index.html"

在views.py中,它很好地添加了虚拟参数,当我单击它时,我似乎无法弄清楚如何获取表格按钮来启动views.py中的python函数.这使我困扰了一段时间,经过数小时的筛选,示例和回答的问题,我仍然无法弄清楚.

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.

我在models.py中的characterTable:

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 )

推荐答案

提交数据时,您确实需要将表单方法更改为POST.之后,您应该在视图中将post方法实现为 TemplateView 继承自查看,效果很好.

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.请考虑使用Django Forms,它将为您节省大量时间和精力.您可以在此处找到实现方法>

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

这篇关于使用Django将我的HTML按钮链接到Python函数时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆