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




Chủ Đề