简体   繁体   中英

how to get information for specific user in Django ( i wanted to get Appointment history for specific user who is now logged in )

#for for adding appointment

def book_appointment(request):
    if request.method=="POST":
        specialization=request.POST['specialization']
        doctor_name=request.POST['doctor']
        time=request.POST['time']
        app=Appointment(specialization=specialization, doctor_name=doctor_name, time=time)
        app.save()  
    messages.success(request, "Successfully Appointment booked tap on view appointment for status")
    return redirect('home')

#view appointment history

  def appointmentInfo(request):
    appts= Appointment.objects.all() 
    return render(request,'home/view_appointment.html',{'appoints':appts})     

#login

def handeLogin(request):
    if request.method=="POST":
        # Get the post parameters
        loginusername=request.POST['loginusername']
        loginpassword=request.POST['loginpassword']
        user=authenticate(username= loginusername, password= loginpassword)
        if user is not None:
            login(request, user)
            messages.success(request, "Successfully Logged In")
            return redirect("home")
        else:
            messages.error(request, "Invalid credentials! Please try again")
            return redirect("home")  
    return HttpResponse("404- Not found") 
    return HttpResponse("login")

If you want the records of the current User,

add a relationship attribute pointing the appointments model to the Users model so as to know who is the patient being treated... something like this,

Models.py:

#import for User model
from django.contrib.auth.models import User

class Appointment(models.Model):
    sno= models.AutoField(primary_key=True)
    doctor_name= models.CharField(max_length=255)
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    specialization= models.CharField(max_length=23)
    fee=models.CharField(max_length=20)
    #email= models.CharField(max_length=100)
    #time= models.TextField()
    #timeStamp=models.DateTimeField(auto_now_add=True, blank=True)

views.py

def appointmentInfo(request):

    appts= Appointment.objects.get(user__id = request.user.id)

    return render(request,'home/view_appointment.html',{'appoints':appts})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM