我是Django的新手,我要做的就是从postgres数据库中提取一个表并将其发布到html中,当我运行runserver时,它必须显示表中的所有记录,因为我正在测试我在Html.py中仅使用了4列
Models.py:
from django.db import models
from django.contrib.gis.db import models
#class Shop(models.Model):
class Anchor(models.Model):
# site_cd_name = models.CharField(max_length=100)
# site_lat = models.FloatField(max_length=100)
# site_long = models.FloatField(max_length=100)
record_type = models.CharField(max_length=2, null=True)
unique_system_identifier = models.DecimalField(max_digits=9,decimal_places=0, null=False)
uls_file_number=models.CharField(max_length=14,null=True)
ebf_number=models.CharField(max_length=30, null=True)
call_sign=models.CharField(max_length=10, null=True)
partition_area_idnumeric=models.DecimalField(decimal_places=0,max_digits=9, null=True)
lower_frequency=models.DecimalField(decimal_places=8,max_digits=16, null=True)
upper_frequency=models.DecimalField(decimal_places=8,max_digits=16, null=True)
def_und_indicator=models.CharField(max_length=1,null=True)
defined_partition_area=models.CharField(max_length=6,null=True)
class Meta:
db_table = 'mf'
def __unicode__(self):
return "%s %d %s %s %s %d %d %d %s %s" %(self.record_type,self.unique_system_identifier,self.uls_file_number,self.ebf_number,self.call_sign,
self.partition_area_idnumeric,self.lower_frequency,self.upper_frequency,self.def_und_indicator,self.defined_partition_area)
Views.py:
from django.shortcuts import render, get_object_or_404, render_to_response
from django.http import HttpResponse
from django.views import generic
from django.contrib.gis.geos import fromstr
from django.contrib.gis.db.models.functions import Distance
from django.contrib.gis.geos import Point
from .models import Anchor
from django.template import RequestContext
# Create your views here.
def index(request):
model= Anchor
context_object_name= "Anchors"
data = Anchor.objects.all()
# record_type1 = queryset.record_type
template_name="Anchors/index.html"
context = {
'context_object_name': context_object_name,
}
# html = "<html><body>Room 1 will be used by %s</body></html>" % record_type1
return render(request, 'index.html', context=context)
Index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>sample print of data</h1>
<p>
{% for record in data %}
{{ data.unique_system_identifier}}
{{ data.uls_file_number }}
{{ data.lower_frequency }}
{{ data.upper_frequency }}
{% endfor %}
</p>
<!-- <a href="/">logout</a>
--></body>
</html>
urls.py:
from django.contrib import admin
from django.urls import path
from anchors import views
urlpatterns = [path("", views.index,name="index"), path("admin/", admin.site.urls)]
我无法理解我在哪里做错
答案 0 :(得分:1)
在视图和模板中,您的代码仍然存在一些问题。
def index(request):
data = Anchor.objects.all()
template_name="Anchors/index.html"
context = {
'data': data
}
return render(request, 'index.html', context)
...
{% for record in data %}
{{ record.unique_system_identifier}}
{{ record.uls_file_number }}
{{ record.lower_frequency }}
{{ record.upper_frequency }}
{% endfor %}