简体   繁体   English

如何以更易读的方式显示从后端接收到的数据?我将 react 用于前端

[英]How can I display the data recieved from the backend in a more readable way?I use react for the Frontend

Here is my model:这是我的 model:

class Festival(models.Model):
    name = models.CharField(max_length=100)
    start_date = models.DateTimeField(blank=True, null=True, default=None)
    end_date = models.DateTimeField(blank=True, null=True, default=None)
    number_of_tickets = models.IntegerField(default=50000)
    location = JSONField(blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

    def format_start_date(self):
        return self.start_date.strftime("The Festival will start on: %d %b %Y at: %H:%M:%S")

    def format_end_date(self):
        return self.end_date.strftime("The Festival will end on: %d %b %Y at: %H:%M:%S")

I try to display the output from format_start_date() function using the frontend.我尝试使用前端显示来自 format_start_date() function 的 output 。 how could i do that?我怎么能这样做? At the moment, in List.Item.Meta i access the item.start_date, but it displays the date in an unwanted way.目前,在 List.Item.Meta 我访问 item.start_date,但它以不想要的方式显示日期。 I want it to be more readable, by using format_start_date() function from Festival model.我希望它更具可读性,通过使用来自 Festival model 的 format_start_date() function。 Is it possible?可能吗? if it isn't, what is the right way to do that: ex: if the date retrieved is "2020-06-12T12:00:00Z", i want to have in description: ""The Festival will start on: 12 06 2020 at: 12:00:00".如果不是,那么正确的方法是什么:例如:如果检索到的日期是“2020-06-12T12:00:00Z”,我想在描述中包含:“”节日将开始:12 2020 年 6 月 12:00:00”。

const IconText = ({ icon, text }) => (
<Space>
    {React.createElement(icon)}
    {text}
</Space>
);

const Festivals = (props) => {
    return(

        <List
            itemLayout="vertical"
            size="large"
            pagination={{
            onChange: page => {
                console.log(page);
            },
            pageSize: 3,
            }}
            dataSource={props.data}
            footer={
            <div>
                <b>ant design</b> footer part
            </div>
            }
            renderItem={item => (
            <List.Item
                key={item.title}
                actions={[
                <IconText icon={StarOutlined} text="156" key="list-vertical-star-o" />,
                <IconText icon={LikeOutlined} text="156" key="list-vertical-like-o" />,
                <IconText icon={MessageOutlined} text="2" key="list-vertical-message" />,
                ]}
                extra={
                <img
                    width={272}
                    alt="logo"
                    src="https://gw.alipayobjects.com/zos/rmsportal/mqaQswcyDLcXyDKnZfES.png"
                />
                }
            >
                <List.Item.Meta
                avatar={<Avatar src={item.avatar} />}
                title={<a href={item.href}>{item.name}</a>}
                description={item.start_date}

                />
                {item.content}
            </List.Item>
            )}
        />
    )
}

export default Festivals;

I also tried using the function toLocaleDataString() from the Festival.js file, but it doesn't work.我还尝试使用 Festival.js 文件中的 function toLocaleDataString(),但它不起作用。

You can use momentjs for that in the front end.您可以在前端使用momentjs

To accomplish your goal you need to call moment(YOUR_DATE).format("MM DD YYYY [at] hh:mm") after installing the moment library.为了实现您的目标,您需要在安装 moment 库后调用moment(YOUR_DATE).format("MM DD YYYY [at] hh:mm")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过 Django 后端将数据从 HDFS 发送到 Angular 5 前端? - How can I send data from HDFS through Django backend to Angular 5 frontend? 如何捕获前端响应到Django的错误和异常数据以添加更多上下文 - How can I capture errors and exceptions data from frontend response to django to add more context 将数据从 React 前端传递到 Django 后端 - Passing data from React frontend to Django Backend 如何使用 React 前端渲染来自 Django 模型的数据? - How would I render data from a Django model with a React frontend? 如何将文本字段输入从前端发送到后端 - how do I send a text field input from the frontend to the backend 如何使用后端和前端配置 nginx? - How do I configurate nginx with backend and frontend? 使用 Django 和 React 将 API 数据从后端实时发送到前端 - Send API data from Backend to Frontend in real time with Django and React 我可以使用哪种编程语言将android和ios应用程序开发为前端,将django开发为后端? - which programing language i can use to develop both android and ios applications as frontend and django as backend? 如何在 React 前端使用 Django 的 naturaltime package? - How do I use Django's naturaltime package in a React frontend? 如何将图像添加到我的 django 数据库并在前端显示 - how can I add Image to my django database and display it in frontend
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM