如何使用 django 制作类似 twitter 的主页?
Posted
技术标签:
【中文标题】如何使用 django 制作类似 twitter 的主页?【英文标题】:How can I make a twitter like homepage with django? 【发布时间】:2021-12-19 18:36:44 【问题描述】:我现在是 Django Web 框架的新手。我想制作一个类似 twitter/facebook 的主页,用户可以在其中发布状态或更新,还可以查看其他 ures 的帖子,但我不能这样做,因为当用户登录时,他会发布更新但可以不认为这是他的家。当他退出时,他可以查看所有帖子。这是我的视图函数。
views.py
def index(request):
posts = NewPost.objects.all().order_by("-timestamp")
if request.user.is_authenticated:
if request.method == "POST":
post = NewPostForm(request.POST)
user = request.user
timestamp = datetime.now()
if post.is_valid:
post = post.save(commit=False)
postdata = NewPost(post=post,user=user,timestamp=timestamp)
postdata.save()
return render(request,"network/index.html",
"post" : post,
"user" : user,
"timestamp" : timestamp
)
else:
post = NewPostForm()
return render(request,"network/index.html",
"post" : post
)
return render(request,"network/index.html",
"posts" : posts
)
urls.py
urlpatterns = [
path("", views.index, name="index"),
path("login", views.login_view, name="login"),
path("logout", views.logout_view, name="logout"),
path("register", views.register, name="register"),
]
模板
% extends "network/layout.html" %
% block body %
<div style="margin: 10px 10px 10px 10px;">
<h1>All Posts</h1>
% if user.is_authenticated %
<div class="border border-success" style="margin: 10px 10px 10px 10px;">
<div style="margin: 10px 10px 10px 10px;">
<h3>New Post</h3>
<form action="% url 'index' %" method="POST">
% csrf_token %
<div class="form-group">
<!-- <textarea name="post" class="form-control" cols="30" rows="5" placeholder="What is in your mind? "></textarea> -->
post.as_table
</div>
<input class="btn btn-success" type="Submit" value="Post">
</form>
</div>
</div>
% endif %
<div id="posts" class="card">
<div class="card-body">
% for posts in posts %
<ul>
<li class="card">
<div class="card-body">
<h5 class="card-title"> posts.user </h5>
<h6 class="card-subtitle text-muted"> posts.timestamp </h6>
<h3 class="card-text"> posts.post </h3>
</div>
</li>
</ul>
% empty %
<h6>No post availabel ????</h6>
% endfor %
</div>
</div>
</div>
% endblock %
如果用户尝试发布更新,他会单击发布按钮,但表单会消失。 my problem
【问题讨论】:
【参考方案1】: 您必须在else
块中的上下文中传递 user
才能使表单出现(如果用户 is_authenticated 则只有您在显示表单)。
您必须将所有帖子传递给上下文才能使所有帖子可见。
这是你必须做的。
def index(request):
posts = NewPost.objects.all().order_by("-timestamp")
if request.user.is_authenticated:
user = request.user # changed here
if request.method == "POST":
post = NewPostForm(request.POST)
timestamp = datetime.now()
if post.is_valid:
post = post.save(commit=False)
postdata = NewPost(post=post,user=user,timestamp=timestamp)
postdata.save()
return render(request,"network/index.html",
"post" : post,
"posts" : posts,
"user" : user,
"timestamp" : timestamp
)
else:
post = NewPostForm()
return render(request,"network/index.html",
"post" : post,
"posts" : posts,
"user" : user, # added here
"timestamp" : timestamp # added now, remove if not needed
)
return render(request,"network/index.html",
"posts" : posts
)
【讨论】:
以上是关于如何使用 django 制作类似 twitter 的主页?的主要内容,如果未能解决你的问题,请参考以下文章
如何在悬停而不是单击时制作 Twitter Bootstrap 的“拆分按钮下拉菜单”下拉菜单?