当我添加 post 然后添加但不显示在仪表板中时,它只显示主页。 Post 没有为当前用户添加我该怎么办

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

当我添加 post 然后添加但不显示在仪表板中它只显示主页。 Post 没有为当前用户添加。如何为当前用户

添加post

Post class 在 Models.py

class Post(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)

VIWE 中的 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

def user_signup(请求):

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

中添加post代码

def 添加post(请求):

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

中添加post代码
<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 您必须在 post 保存时添加用户。

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/')