【问题标题】:Error: Reverse for 'coach_profile' with arguments '('',)' not found. 1 pattern(s) tried: - Unable to resolve错误:未找到带有参数“(”,)”的“coach_profile”的反向。已尝试 1 种模式:- 无法解决
【发布时间】:2021-02-03 05:24:54
【问题描述】:

我正在尝试创建一个基于目录的网站。我创建了一个列表视图,并创建了一个详细视图,当您单击列表视图中的某个项目时会填充该视图。但是,我还想使用类似的详细信息视图在个人资料页面上为用户提供自己的数据。我通过在单独的视图中创建查询集来做到这一点。

问题是,我似乎无法正确配置 url 和模板标签以使其显示在页面上,我只是收到以下错误:

Reverse for 'coach_profile' with arguments '('',)' not found. 1 pattern(s) tried: ['coach/(?P<pk>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/profile$']

我正在尝试通过导航栏上名为您的个人资料的链接访问此页面。我正在尝试的模板标签如下

href="{% 'coach_profile' %}">Your Coach Profile</a>
href="{% 'coach_profile' coach.id %}">Your Coach Profile</a>
href="{% 'coach_profile' coach.pk %}">Your Coach Profile</a>

urls.py

from django.urls import path
from .views import CoachListView, CoachDetailView, CoachProfileView

urlpatterns = [
    path('', CoachListView.as_view(), name='coach_list'),
    path('<uuid:pk>', CoachDetailView.as_view(), name='coach_detail'),
    path('<uuid:pk>/profile/', CoachProfileView.as_view(), name='coach_profile'),
    ]

views.py

from django.views.generic import ListView, DetailView
from django.contrib.auth.mixins import LoginRequiredMixin
from .models import Profile


class CoachListView(LoginRequiredMixin, ListView):
    model = Profile
    context_object_name = 'coach_list'
    template_name = 'coach/coach_list.html'
    login_url = 'account_login'


class CoachDetailView(LoginRequiredMixin, DetailView):
    model = Profile
    context_object_name = 'coach_detail'
    template_name = 'coach/coach_detail.html'
    login_url = 'account_login'


class CoachProfileView(LoginRequiredMixin, DetailView):
    model = Profile
    context_object_name = 'coach_profile'
    template_name = 'coach/coach_profile.html'
    login_url = 'account_login'

    def get_queryset(self):
        if self.request.user.is_superuser:
            return Profile.objects.all()
        else:
            return Profile.objects.filter(user=self.request.user)

models.py

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from django.urls import reverse
from django.contrib.auth import get_user_model

import uuid


class Profile(models.Model):
    id = models.UUIDField(
        primary_key=True,
        default=uuid.uuid4,
        editable=False)
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    first_name = models.CharField(max_length=30, null=True)
    last_name = models.CharField(max_length=30, null=True)
    bio = models.TextField(max_length=500)
    profile_image = models.ImageField(upload_to='profile_images/', blank=True)

    def __str__(self):
        return '{} {}'.format(self.first_name, self.last_name)

    def get_absolute_url(self):
        return reverse('coach_detail', args=[str(self.id)])


@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)


@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

Templates (_base.html)

{% if user.is_authenticated %}
<a class="p-2 text-dark" href="{% url 'coach_profile' coach.id %}">Your Coach Profile</a>
<a class="p-2 text-dark" href="{% url 'account_logout' %}">Log Out</a>
{% else %}
<a class="p-2 text-dark" href="{% url 'account_login' %}">Log In</a>
<a class="btn btn-outline-primary" href="{% url 'account_signup' %}">Sign Up</a>
{% endif %}

这个之前提出的问题解决了我的疑问。

Django variable in base.html

【问题讨论】:

  • 你包含的模板代码在哪里?
  • 它位于 _base.html
  • 在对此进行了更多研究之后,它与我在 _base.html 中访问变量的位置有关。我在上面编辑了我的答案,以显示回答我的问题。
  • 您可能需要考虑将您的解决方案放在这里作为答案。

标签: django django-views django-templates django-urls


【解决方案1】:

我不知道此模板与哪个视图相关,但如果此模板与您的 CoachListView 相关,您应该执行以下操作:

{% for obj in coach_list %}
<a class="p-2 text-dark" href="{% url 'coach_profile' obj.id %}">Your Coach Profile</a>
{% endfor %}

因为coach_list 是一个查询集,并且要获取对象的 id,您应该遍历它。

【讨论】:

  • 感谢您的评论。这极大地解决了我的问题,以至于该链接现在可以正常工作。但由于我有多个用户,这会在导航栏上创建多个“您的教练资料”链接,登录用户只能访问一个。我希望只有一个与登录用户相关的链接版本,因此我不确定 for 循环是正确的方法。仅供参考,coach_list 是一个错字,我已经编辑了这个问题来解决这个问题。模板标签与在视图的上下文对象名称中称为“coach_profile”的 CoachProfileView 相关。
猜你喜欢
  • 2019-08-13
  • 1970-01-01
  • 2021-11-16
  • 1970-01-01
  • 2021-11-26
  • 1970-01-01
  • 2018-11-18
  • 2021-01-17
  • 2020-12-28
相关资源
最近更新 更多