簡體   English   中英

如何查看 django 中的 models.py?

[英]How I can give a view to my models.py in django?

我的模型.py 文件

from django.db import models

class Post(models.Model):
    name=models.CharField(max_length=200)
    content=models.CharField(max_length=200)
    pub_time=models.DateTimeField( auto_now=True)

你能告訴我如何把它放在我的views.py和HTML文件中嗎?

如果我正確理解您的問題,您希望訪問模型中的數據並將其傳遞給模板。 為此,

在views.py中

from .models import Post
def your_view_function(request):
    data = Post.objects.all()
    context = { "data" : data, }
    return render(request, "<link_to_your_template>", context)

在您的模板中,

{% for item in data %}
   <h1>{{ item.name }}</h1>
   <h1>{{ item.content }}</h1>
   {% endfor %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM