如何解决如何从Django中的数据库访问特定对象以将其显示在网页中?
我想显示Django中SQLite数据库的标题和描述吗?
我已经从数据库和视图文件中获取了所有数据,我已将其返回为context
。
这是model.py
文件:
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
这是我的views.py
文件:
def home(request):
posts = Post.objects.all()
search_query = request.GET.get('q')
if search_query:
posts = posts.filter(
Q(title__icontains = search_query) |
Q(content__icontains = search_query)
)
context={
'posts': posts
}
return render(request,'blog/home.html',context)
这里是HTML代码:-
{% for post in posts %}
<div class="slide-description">
<label class="slide-0">
<h1 class="text-slide">{{Here i want to print the title 1}}</h1>
<h5>{{Here I want to print the content 1}}</h5>
<a href="/" class="readmorebutton">Read More</a>
</label>
<label class="slide-1">
<h1 class="text-slide">{{Here i want to print the title 2}}</h1>
<h5>{{Here I want to print the content 2}}</h5>
<a href="/" class="readmorebutton">Read More</a>
</label>
<label class="slide-2">
<h1 class="text-slide">{{Here i want to print the title 3}}</h1>
<h5>{{Here I want to print the content 3}}</h5>
<a href="/" class="readmorebutton">Read More</a>
</label>
<label class="slide-3">
<h1 class="text-slide">{{Here i want to print the title 4}}</h1>
<h5>{{Here I want to print the content 4}}</h5>
<a href="/" class="readmorebutton">Read More</a>
</label>
</div>
{% endfor %}
有什么办法可以完成我的工作,即一次显示一个标题,还是像我想说的那样,是否有诸如post.title[1]
,post.title[2]
,...或类似的东西?
解决方法
您可以简单地浏览帖子。
import subprocess
moba_path = "C:\Program Files (x86)\Mobatek\MobaXterm\MobaXterm.exe"
subprocess.run(f'{moba_path} -exec ssh my_server "mkdir test_dir"')
,
您可以获得具有特定标题或内容的对象,如下所示:
@Transactional
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。