[英]How to fix No Reverse Match error in Django
每当我想查看属于某个组的帖子时,我都会遇到此错误
NoReverseMatch at /groups/posts/in/python/
Reverse for 'user-posts' with keyword arguments '{'username': ''}' not found. 1 pattern(s) tried: ['user/(?P<username>[^/]+)$']
以下是我的urls.py
代码
from django.urls import path
from .views import (PostListView, PostCreateView,
PostUpdateView, PostDetailView, PostDeleteView, UserPostListView)
from . import views
app_name = 'posts'
urlpatterns = [
path('', PostListView.as_view(), name="blog-home"),
path('user/<str:username>', UserPostListView.as_view(), name="user-posts"),
path('post/<int:pk>/', PostDetailView.as_view(), name="post-detail"),
path("post/<int:pk>/", views.post_comment, name="comments"),
path('post/new/', PostCreateView.as_view(), name="post-create"),
path('post/<int:pk>/update', PostUpdateView.as_view(), name="post-update"),
path('post/<int:pk>/delete', PostDeleteView.as_view(), name="post-delete"),
path('about/', views.about, name="blog-about"),
]
问题似乎来自我的_posts.html
文件,但我无法解决。
这是_posts.html
代码
<div class="media">
<h3 class="mr-5"><a href="{% url 'posts:user-posts' username=post.user.username %}">@{{ post.user.username }}</a></h3>
<div class="media-body">
<strong>{{ post.user.username }}</strong>
<h5>{{ post.message_html|safe }}</h5>
<time class="time"><a href="{% url 'posts:post-detail' username=post.user.username pk=post.pk %}">{{ post.created_at }}</a></time>
{% if post.group %}
<span class="group-name">in <a href="#">{{ post.group.name }}</a></span>
{% endif %}
</h5>
<div class="media-footer">
{% if user.is_authenticated and post.user == user and not hide_delete %}
<a href="{% url 'posts:post-delete' pk=post.pk %}" title="delete" class="btn btn-simple">
<span class="fa fa-remove text-danger" aria-hidden="true"></span>
<span class="text-danger icon-label">Delete</span>
</a>
{% endif %}
</div>
</div>
</div>
一旦我单击组的列表页面将我带到与该组关联的帖子,我就会收到上述错误。
有人说我传递给 urls.py 的用户名是空的,我无法从我的 views.py 中找出代码有什么问题
from django.contrib import messages
from django.contrib.auth.mixins import (LoginRequiredMixin, UserPassesTestMixin)
from django.contrib.auth.models import User
from django.urls import reverse_lazy, reverse
from django.http import HttpResponseRedirect, Http404
from django.shortcuts import render, get_object_or_404, redirect
from django.views.generic import (ListView, DetailView, CreateView,
UpdateView, DeleteView)
from .models import Post, Comment
from django.core.files.storage import FileSystemStorage
from .forms import CommentForm, PostForm
from . import models
def home(request):
context = {
'posts': Post.objects.all()
}
return render(request, 'blog/home.html', context)
def upload(request):
if request.method == 'POST':
form = PostForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('post_detail')
else:
form = PostForm()
return render(request, 'blog/post_detail.html',
{'form': form })
class PostListView(ListView):
model = Post
template_name = 'blog/home.html'
context_object_name = 'posts'
ordering = ['-date_posted']
paginate_by = 6
class UserPostListView(ListView):
model = Post
template_name = 'blog/user_posts.html'
context_object_name = 'posts'
paginate_by = 6
def get_queryset(self):
user = get_object_or_404(User, username=self.kwargs.get('username'))
return Post.objects.filter(author=user).order_by('-date_posted')
class PostDetailView(DetailView):
model = Post
def post_comments(request, slug):
template_name = 'blog/post_detail.html'
post = get_object_or_404(Post, slug=slug)
comments = post.comments.filter(active=True)
new_comment = None
# Comment posted
if request.method == 'POST':
comment_form = CommentForm(data=request.POST)
if comment_form.is_valid():
# Create Comment object but don't save to database yet
new_comment = comment_form.save(commit=False)
# Assign the current post to the comment
new_comment.post = post
# Save the comment to the database
new_comment.save()
else:
comment_form = CommentForm()
return render(request, template_name, {'post': post,
'comments': comments,
'new_comment': new_comment,
'comment_form': comment_form})
class PostCreateView(LoginRequiredMixin, CreateView):
model = models.Post
fields = ['title', 'content', 'group', 'image']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Post
fields = ['title', 'content']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
return False
class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
model = Post
success_url = reverse_lazy('blog-home')
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
return False
def about(request):
return render(request, 'blog/about.html', {'title': 'About'})
您的 urls.py 和模板没有任何问题。 问题在于'{'username': ''}'
因为用户名是空的,它不会与正则表达式匹配( user/(?P<username>[^/]+)$
)。 尝试找出用户名为空的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.