错误:找不到带有参数“'',”的“ coach_profile”尝试了1种模式:-无法解决

如何解决错误:找不到带有参数“'',”的“ coach_profile”尝试了1种模式:-无法解决

我正在尝试创建一个基于目录的网站。我创建了一个列表视图,并且创建了一个细节视图,当您单击列表视图中的项目时,该视图就会填充。但是,我也想使用类似的详细信息视图在配置文件页面上向他们提供用户自己的数据。我是通过在单独的视图中创建查询集来完成此操作的。

问题是,我似乎无法正确配置url和template标签以使其显示页面上,而我却遇到了以下错误

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,**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

解决方法

我不知道此模板与哪个视图相关,但是如果该模板与您的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,您应该对其进行迭代。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?