如何从Django数据库中获取数据并以html显示

时间:2018-08-05 15:16:39

标签: django

我是django的新手,我正在尝试根据密钥从数据库中获取数据并将其包含到html

这是我的模型。py

@XmlRootElement(name = "DATA_DS")
@XmlAccessorType(XmlAccessType.FIELD)
public class PaymentTerm {

    @XmlElement(name = "TERM_ID")
    private double termId;

    @XmlElement(name = "NAME")
    private String termName;



    public double getTermId() {
        return termId;
    }
    public void setTermId(double termId) {
        this.termId = termId;
    }
    public String getTermName() {
        return termName;
    }
    public void setTermName(String termName) {
        this.termName = termName;
    }



}

这是我的forms.py

jaxbContext = JAXBContext.newInstance(PaymentTerm.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(xmlString);
PaymentTerm paymentTerm = (PaymentTerm) unmarshaller.unmarshal(reader);

这是我的views.py

from django.db import models

# Create your models here.
class Car(models.Model):
    billing_number = models.BigIntegerField(primary_key=True)
    customer = models.CharField(max_length=100)
    price = models.IntegerField()
    purchase_date = models.DateTimeField()
    company = models.CharField(max_length=100)
    model = models.CharField(max_length=100)
    serial_number = models.BigIntegerField()
    mfg_date = models.DateTimeField()
    shipping_date = models.DateTimeField()

如何更改它们以根据键值显示数据

1 个答案:

答案 0 :(得分:1)

如果要获取数据而不更改页面,请使用Ajax(异步JavaScript)

  1. 从ajax发送数据以查看
  2. 在视图中,将数据准备为json并将其返回

要返回它们,您可以使用JsonResponse