Я хочу: 1. сохранить данные из формы HTML в MySQL 2. удалить данные из mySQL, введенного через форму HTML.
Функциональность сохранения работает , Пока есть ошибка в функции удаления.
Model.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
views.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"))
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.index, name = 'index'),
path('submit/', views.message, name = 'submit'),
]
index. 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.
Первичный идентификатор генерируется автоматически, как удалить запись строки в mysql, где имя, фамилия, и адрес электронной почты соответствует?