如何在django中将数据库中的内容显示为表格格式?

时间:2018-03-27 13:06:14

标签: python django

class Venue(
    RandomIDMixin,
    TimeStampedModelMixin,
    models.Model
):

# Venue model for storing metadata about a place.

VENUE_TYPES = (
    ('shul', 'Shul'),
    ('hall', 'Hall'),
    ('residence', 'Residence'),
    ('other', 'Other'),
)

address1 = models.CharField(max_length=256, blank=True, default="")
address2 = models.CharField(max_length=256, blank=True, default="")
city = models.CharField(max_length=256)
country = models.ForeignKey('countries.Country', null=True)
description = models.TextField(blank=True, default="")
display_address = models.CharField(max_length=256, blank=True, default="")
image = models.FileField(
    blank=True, null=True,
    upload_to=prefix_venue_images, default='{}{}'.format(settings.MEDIA_URL, settings.VENUE_DEFAULT_IMAGE))
name = models.CharField(max_length=128)
postal_code = models.CharField(max_length=12, blank=True, default="")
region = models.CharField(max_length=128, blank=True, default="")
venue_type = models.CharField(max_length=128, choices=VENUE_TYPES, default='hall')
website_url = models.URLField(max_length=512, blank=True, default="")

class Meta:
    unique_together = ('name', 'address1', 'city')

def __str__(self):
    return '{0}, {1}, {2}'.format(self.name, self.city, self.region)

我想在管理网站上以表格格式显示上述信息。我是一个对django一无所知的初学者。

2 个答案:

答案 0 :(得分:0)

假设我理解正确,您只需在Django应用程序中转到String str2 = str1.replace("^", "\""); 并添加以下行:

admin.py

我建议你按照django教程,从头开始: https://docs.djangoproject.com/en/2.0/intro/tutorial01/

答案 1 :(得分:0)

要在django admin上以表格格式显示此模型,您需要编辑admin.py文件:

from django.contrib import admin
from .models import Venue

class VenueAdmin(admin.ModelAdmin):
    list_display = ('name', 'city', 'region')

admin.site.register(Venue, VenueAdmin)