我很困惑,请帮忙。
每当我单击应该触发ProjectDetailView的项目标题时,都会出现此错误:

找不到页面(404)
请求方法:GET
请求网址:http://127.0.0.1:8000/projects/7/
提出者:projects.views.UserProjectListView
没有用户匹配给定的查询。

为什么在UserProjectListView中引发错误?

这是我的项目模型:

class Project(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()
    date_created = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

def __str__(self):
    return self.name

def get_absolute_url(self):
    return reverse('project-detail', kwargs={'pk': self.pk})


我的看法:

class ProjectDetailView(DetailView):
model = Project
context_object_name = 'projects'

class ProjectListView(ListView):
model = Project
context_object_name = 'projects'
template_name = 'projects/project_list.html'
ordering = ['-date_created']
paginate_by = 2

class UserProjectListView(ListView):
model = Project
template_name = 'projects/user_project_list.html'
context_object_name = 'projects'
paginate_by = 2

def get_queryset(self):
    user = get_object_or_404(User, username=self.kwargs.get('username'))
    return Project.objects.filter(author=user).order_by('-date_created')


我的网址:

urlpatterns = [
path('', ProjectListView.as_view(), name='project-home'),
path('<str:username>/', UserProjectListView.as_view(), name='user-projects'),
path('<int:pk>/', ProjectDetailView.as_view(), name='project-detail'),
path('new/', ProjectCreateView.as_view(), name='project-create'),


**模板**

<div class="card shadow border-left-csu border-bottom-csu mb-4 mx-3">
            <!-- Card Header - Dropdown -->
            <div class="card-header py-3 d-flex flex-row  align-items-center justify-content-between">
                <div class="col-xs-2">
                    <img class="rounded-circle mr-1" src="{{ project.author.profile.image.url }}" alt="{{ user.username }}">
                    <p><a href="{% url 'user-projects' project.author %}"> {{ project.author }} </a></p>
                </div>

                <h6 class="m-0 font-weight-bold text-primary">
                    {% if user.is_authenticated %}
                        <a class="article-title" href="{% url 'project-detail' project.id %}">{{ project.name }}</a>
                    {% else %}
                        <p>{{ project.name }}</p>
                    {% endif %}
                </h6>

                <div class="dropdown no-arrow">
                    <a class="dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <i class="fas fa-ellipsis-v fa-sm fa-fw text-gray-400"></i>
                    </a>

                    <div class="dropdown-menu dropdown-menu-right shadow animated--fade-in" aria-labelledby="dropdownMenuLink">
                        <div class="dropdown-header">Options:</div>
                        <a class="dropdown-item" href="{% url 'project-update' project.id %}">Modify</a>
                        <a class="dropdown-item" href="{% url 'project-base-file' project.id %}">Set Base File</a>
                        <a class="dropdown-item" href="#">Change Base File</a>
                        <div class="dropdown-divider"></div>
                        <a class="dropdown-item" href="#">Delete Project</a>
                        </div>
                    </div>
            </div>

            <!-- Card Body -->
            <div class="card-body">
                <p> {{ project.date_created|date:"F d, Y" }}</p>
                <p> {{ project.description }}</p>
            </div>
        </div>

最佳答案

“ 7”是UserProjectListView的username参数的有效值,因此URL首先匹配。

您可能应该使这些URL更具唯一性。例如:

path('user_project/<str:username>/', UserProjectListView.as_view(), name='user-projects'),
path('project/<int:pk>/', ProjectDetailView.as_view(), name='project-detail'),

关于django - 找不到页面(404)Django 2,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55025024/

10-12 21:25