【发布时间】: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 %}
这个之前提出的问题解决了我的疑问。
【问题讨论】:
-
你包含的模板代码在哪里?
-
它位于 _base.html
-
在对此进行了更多研究之后,它与我在 _base.html 中访问变量的位置有关。我在上面编辑了我的答案,以显示回答我的问题。
-
您可能需要考虑将您的解决方案放在这里作为答案。
标签: django django-views django-templates django-urls