Python 如何使用表单更新django中的行?
我正在尝试使用django创建一个网站来跟踪我的志愿者的出勤情况,会有一个签入和签出按钮,因此当点击签入按钮时,数据会进入数据库,没有问题,问题出在签出按钮上,单击“签出”按钮时,应更新行并添加签出的日期/时间 models.py:Python 如何使用表单更新django中的行?,python,django,django-models,django-rest-framework,django-forms,Python,Django,Django Models,Django Rest Framework,Django Forms,我正在尝试使用django创建一个网站来跟踪我的志愿者的出勤情况,会有一个签入和签出按钮,因此当点击签入按钮时,数据会进入数据库,没有问题,问题出在签出按钮上,单击“签出”按钮时,应更新行并添加签出的日期/时间 models.py: from django.db import models from django.forms import ModelForm # Create your models here. class Volunteer(models.Model): full_
from django.db import models
from django.forms import ModelForm
# Create your models here.
class Volunteer(models.Model):
full_name = models.CharField(max_length=200)
phone_number = models.CharField(max_length=30)
email = models.CharField(max_length=255)
national_id = models.CharField(max_length=255)
def __str__(self):
return self.full_name
class Login(models.Model):
full_name = models.CharField(max_length=200,default="", null=True,)
national_id = models.CharField(max_length=200,default="", null=True,)
check_in = models.DateTimeField(auto_now_add=True)
check_out = models.DateTimeField(auto_now=True)
check_in.editable=True
check_out.editable=True
def __str__(self):
return self.full_name
views.py:
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from .models import Volunteer, Login
from django import forms
# Create your views here.
def volunteerView(request):
if request.method=='POST':
print ("Recieved a POST request")
form=LoginForm(request.POST)
if form.is_valid():
print ("FORM is valid")
else:
print ("FORM is unvalid")
all_volunteers = Volunteer.objects.all()
return render(request, 'volunteer.html',
{'all_volunteers': all_volunteers, 'form':LoginForm()})
def loginView(request):
login_view = Login.objects.all()
return render(request, 'login.html',
{'login_view': login_view})
def addVolunteer(request):
new_volunteer = Volunteer(full_name = request.POST['full_name'],
phone_number = request.POST['phone_number'],
email = request.POST['email'],
national_id = request.POST['national_id'],
)
new_volunteer.save()
return HttpResponseRedirect('/')
def addChekIn(request):
new_checkin = Login(
national_id = request.POST['national_id'],
full_name = request.POST['full_name'],
)
new_checkin.save()
return HttpResponseRedirect('/login/')
模板/login.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Ertiqa | Login</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<style>
h1{
color: white;
}
</style>
<body>
<form action="/addCheckIN/" method="POST" class="container">
{{ form.as_p }}
{% csrf_token %}
<h1>Check IN</h1>
<h3>Full Name</h3>
<input type="text" name="full_name"><br>
<h3>National ID</h3>
<input type="text" name="national_id"><br>
<input type="submit" value="Check IN" class="btn btn-primary">
</form>
</body>
</html>
Ertiqa |登录
h1{
颜色:白色;
}
{{form.as_p}}
{%csrf_令牌%}
登记入住
全名
国民身份证
我知道你的问题。首先,你的小说有点不对劲。您可以像这样使用模型类:
from django.db import models
from django.forms import ModelForm
# Create your models here.
class Volunteer(models.Model):
full_name = models.CharField(max_length=200)
phone_number = models.CharField(max_length=30)
email = models.CharField(max_length=255)
national_id = models.CharField(max_length=255)
def __str__(self):
return self.full_name
class Login(models.Model):
full_name = models.CharField(max_length=200,default="", null=True)
national_id = models.CharField(max_length=200,default="", null=True)
check_in = models.DateTimeField(auto_now_add=True, editable=True) # <--- You can use editable arg inline.
check_out = models.DateTimeField(auto_now=True, editable=True) # <--- You can use editable arg inline.
def __str__(self):
return self.full_name
最后一个,当您给函数命名时,应该使用下划线而不是大小写。(如def add_check_in())。这只是pep8 Python的拼写规则。
我希望这对你有帮助。嗨!您需要添加您为我们所做的尝试,以帮助您非常感谢!!我是刚到django的,所以我不是很好,谢谢你的帮助。不客气。不要忘记,如果没有人互相帮助,我们就无法生活在这个世界上;)
class Volunteer(models.Model):
full_name = models.CharField(max_length=200, unique=True)
phone_number = models.CharField(max_length=30)
email = models.CharField(max_length=255)
national_id = models.CharField(max_length=255, unique=True)
check_in = models.DateTimeField(auto_now_add=True, editable=True) #<-- auto_now_add=True args mean: when you call .save() method first time, this field is filled in with the current date and time.
check_out = models.DateTimeField(auto_now=True, editable=True) #<-- auto_now=True args mean: when you call .save() method second and next times (every updating), this field is filled in with the current date and time.
def __str__(self):
return self.full_name
def checkout(request):
national_id = request.POST['national_id'], # If national_id is unique, It's enough.
try: #Check this national id is exist in your db.
person = Login.objects.get(national_id=national_id)
person.save()
messages.success(request, "Thank you for checkout blabla")
return HttpResponseRedirect('/login/') # Maybe you can create a success page.
except LoginDoesNotExist: # If doesn't you can show error message.
messages.error(request, "No such registry was found in the system.")
return redirect("/login/")