【问题标题】:Delete posts on profile django删除个人资料 django 上的帖子
【发布时间】:2020-08-22 02:54:48
【问题描述】:

我一直在处理一个项目,它具有删除帖子的功能之一,当在 imagelist 视图中时,我可以删除这些帖子,但是当我在显示用户帖子的配置文件视图中时我尝试删除它,出现一个 404 错误页面未找到。我不知道错误在哪里,但我认为它在配置文件视图中。

views.py

    def profile(request, pk=None):
        if pk:
            post_owner = get_object_or_404(User, pk=pk)
            user_posts=Post.objects.filter(user_id=pk)
        else:
            post_owner = request.user
            user_posts=Post.objects.filter(user_id=pk)
        return render(request, 'profile.html', {'post_owner': post_owner, 'user_posts': user_posts,})

profile.html

    <div class="content-section">
      <div class="media">
        <img class="rounded-circle account-img" src="{{ user.profile.image.url }}">
        <div class="media-body">
          <h2 class="account-heading">{{ post_owner.username }}</h2>
          <p class="text-secondary">{{ post_owner.email }}</p>
        </div>
      </div>
      <div>
        {% for Post in user_posts %}
        <li class="list-group-item">{{ Post.text }}
          <a href="{% url 'profile_pk' pk=Post.user.pk %}">{{ Post.user }}</a>
          {% if Post.posti %}
            <img src="{{ Post.posti.url }}" alt="image here" style="max-width: 300px; max-height: 300px">
          {% endif %}
          {% if Post.user == user %}
            <div class="float-right">
              <form action="delete_image/{{ Post.id }}/" action="post">
                <button type="submit" class="btn btn-outline-danger btn-sm">Delete</button>
              </form>
            </div>
          {% endif %}
        </li>     
      {% endfor %}
    </div>
  </div>

urls.py

    urlpatterns = [
        path('profile/<int:pk>/', views.profile, name='profile_pk'),
        path('imagepost', views.uimage, name='image'),
        path('imagelist', views.imagelist, name='imagelist'),
        path('delete_image/<int:image_id>/', views.delete_image, name='delete_image'),
    ]

【问题讨论】:

    标签: html django forms django-forms http-status-code-404


    【解决方案1】:

    我为解决这个问题所做的只是在我的 imagelist.html 中添加一个 if 语句

        {% if image.user == user %}
          <a href="/profile">{{ image.user }}</a>
        {% else %}
          <a href="{% url 'profile_pk' pk=image.user.pk %}">{{ image.user }}</a>
        {% endif %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-19
      • 1970-01-01
      • 1970-01-01
      • 2017-09-19
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多