Cách lấy dữ liệu từ cơ sở dữ liệu ở dạng html bằng python django

Tôi còn khá mới với Django. Tôi muốn lưu dữ liệu từ biểu mẫu đã điền, đây là dữ liệu được điền vào bảng chấm công. html vào Database sau đó lấy dữ liệu và hiển thị thành bảng list_timetable. html. Nhưng tôi bị kẹt và sẽ gặp lỗi thuộc tính trên lượt xem. py "query_results = ltimesheet. các đối tượng. tất cả các()"

thời gian biểu. html

{% extends 'hrfinance/base.html' %}
{% block title %} Timesheet {% endblock %}
{% block link %}
{% load staticfiles %}

 

{% endblock %}
{% block body %}

Submit Timesheet







Start Date: {{timesheet.startDate}}

End Date: {{timesheet.endDate}}










By checking this box I agree that I have satisfied all requirements to continue receiving my scholarship allowance.


{% endblock %}

list_timesheet. html - được sử dụng để hiển thị dữ liệu trong bảng

{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')

người mẫu. py

from django.db import models

from django.http import HttpResponseRedirect
from django.shortcuts import render

#consists of all the details in the timesheet
class Timesheet(models.Model):
studentID = models.CharField("Student ID", max_length=8, primary_key=True, default="")
studentName = models.CharField("Student Name", max_length=500, default="")
startDate = models.DateField("Start Date", max_length=8)
endDate = models.DateField("End Date", max_length=8)

def __str__(self):
    return self.studentID

#consists of all the details of the timesheet under 'View Timesheets'        

class LTimesheet(models.Model):
timesheet = models.ForeignKey(Timesheet, on_delete=models.CASCADE)
status = models.CharField("Status", max_length=100)

Django is often called “Batteries Included Framework” because it has a default setting for everything and has features that can help anyone develop a website rapidly. Talking about forms, In HTML, a form is a collection of elements inside

that allow a visitor to do things like entering text, select options, manipulate objects or controls, and so on, and then send that information back to the server. Basically, it is a collection of data for processing it for any purpose including saving it in the database or fetching data from the database. Django supports all types of HTML forms and rendering data from them to a view for processing using various logical operations.

Để biết thêm về các biểu mẫu HTML, hãy truy cập HTML. thẻ biểu mẫu

Django cũng cung cấp tính năng tích hợp sẵn của Django Forms giống như Django Models. Người ta có thể tạo các biểu mẫu trong Django và sử dụng chúng để lấy dữ liệu từ người dùng một cách thuận tiện.
Để bắt đầu với biểu mẫu, người ta cần làm quen với các yêu cầu GET và POST trong biểu mẫu.

  • LẤY. Ngược lại, GET gộp dữ liệu đã gửi vào một chuỗi và sử dụng dữ liệu này để soạn một URL. URL chứa địa chỉ nơi dữ liệu phải được gửi, cũng như các khóa và giá trị dữ liệu. Bạn có thể thấy điều này đang hoạt động nếu bạn thực hiện tìm kiếm trong tài liệu Django, tài liệu này sẽ tạo ra một URL có dạng https. // tài liệu. djangoproject. com/search/?q=forms&release=1
  • BƯU KIỆN. Bất kỳ yêu cầu nào có thể được sử dụng để thay đổi trạng thái của hệ thống – ví dụ: yêu cầu tạo ra các thay đổi trong cơ sở dữ liệu – nên sử dụng POST

Kết xuất biểu mẫu HTML trong Django Giải thích

Minh họa Biểu mẫu Django bằng Ví dụ. Hãy xem xét một dự án tên là geeksforgeeks có một ứng dụng tên là geeks

Tham khảo các bài viết sau để biết cách tạo project và app trong Django

  • Làm cách nào để tạo một dự án cơ bản bằng MVT trong Django?
  • Làm cách nào để tạo một ứng dụng trong Django?

Hãy tạo một biểu mẫu HTML đơn giản để cho biết cách bạn có thể nhập dữ liệu từ người dùng và sử dụng dữ liệu đó trong chế độ xem của mình. Nhập mã sau vào chuyên viên máy tính> mẫu> nhà. html

HTML




<form action ="" method =

{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
0
{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
1

{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
2______3form1 form2=form4form5form1
{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
1

{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
2<action0 action1=form4 action4=action6 action7=form4
{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
1

{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
2__<action0 action4==6 =7==9
{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
1

""1____4

{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
1

Bây giờ để hiển thị nó trong chế độ xem của chúng tôi, chúng tôi cần sửa đổi các url. py cho ứng dụng chuyên viên máy tính.
Nhập mã sau vào geeksforgeeks > url. p

Python3




""4 ""5""6 ""7

 

""8

""4 method0""6 method2

 

method3= method5

{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
2method7

method8

Bây giờ, hãy chuyển đến home_view của chúng tôi và bắt đầu kiểm tra xem chúng tôi sẽ lấy dữ liệu như thế nào. Toàn bộ dữ liệu từ biểu mẫu HTML trong Django được chuyển dưới dạng đối tượng JSON được gọi là yêu cầu. Trước tiên hãy tạo một chế độ xem và sau đó chúng tôi sẽ thử tất cả các phương pháp để tìm nạp dữ liệu từ biểu mẫu

Python3




""4 =0""6 =2

 

=3

=4 =5

 

{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
2=7

{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
2=9
{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
00
{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
01
{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
02

Khi chúng tôi đã thiết lập mọi thứ, hãy để chúng tôi chạy quản lý Python. py chạy máy chủ và kiểm tra xem biểu mẫu có trên trang chủ không

Cách lấy dữ liệu từ cơ sở dữ liệu ở dạng html bằng python django

Theo mặc định, mọi biểu mẫu từng được viết bằng HTML đều đưa ra yêu cầu GET ở mặt sau của ứng dụng, yêu cầu GET thường hoạt động bằng cách sử dụng các truy vấn trong URL. Hãy chứng minh điều đó bằng biểu mẫu trên, Điền vào biểu mẫu bằng tên của bạn và hãy kiểm tra xem điều gì sẽ xảy ra

Cách lấy dữ liệu từ cơ sở dữ liệu ở dạng html bằng python django

URL ở trên được nối với thuộc tính tên của thẻ đầu vào và tên được nhập trong biểu mẫu. Đây là cách yêu cầu GET hoạt động bất kể số lượng đầu vào mà chúng sẽ được thêm vào URL để gửi dữ liệu đến mặt sau của ứng dụng. Hãy kiểm tra xem làm thế nào để cuối cùng có được dữ liệu này trong chế độ xem của chúng tôi để logic có thể được áp dụng dựa trên đầu vào.
Trong lượt xem. p

Python3




""4 =0""6 =2

 

=3

=4 =5

{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
2____111
{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
12

{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
2=9
{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
00
{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
01
{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
02

Bây giờ khi chúng tôi điền vào biểu mẫu, chúng tôi có thể thấy đầu ra trong thiết bị đầu cuối như bên dưới

Cách lấy dữ liệu từ cơ sở dữ liệu ở dạng html bằng python django

yêu cầu. GET trả về một từ điển truy vấn mà người ta có thể truy cập giống như bất kỳ từ điển python nào khác và cuối cùng sử dụng dữ liệu của nó để áp dụng một số logic.
Tương tự, nếu phương thức truyền là POST, bạn có thể sử dụng request. POST dưới dạng từ điển truy vấn để hiển thị dữ liệu từ biểu mẫu thành dạng xem.

Ở nhà. html

HTML




<form action ="" method =

{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
25
{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
1

{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
2____128

{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
2______3form1 form2=form4form5form1
{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
1

{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
2<action0 action1=form4 action4=action6 action7=form4
{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
1

{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
2__<action0 action4==6 =7==9
{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
1

""1____4

{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
1

Lưu ý rằng bất cứ khi nào chúng tôi tạo yêu cầu biểu mẫu, Django đều yêu cầu bạn thêm {% csrf_token %} vào biểu mẫu vì mục đích bảo mật 
Bây giờ, trong chế độ xem. py hãy kiểm tra xem yêu cầu nào. POST đã có.

Python3




""4 =0""6 =2

 

=3

=4 =5

{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
2____111form43

{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
2=9
{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
00
{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
01
{% block body %}

    { % for item in query_results % }
    
    { % end for % }

    
        Student ID
        Student Name
        Start Date
        End Date
        Status
    
        {{ltimesheet.timesheet}}
        {{ltimesheet.status}}
    

{% endblock %}

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Timesheet, LTimesheet, Application, Scholarship

def ltimesheet(request):
query_results = ltimesheet.objects.all()
return render(request, 'hrfinance/list_timesheet.html')
02

Bây giờ khi chúng tôi gửi biểu mẫu, nó sẽ hiển thị dữ liệu như bên dưới

Cách lấy dữ liệu từ cơ sở dữ liệu ở dạng html bằng python django

Bằng cách này, người ta có thể sử dụng dữ liệu này để truy vấn vào cơ sở dữ liệu hoặc để xử lý bằng cách sử dụng một số thao tác logic và chuyển bằng cách sử dụng từ điển ngữ cảnh cho mẫu

Làm cách nào để truy xuất dữ liệu từ cơ sở dữ liệu ở dạng html Django?

Hiển thị dữ liệu từ cơ sở dữ liệu trong Django bằng mã ví dụ .
Kết nối PHP với cơ sở dữ liệu MySQL
Chèn dữ liệu vào bảng PHPMyAdmin
Lấy dữ liệu từ bảng MySQL
Hiển thị dữ liệu trong bảng HTML
Tự kiểm tra để chèn dữ liệu
Hiển thị dữ liệu bằng thủ tục MySQLi
Hiển thị dữ liệu bằng hướng đối tượng MySQLi

Làm cách nào để lấy dữ liệu từ cơ sở dữ liệu trong Python Django?

Bộ truy vấn Django Lấy dữ liệu .
Phương thức value(). Phương thức value() cho phép bạn trả về từng đối tượng dưới dạng từ điển Python, với tên và giá trị dưới dạng cặp khóa/giá trị. .
Trả về các cột cụ thể. Phương thức value_list() cho phép bạn chỉ trả về các cột mà bạn chỉ định. .
Trả lại hàng cụ thể

Làm cách nào để hiển thị dữ liệu từ cơ sở dữ liệu trong html bằng Python?

Làm cách nào để tìm nạp dữ liệu từ cơ sở dữ liệu bằng Python và hiển thị bảng HTML? .
Kết nối với MySQL từ Python
Xác định truy vấn CHỌN SQL
Nhận đối tượng con trỏ từ kết nối
Thực thi truy vấn SELECT sử dụng phương thức exec()
Trích xuất tất cả các hàng từ một kết quả
Lặp lại từng hàng
Đóng đối tượng con trỏ và đối tượng kết nối cơ sở dữ liệu

Làm cách nào để hiển thị dữ liệu từ cơ sở dữ liệu trên trang web ở Django?

hiển thị dữ liệu từ cơ sở dữ liệu trong django .
dữ liệu = Sinh viên. các đối tượng. tất cả các()
stu = {
"số_học_viên". dữ liệu
trả về render_to_response("đăng nhập/hồ sơ. html", stu)
// trong tệp html
{% cho sinh viên trong student_number %}