简体   繁体   English

如何在 Django 中获取特定用户的信息(我想获取现在已登录的特定用户的约会历史记录)

[英]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 #for 添加约会

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,添加一个关系属性,将约会 model 指向用户 model 以便知道谁是正在接受治疗的患者......像这样,

Models.py:模型.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视图.py

def appointmentInfo(request):

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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