【问题标题】:Trouble getting my HTML button linked to a Python function using Django使用 Django 将我的 HTML 按钮链接到 Python 函数时遇到问题
【发布时间】:2020-03-25 12:00:22
【问题描述】:

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

# 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,在我的模板文件夹中:


<!-- 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 以在之后使用虚拟参数触发

template_name = "index.html"

在views.py中,它添加了虚拟参数就好了,当我点击它时,我似乎无法弄清楚如何让我的表单按钮触发views.py中的python函数。这让我困扰了很长一段时间,经过数小时筛选文档、示例和已回答的问题后,我仍然无法弄清楚。

编辑: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,我会向你调查一下,一会儿:)
  • 能否请您提供您的 characterTable 模型@nunzio?
  • @EliakinCosta 抱歉耽搁了,我刚刚编辑了原始帖子以包含它,非常感谢您愿意看一看!

标签: python html django


【解决方案1】:

在提交数据时,您确实需要将表单方法更改为 POST。之后,你应该在你的视图中实现 post 方法,因为 TemplateView 继承自 View,它工作得很好。

<!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, {})

附:请考虑使用 Django Forms,它将为您节省大量时间和精力。你可以找到如何实现它here

【讨论】:

  • 不客气。请不要忘记将问题标记为已解决:) @nunzio
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-26
  • 1970-01-01
  • 2019-11-27
  • 1970-01-01
  • 2022-11-16
  • 1970-01-01
相关资源
最近更新 更多