我正在尝试创建一个网站来使用django跟踪我的志愿者的出勤情况,其中将有一个签入/签入和签入/签出按钮,因此当单击签入/签入按钮时,数据将转到那里的数据库没问题,这是在“检出/检出”按钮中,单击检出/检出按钮时,它应该更新行并添加检出/检出的日期/时间。
models.py:
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,)
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/')
template / 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>