【问题标题】:NoReverseMatch at /projects/ Reverse for 'user-profile' with arguments '('',)' not foundNoReverseMatch at /projects/ Reverse for 'user-profile' with arguments '('',)' not found
【发布时间】:2022-06-14 08:33:17
【问题描述】:

我刚开始使用 Django,我不知道这个错误的确切来源。它可能与所有者属性有关。到目前为止,这是我的代码。

项目/modely.py

class Project(models.Model):
    owner = models.ForeignKey(Profile, null=True, blank=True, on_delete=models.SET_NULL)
    title = models.CharField(max_length=200)
    

users/models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
    name = models.CharField(max_length=200, blank=True, null=True)
    

projects/views.py

def projects(request):
    projects = Project.objects.all()
    context = {'projects':projects}
    return render(request, 'projects/projects.html', context)

projects.html

{% for project in projects %}
<p><a class="project__author" href="{% url 'user-profile' project.owner.name %}">{{project.owner.name}}</a></p>
{% endfor %}

users/views.py

def userProfile(request, pk):
    profile = Profile.objects.get(id=pk)
    context = {'profile':profile}
    return render(request, 'users/user-profile.html', context)

【问题讨论】:

  • 请添加您的根目录和应用级别的 urls.py 文件。

标签: django django-models django-templates


【解决方案1】:

主要问题是您允许名称字段为空白和空值,因此当您在 &lt;a href&gt; 的 url 中请求它时,它会发送一个空字符串,但您的 userProfile 视图正在请求pk

首先,从你的类中删除 null 和空白:

class Project(models.Model):
    owner = models.ForeignKey(Profile, on_delete=models.CASCADE)
    title = models.CharField(max_length=200)

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=200, blank=True, null=True)

其次,要求用户在进入之前先登录,否则project.owner仍然会有空白字符串,这将导致相同的结果。您可以使用 @login_required 装饰器来做到这一点:

from django.contrib.auth.decorators import login_required

@login_required
def projects(request):
    projects = Project.objects.all()
    context = {'projects':projects}
    return render(request, 'projects/projects.html', context)

然后在你的html中:

<a class="project__author" href="{% url 'user-profile' project.owner.user.pk %}">

但最后,@Snow 的答案是正确的。 User 模型已经有一个 username、first_name、last_name 字段,所以你可以去掉 Profile 模型中的 name 字段,然后这样做:

<a class="project__author" href="{% url 'user-profile' request.user.pk %}">

【讨论】:

  • 这里是 urls.py 的一部分:path('profile/&lt;str:pk&gt;/', views.userProfile, name="user-profile"), 如果我按照您所说的那样做,点击链接时会收到以下错误消息:DoesNotExist at /profile/230d5590-d90a- 4697-beb7-b1f5415a690f/ 配置文件匹配查询不存在。
  • 查看我编辑的答案@PhilGrütter。希望这会有所帮助。
【解决方案2】:

User object 已经包含基本信息字段,例如 usernamepasswordemailfirstlast 名称。

您正在创建一个名为 name 的新字段来进行 URL 查找,但已将您的字段名称设置为 blank=True。如果没有slug(在您的情况下是个人资料名称),您将无法查找个人资料。因此,您应该通过用户名查找用户。始终尝试坚持 DRY 方法(不要重新发明轮子)。

尝试这样做:

<a class="project__author" href="{% url 'user-profile' project.owner.user.username %}">

如果您的网址中有app_name,那么您需要使用应用名称。

<a class="project__author" href="{% url '<app_name>:user-profile' project.owner.user.username %}">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 2022-07-04
    • 1970-01-01
    相关资源
    最近更新 更多