当我添加帖子然后添加但不显示在仪表板中时,它只显示主页。未为当前用户添加帖子我该怎么办
Posted
技术标签:
【中文标题】当我添加帖子然后添加但不显示在仪表板中时,它只显示主页。未为当前用户添加帖子我该怎么办【英文标题】:when i am add post then add but not show in dashboard it is show only home page . Post is not add for current user what can i do 【发布时间】:2021-08-24 05:22:38 【问题描述】:当我添加帖子然后添加但不显示在仪表板中时,它只显示主页。不为当前用户添加帖子。如何为当前用户添加帖子
Models.py 中的帖子类
类帖子(models.Model):
title = models.CharField(max_length=100)
decs = models.TextField()
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
def get_absolute_url(self):
return reverse('author-detail', kwargs='pk': self.pk)
def __str__(self):
return self.title + '|' + str(self.user)
viwes 中的 DASHBOARD 代码
def 仪表板(请求):
if request.user.is_authenticated:
current_user = request.user
posts = Post.objects.filter(user=current_user)
else:
redirect('/login/')
return render(
request,
"dashboard.html",
'posts':posts
)
仪表板 HTML 代码
<h3 class="my-5">Dashboard </h3>
<a href="% url 'addpost' %" class="btn btn-success">Add Post</a>
<h4 class="text-center alert alert-info mt-3">Show Post Information</h4>
% if posts %
<table class="table table-hover bg white">
<thead>
<tr class="text-center">
<th scope="col" style="width: 2%;">ID</th>
<th scope="col" style="width: 25%;">Title</th>
<th scope="col" style="width: 55%;">Description</th>
<th scope="col" style="width: 25%;">Action</th>
</tr>
</thead>
<tbody>
% for post in posts %
% if post.user == request.user%
<tr>
<td scope="row">post.id</td>
<td>post.title</td>
<td>post.decs</td>
<td class="text-center">
<a href="% url 'updatepost' post.id %" class="btn btn-warning btn-sm">Edit</a>
<form action="% url 'deletepost' post.id %" method="post" class="d-inline">
% csrf_token %
<input type="submit" class = "btn btn-danger btn-sm" value="Delete">
</form>
</td>
</tr>
% endif %
% endfor %
</tbody>
</table>
% else %
<h4 class="text-center alert alert-warning">No Record</h4>
% endif %
在 viwes.py 中注册代码
定义用户注册(请求):
if request.method =="POST":
form = SignUpForm(request.POST)
if form.is_valid():
messages.success(request, 'Successfully SignUp')
form.save()
form = SignUpForm(request.POST)
else:
form = SignUpForm()
return render(
request,
"signup.html",
'form' : form
)
在views.py中添加邮政编码
def addpost(请求):
if request.user.is_authenticated:
if request.method =="POST":
form = PostForm(request.POST)
if form.is_valid():
title = form.cleaned_data['title']
decs = form.cleaned_data['decs']
pst = Post(title=title, decs=decs)
pst.save()
messages.success(request, "Blog Added successfully !!")
form = PostForm()
return render(
request,
"addpost.html",
'posts':pst
)
else:
form = PostForm()
return render(
request,
"addpost.html",
'form' : form
)
else:
return HttpResponseRedirect('/login/')
在 HTML 中添加邮政编码
<h3 class = " text-white my-5">Dashboard/ Add Post</h3>
% if messages %
% for message in messages %
<p % if message.tags % class="alert alert-message.tags my-5"% endif %>message</p>
% endfor %
% endif %
<form action="" method="post">
% csrf_token %
form.as_p
<input type="submit" value="Add" class = "btn btn-success">
<a href="% url 'dashboard' %" class="btn btn-danger">Cancel</a>
</form>
【问题讨论】:
【参考方案1】:#1 你必须在保存后添加一个用户。
if request.user.is_authenticated:
if request.method =="POST":
form = PostForm(request.POST)
if form.is_valid():
title = form.cleaned_data['title']
decs = form.cleaned_data['decs']
#here in last parameter I have set user
pst = Post(title=title, decs=decs, user=request.user)
pst.save()
#please add here redirect otherwise throw error after post save
messages.success(request, "Blog Added successfully !!")
form = PostForm()
return render(
request,
"addpost.html",
'posts':pst
)
else:
form = PostForm()
return render(
request,
"addpost.html",
'form' : form
)
else:
return HttpResponseRedirect('/login/')
【讨论】:
以上是关于当我添加帖子然后添加但不显示在仪表板中时,它只显示主页。未为当前用户添加帖子我该怎么办的主要内容,如果未能解决你的问题,请参考以下文章