'How can I store data of registration into my django database?
I want to store data of patient from registration page to my django database using serializers but I am not sure how to do that!!
Here I am attaching views.py file:
from django.http import HttpResponse, HttpResponseForbidden
from django.shortcuts import render,redirect
from django.contrib import messages
from django.contrib.auth import authenticate,login,logout
from rest_framework import viewsets
from .models import *
from .serializers import PatientSerializer
class PatientView(viewsets.ModelViewSet):
queryset = patient.objects.all()
serializer_class = PatientSerializer
def login_user(request):
if request.method == 'POST':
username = request.POST['Username']
password = request.POST['Password']
user_type = request.POST['user_type']
user = authenticate(username=username, password=password)
#doctor is superuser and receptionist is normaluser
if user is None:
login(request, user)
if user_type == 'Doctor':
return render(request,'')
elif user_type == 'Receptionist':
return render(request, 'Auth/registration.html')
else:
return render(request,'')
else:
messages.error(request, "Bad Credentials")
return redirect('login')
return render(request, "Auth/login.html")
def registration(request):
if request.method == "POST":
username = request.POST['username']
PID = request.POST['PID']
Name = request.POST['Name']
Age = request.POST['Age']
DOB = request.POST['DOB']
gender = request.POST['gender']
BG = request.POST['BG']
PN = request.POST['PN']
Add = request.POST['Add']
else:
if request.user.is_staff:
return render(request,'Auth/registration.html')
else:
return HttpResponseForbidden('<h1> 403 Forbidden <br>You are not allowed to access this page.</h1>')
This is my registration.html file :
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="static/CSS/style1.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css"/>
<title>Registration</title>
</head>
<body>
<div class="container">
<div class="wrapper">
<div class="title"><span>Registration Form</span></div>
<form action="#">
<div class="row">
<h3>Patient ID</h3>
<input type="text" name="PID" required>
</div>
<div class="row">
<h3>Name</h3>
<input type="text" name="Name" required>
</div>
<div class="row">
<h3>Age</h3>
<input type="number" name="Age" required>
</div>
<div class="row">
<h3>DOB</h3>
<input type="date" name="DOB" required>
</div>
<div class="row">
<h3>Gender</h3>
<select name="gender">
<option value="none" disabled selected></option>
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">Other</option>
</select>
</div>
<div class="row">
<h3>Blood Group</h3>
<select name="BG" class="bg">
<option value="none" disabled selected></option>
<option value="A+">A+</option>
<option value="A-">A-</option>
<option value="B+">B+</option>
<option value="B-">B-</option>
<option value="AB+">AB+</option>
<option value="AB-">AB-</option>
<option value="O+">O+</option>
<option value="O-">O-</option>
</select>
</div>
<div class="row">
<h3>Phone Number</h3>
<input type="text" name="PN" onkeypress="return event.charCode >= 48 && event.charCode <= 57" minlength="10" maxlength="10" required />
</div>
<div class="row">
<h3>Address</h3>
<input type="text" name="Add" required>
</div>
<div class="row button">
<input type="submit" value="Submit">
</div>
</form>
</div>
</div>
</body>
</html>
This is my models.py file:
from django.db import models
from django import forms
User_type = (
("Doctor", "Doctor"),
("Receptionist", "Receptionist"),
("View Patient","View Patient"),
)
Gender = (
("Male", "Male"),
("Female", "Female"),
("Other","Other"),
)
Blood_group = (
("A+","A+"),
("A-","A-"),
("B+","B+"),
("B-","B-"),
("AB+","AB+"),
("AB-","AB-"),
("O+","O+"),
("O-","O-"),
)
class Doctor(models.Model):
Username = models.CharField(max_length=200)
Password = models.CharField(max_length=32)
type = models.CharField(max_length=10,default='doctor')
def __str__(self):
return self.Username
def is_doctor(self):
return self.type
class receptionist(models.Model):
Username = models.CharField(max_length=200)
Password = models.CharField(max_length=32)
type = models.CharField(max_length=10,default='Receptionist')
def __str__(self):
return self.Username
def is_receptionist(self):
return self.type
class patient(models.Model):
Patient_ID = models.CharField(max_length=200)
Name = models.CharField(max_length=200)
Age = models.CharField(max_length=200)
DOB = models.DateTimeField()
Gender = models.CharField(max_length=200 ,choices=Gender)
BloodGroup = models.CharField(max_length=200 ,choices=Blood_group)
Phone = models.CharField(max_length=200)
Address = models.CharField(max_length=200)
and this is serializers.py file:
from rest_framework import serializers
from .models import *
class PatientSerializer(serializers.ModelSerializer):
class Meta:
model = patient
fields = ("PID", 'Name', 'Age', 'DOB', 'gender', 'bg', 'pn', 'Add' )
I am not sure that serializer is needed or not but one of my friend said that you can use serializers for that so i have used it if it is not necessary that tell me another way also!!
and actually,I am working on this project so somethings may not be done so ignore those things I just want to store Patient info into my database.So please help me to find the solution.Thank you!!
Solution 1:[1]
To save a new entry of an object in your database do it like this, in your registration function in views.py:
from . import patient
new_patient = patient(Name=Name, Age=Age, gender=Gender) #include all properties here
new_patient.save()
Solution 2:[2]
In the directory where your models.py and views.py live create a file if does not already exist forms.py and add the following code.
In forms.py
from . import models
from django import forms
from .models import patient
# Create your forms here
class PatientForm(forms.Form):
class Meta:
model = patient
# If you want all fields of your model then un-comment the below
# and comment the next line
# fields = '__all__'
fields = ("PID", "Name", "Age", "DOB", "gender", "bg", "pn", "Add")
In views.py
from .forms import PatientForm
def registration(request):
form = PatientForm()
# When method is post
if request.method == "POST":
# Populate form with data posted
form = PatientForm(request.POST)
# Checking form validity
if form.is_valid():
# Save data to the db
form.save()
return # do something
# When form is not valid
else:
# Re-render the page with existing data
return render(request, "Auth/registration.html", {"form": form})
# When method is not post
else:
if request.user.is_staff:
return render(request, "Auth/registration.html", {"form": form})
else:
return HttpResponseForbidden("<h1> 403 Forbidden <br>You are not allowed to access this page.</h1>")
In registration.html
<form>
{% csrf_token %}
<!-- Any other thing you want -->
{{ form }}
<!-- Any other thing you want -->
</form>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jelle |
| Solution 2 | shah sawood |
