删除个人资料 django 上的帖子
Delete posts on profile django
我一直在做一个项目,它具有删除帖子的功能之一,当在图像列表视图中时,我能够删除这些帖子,但是当我在显示用户的配置文件视图中时当我尝试删除它时,出现 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'),
]
我为解决这个问题所做的一切就是创建添加一个 if 语句到我的 imagelist.html
{% if image.user == user %}
<a href="/profile">{{ image.user }}</a>
{% else %}
<a href="{% url 'profile_pk' pk=image.user.pk %}">{{ image.user }}</a>
{% endif %}
我一直在做一个项目,它具有删除帖子的功能之一,当在图像列表视图中时,我能够删除这些帖子,但是当我在显示用户的配置文件视图中时当我尝试删除它时,出现 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'),
]
我为解决这个问题所做的一切就是创建添加一个 if 语句到我的 imagelist.html
{% if image.user == user %}
<a href="/profile">{{ image.user }}</a>
{% else %}
<a href="{% url 'profile_pk' pk=image.user.pk %}">{{ image.user }}</a>
{% endif %}