因此,当我转到网址时:http://127.0.0.1:8000/post/2/。它没有向我显示帖子详细信息视图,而是给了我一个错误。我对此很困惑。但是我可以在列表视图和个人资料页面中看到它。 编辑:添加错误图像和detail.html
发表意见.py
class PostDetailView(LoginRequiredMixin, DetailView):
model = Posts
template_name = 'posts/detail.html'
模型.py
class Posts(models.Model):
caption = models.CharField(max_length=2200)
date_posted = models.DateTimeField(default=timezone.now())
image = models.ImageField( upload_to='PostsImages')
user = ForeignKey(User, on_delete=models.CASCADE ,related_name='userposts')
def __str__(self):
return f"Post {self.id} ({self.user.username})'s"
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
img = Image.open(self.image.path)
img.save(self.image.path)
主要的 urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('posts.urls')),
path('user/', include('users.urls')),
path('comments/', include('comments.urls'))
]
帖子 urls.py
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from .views import PostsListView, PostCreatView, PostDeleteView, PostDetailView
urlpatterns = [
path('', PostsListView.as_view(), name='homepage'),
path('delete/<int:pk>/', PostDeleteView.as_view(), name='delete-post'),
path('creat-post', PostCreatView.as_view(), name='create-post'),
path('post/<int:pk>/', PostDetailView.as_view(), name='detail-post')
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
用户 urls.py
from django.urls import path
from django.contrib.auth import views as auth_views
from .views import ProfileDetailView
from .views import SignUp, LogOut
urlpatterns = [
path('signup/', SignUp, name='signup'),
path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'), #template_name'xx' tells it where to find the log in url
path('logout/', LogOut, name='logout'),
path('<int:pk>/profile', ProfileDetailView.as_view(), name='profile')
]
详细信息.html
{% extends "posts/base.html" %}
{% block content %}
<a href="{% url 'profile' object.user.profile %}">{{object.user.username}}</a>
<div>
<img class="rounded-circle article-img" src="{{ objetc.user.profile.picture.url }}" />
</div>
{% if user == object.user %}
<div>
<a href="{% url 'delete-post' object.pk %}" class="btn btn-outline-danger">Delete Post</a>
</div>
{% endif %}
<img src="{{ object.image.url }}" />
<div>
<a href="{% url 'profile' object.user.profile %}">{{object.user.username}}</a>
</div>
<p>{{object.caption}}</p>
<h6>Comments</h6>
{% for comment in object.comments.all %}
<hr class="bg-danger border-2 border-top border-primary">
<a href="{% url 'profile' comment.user.profile.pk %}">{{comment.user.username}}</a>
<p>{{comment.text}}</p>
{% if user == comment.user %}
<a href="{% url 'delete-comment' comment.pk %}" class="btn btn-outline-primary btn-sm mb-2 mr-4">Delete</a>
<a href="{% url 'update-comment' comment.pk %}" class="btn btn-outline-primary btn-sm mb-2 mr-4">Edit</a>
{% endif %}
{% endfor %}
<div>
<a href="{% url 'add-comment' object.pk %}" class="btn btn-outline-primary mt-2">Add Comment</a>
</div>
{% endblock %}