简体   繁体   English

如何查看 django 中的 models.py?

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

My models.py File我的模型.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)

Can You tell me how I can put this in my views.py and HTML file?你能告诉我如何把它放在我的views.py和HTML文件中吗?

If I understand your question correctly, you want to access the data from your models and pass it on to a template.如果我正确理解您的问题,您希望访问模型中的数据并将其传递给模板。 In order to do this,为此,

in views.py在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)

in your template,在您的模板中,

{% 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