gpt4 book ai didi

django - 找不到页面 (404) Django 2

转载 作者:行者123 更新时间:2023-12-02 02:48:11 25 4
gpt4 key购买 nike

我很困惑,请帮忙。每当我单击应该触发 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 的用户名参数的有效值,因此首先匹配 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com