NoReverseMatch at /projects/ Reverse for 'user-profile' with arguments '('',)' not found
NoReverseMatch at /projects/ Reverse for 'user-profile' with arguments '('',)' not found
我刚刚开始使用 Django,我不知道这个错误是从哪里来的。这可能与所有者属性有关。到目前为止,这是我的代码。
projects/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)
主要问题是您允许名称字段为空且为空,因此当您在 <a href>
的 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,模型已经有一个用户名,first_name,last_name 字段,所以你可以去掉 Profile 模型中的 name 字段,然后只做:
<a class="project__author" href="{% url 'user-profile' request.user.pk %}">
User object,已经有基本信息的字段,如 username
、password
、email
和 first
和 last
名称.
您正在创建一个名为 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 %}">
我刚刚开始使用 Django,我不知道这个错误是从哪里来的。这可能与所有者属性有关。到目前为止,这是我的代码。
projects/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)
主要问题是您允许名称字段为空且为空,因此当您在 <a href>
的 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,模型已经有一个用户名,first_name,last_name 字段,所以你可以去掉 Profile 模型中的 name 字段,然后只做:
<a class="project__author" href="{% url 'user-profile' request.user.pk %}">
User object,已经有基本信息的字段,如 username
、password
、email
和 first
和 last
名称.
您正在创建一个名为 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 %}">