0

我想要: 1. 将 HTML 表单中的数据保存到 MySQL 2. 从通过 HTML 表单输入的 mySQL 中删除数据。

保存功能正在运行。虽然删除功能有错误。

模型.py

from django.db import models

# Create your models here.
class info_needed(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=200)
    surname = models.CharField(max_length=200)
    email = models.CharField(max_length=200)

    def __str__(self):
        return self.name

视图.py

# Create your views here.
from django.shortcuts import render
from django.http import HttpResponse

from .models import info_needed

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

def message(request):
    if request.method == 'POST':
        fname = request.POST.get("fname")
        lname = request.POST.get("lname")
        mail = request.POST.get("mail")
        action = request.POST.get("submit_button")
        action1 = request.POST.get("delete_button")
        p = info_needed(name = fname , surname = lname, email = mail)
        print("name", fname)
        print("action", action)
        print("action2", action1)
        if action == "Submit":
            p.save()
        elif action1 == "Delete":

            p.delete()
        else:
            return HttpResponse("Wrong Action Provided")

    return(HttpResponse("Thank you for submitting your details"))

网址.py

from django.urls import path

from . import views

urlpatterns = [
    path('',views.index, name = 'index'),
    path('submit/', views.message, name = 'submit'),
]

索引.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My Page</title>
</head>
<body>
<form action="{% url 'submit' %}" method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form }}
  <label for="fname">Name:</label>
  <input type="text" id="fname" name="fname"><br><br>
  <label for="lname">Surname:</label>
  <input type="text" id="lname" name="lname"><br><br>
  <label for="mail">E-Mail:</label>
  <input type="text" id="mail" name="mail"><br><br>
    <label for="submit_button">Save</label>
    <input type="radio"  name="submit_button" id="submit_button" value="Submit">
    <label for="delete_button">Delete</label>
  <input type="radio" name="delete_button" id="delete_button" value="Delete">
    <input type="submit", value="Submit">
</form>
</body>
</html>

删除时出错:

AssertionError at /userdata/submit/
info_needed object can't be deleted because its id attribute is set to None.

主ID是自动生成的,如何删除mysql中姓名、姓氏和电子邮件匹配的行条目?

4

1 回答 1

0

删除此行

id = models.IntegerField(primary_key=True)

你不需要它...... django会自动制作

- 更新

好的,我明白了,您不应该像这样制作 id<input type="text" id="fname" name="fname"><br><br> 所以我建议按照这里的文档:文档然后将此代码放入您的表单中,它将起作用

class DeletableModelForm(forms.ModelForm):
    """
    Model form that allows you to delete the object
    """
    delete = forms.BooleanField(
        initial=False,
        help_text=_('Check this to delete this object')
    )

    def save(self, commit=True):
        if self.cleaned_data['delete']:
            return self.instance.delete()
        return super(DeletableModelForm, self).save()
于 2020-04-30T10:07:54.143 回答