如何解决错误:找不到带有参数“'',”的“ 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 %}
解决方法
我不知道此模板与哪个视图相关,但是如果该模板与您的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 举报,一经查实,本站将立刻删除。