I am beginner to Django. I am creating the program to keep record of blood donor.
While creating donation I want to check whether the person already donated or not. If the person have not donated blood previously then I want to save the data directly and if the person have donated the blood previously I want to check whether the time difference between previously donated date and current date exceeds three months or not. If the time difference exceeds then only I want to save the data else do not allow the donor to donate the blood.
Currently I have not added any custom template I have been using modelform. I have no idea how I can validate the data Below are my codes of models and views file.
Code of models.py
from django.db import models
GENDER_CHOICES = (
('M', 'MALE'),
('F', 'FEMALE'),
('O', 'Others'),
)
BLOOD_GROUP_CHOICES = (
('A+', 'A+'),
('A-', 'A-'),
('B+', 'B+'),
('B-', 'B-'),
('O+', 'O+'),
('O-', 'O-'),
('AB+', 'AB+'),
('AB-', 'AB-'),
)
class Donor(models.Model):
first_name = models.CharField(max_length=20)
middle_name = models.CharField(max_length=20, blank=True)
last_name = models.CharField(max_length=20)
blood_group = models.CharField(choices=BLOOD_GROUP_CHOICES, max_length=3, null=True)
gender = models.CharField(choices=GENDER_CHOICES, max_length=1)
email = models.EmailField(blank=True)
mobile_number = models.CharField(max_length=15)
telephone_number = models.CharField(blank=True, max_length=15)
date_of_birth = models.DateField()
def __str__(self):
return self.first_name + ' ' + self.last_name
class Donation(models.Model):
donor = models.ForeignKey(Donor, related_name='donor_name', on_delete=models.CASCADE)
date_of_donation = models.DateField()
donation_address = models.CharField(max_length=200)
def __str__(self):
return self.donor.first_name
code of views.py
from django.shortcuts import render, redirect
from .forms import DonorForm, DonationForm
from .models import Donor, Donation
from datetime import datetime
def donorPage(request):
donors = Donor.objects.all()
context = {'donor': donors}
return render(request, 'donor/donor_page.html', context)
def createDonor(request):
form = DonorForm()
if request.method == 'POST':
form = DonorForm(request.POST)
if form.is_valid():
form.save()
return redirect('donor_page')
context = {'form': form}
return render(request, 'donor/donor_form.html', context)
def updateDonor(request, pk):
donor = Donor.objects.get(id=pk)
form = DonorForm(instance=donor)
if request.method == 'POST':
form = DonorForm(request.POST, instance=donor)
if form.is_valid():
form.save()
return redirect('donor_page')
context = {'form': form}
return render(request, 'donor/donor_form.html', context)
def deleteDonor(request, pk):
donor = Donor.objects.get(id=pk)
if request.method == "POST":
donor.delete()
return redirect('donor_page')
context = {'item': donor}
return render(request, 'donor/donor_delete.html', context)
def donationPage(request):
donations = Donation.objects.all()
context = {'donation': donations}
return render(request, 'donation/donation_page.html', context)
def createDonation(request):
form = DonationForm()
if request.method == 'POST':
form = DonationForm(request.POST)
if form.is_valid():
form.save()
return redirect('donation_page')
context = {'form': form}
return render(request, 'donation/donation_form.html', context)
def updateDonation(request, pk):
donation = Donation.objects.get(id=pk)
form = DonationForm(instance=donation)
if request.method == 'POST':
form = DonationForm(request.POST, instance=donation)
if form.is_valid():
form.save()
return redirect('donation_page')
context = {'form': form}
return render(request, 'donation/donation_form.html', context)
def deleteDonation(request, pk):
donation = Donation.objects.get(id=pk)
if request.method == "POST":
donation.delete()
return redirect('donation_page')
context = {'item': donation}
return render(request, 'donation/donation_delete.html', context)
Code of forms.py
from django.forms import ModelForm
from .models import Donor, Donation
class DonorForm(ModelForm):
class Meta:
model = Donor
fields = '__all__'
class DonationForm(ModelForm):
class Meta:
model = Donation
fields = '__all__'