Вытащите данные из Postgres и покажите их в шаблоне - PullRequest
1 голос
/ 02 апреля 2019

Я новичок в Django, все, что я пытаюсь сделать, это извлечь таблицу из базы данных postgres и опубликовать ее в формате html. Когда я запускаю сервер запуска, он должен отображать все записи из таблицы, так как я тестирую используется только 4 столбца в Html.py

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)]

Я не могу понять, где я делаю неправильно

1 Ответ

1 голос
/ 02 апреля 2019

Все еще есть некоторые проблемы с вашим кодом, как в представлении, так и в шаблоне.

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 %}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...