Django:NoReverseMatch 在

Django: NoReverseMatch at

所以当我去url时:http://127.0.0.1:8000/post/2/. instead of showing me the post detail view, it gives me an error. I am so confused about it. However I can see it in list view and in profile page. 编辑:添加错误图像和 detail.html

posts views.py

class PostDetailView(LoginRequiredMixin, DetailView):
   model = Posts
   template_name = 'posts/detail.html'

models.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) 

the main 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'))

]

posts 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)

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

]

detail.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 %}

改变这个

  <a href="{% url 'profile' object.user.profile %}">{{object.user.username}}</a>

  <a href="{% url 'profile' object.user.profile.pk %}">{{object.user.username}}</a>

应该可以。