在 Django 中的 MySQL 数据库(Navicat Premium)中记录了奇怪的“on”类型

Posted

技术标签:

【中文标题】在 Django 中的 MySQL 数据库(Navicat Premium)中记录了奇怪的“on”类型【英文标题】:Strange "on" type being recorded in MySQL database (Navicat Premium) in Django 【发布时间】:2021-12-31 14:23:36 【问题描述】:

我一直在尝试将带有 html 表单的信息插入到我的数据库中。但是,当我提交时,我的“gender”和“website_rating”奇怪地被替换为“on”,而它应该被替换为我的网站输入。

数据库图片:https://i.stack.imgur.com/94d8L.png

这是我的models.py

# _*_ coding:utf-8 _*_  
from __future__ import unicode_literals
from django.db import models

"""
Quick Guide to Model Fields that We Did Not Use:

    - models.ForeignKey()
    - models.DateTimeField()
    - models.IntegerField()
    - models.IPAddressField()
    - models.FileField()
    - models.ImageField()
"""

# Create your models here.

class PersonalInfoResponse(models.Model):
    # Personal Info Section
    name = models.CharField(max_length=30, null=True, default="N/A", verbose_name=u"Name")
    age = models.IntegerField(verbose_name=u"Age", default="N/A", null=True)
    email = models.CharField(max_length=30, null=True, default="N/A", verbose_name=u"text#@thing.com")
    gender = models.CharField(max_length=7, null=True, verbose_name=u"gender")

    # Website Response Section
    website_rating = models.CharField(max_length=3, verbose_name=u"1-10", null=True)
    heardof_flag = models.BooleanField(verbose_name=u"truefalse", null=True)
    heardof = models.CharField(max_length=255, null=True, verbose_name=u"How did you know about the C&C Education Center?")

    # Misc Section
    fav_subject = models.CharField(max_length=50, null=True, default="None", verbose_name=u"truefalse")
    fav_website = models.CharField(max_length=255, null=True, default="N/A", verbose_name=u"What is your favorite part about this website?")
    website_improve = models.CharField(max_length=255, null=True, default="N/A", verbose_name=u"What is something that this website needs improvement on?")

    class Meta:
        verbose_name = u"User Contact Info"
        verbose_name_plural = verbose_name

这是我的观点.py

from django.shortcuts import render
from django.shortcuts import redirect
from .models import PersonalInfoResponse

# Special Pages

def AfterSubmission(request):
    return render(request, 'html/elements/temp.html')

# Create your views here.

def Home(request):
    return render(request, 'html/index.html')

def Contact(request):
    # all_messages = PersonalInfoResponse.objects.all()
    # for message in all_messages:
    #     print(message.name)
    if (request.method == 'POST'):
        name = str(request.POST.get('name', ''))
        age = int(request.POST.get('age', ''))
        email = str(request.POST.get('email', ''))
        gender = request.POST.get('gender', '')
        website_rating = request.POST.get('rscale', '')
        heardof_flag = request.POST.get('ynw', '')
        heardof = str(request.POST.get('heardof_res', ''))
        fav_subject = str(request.POST.get('subjects', ''))
        fav_website = str(request.POST.get('fav_part', ''))
        website_improve = str(request.POST.get('improve_part', ''))

        # Saves User Input
        usermessage = PersonalInfoResponse()

        usermessage.name = name if isinstance(name, str) else 0
        usermessage.age = age if isinstance(age, int) else 0
        usermessage.email = email if isinstance(email, str) else 0
        usermessage.gender = gender 
        usermessage.website_rating = website_rating
        usermessage.heardof_flag = (1 if heardof_flag == 'Yes' else 0)
        usermessage.heardof = heardof
        usermessage.fav_subject = fav_subject
        usermessage.fav_website = fav_website
        usermessage.website_improve = website_improve

        usermessage.save()
    return render(request, 'html/contactform.html')

def About(request):
    return render(request, 'html/about.html')

# Elements folder

def Register(request):
    return render(request, 'html/elements/register.html')

def Login(request):
    return render(request, 'html/elements/login.html')

def Classes(request):
    return render(request, 'html/elements/classes.html')

“gender”和“website_rating”都是 HTML 单选输入。这是我的 HTML 代码部分:

Gender:<br>
<input type="radio" name="gender" required="required"> Male
<input type="radio" name="gender" required="required"> Female 
<input type="radio" name="gender" required="required"> Other

<br><br>
                
On a scale of 1~10, how do you rate this website?<br> 
<input type="radio" name="rscale" required="required"> 1
<input type="radio" name="rscale" required="required"> 2
<input type="radio" name="rscale" required="required"> 3
<input type="radio" name="rscale" required="required"> 4
<input type="radio" name="rscale" required="required"> 5
<input type="radio" name="rscale" required="required"> 6
<input type="radio" name="rscale" required="required"> 7
<input type="radio" name="rscale" required="required"> 8
<input type="radio" name="rscale" required="required"> 9
<input type="radio" name="rscale" required="required"> 10

我不知道发生了什么。有人可以帮帮我吗?

这是我在 *** 上的第一个问题,如果我遗漏了什么,请随时批评我。

【问题讨论】:

【参考方案1】:

您的输入缺少 values,因此根据 HTML 规范,您会得到 "on",因为选择了单选按钮。

<input type="radio" name="rscale" required="required" value="1"> 1
<input type="radio" name="rscale" required="required" value="2"> 2

等等……

另外,请查看Django's Forms 和ModelForms,而不是手动操作request.POST

第三,如果评级是一个数字,那么:

website_rating = models.IntegerField(verbose_name=u"1-10", null=True)

如果需要,使其不可为空。

website_rating = models.IntegerField(verbose_name=u"1-10")

【讨论】:

非常感谢!这很有帮助。

以上是关于在 Django 中的 MySQL 数据库(Navicat Premium)中记录了奇怪的“on”类型的主要内容,如果未能解决你的问题,请参考以下文章

如何在不使用 Django 模型的情况下使用 MySQL 中的完整数据库

Django中的app及mysql数据库篇(ORM操作)

如何在 django 和 MySQL 中过滤数据库中的两个字段?

Django 链接MySQL及数据操作

这些错误是啥意思? django中的mysql

使用mysql和django按日期时间删除数据库中的条目?