[英]Django: How Can I get 'pk' in views.py when I use class based views?
I'm writhing an app with class based views.我正在使用基于类的视图扭曲应用程序。 I need to receive 'pk' in my views.py file when I load my page using generic DetailView.当我使用通用 DetailView 加载页面时,我需要在 views.py 文件中接收“pk”。
My urls.py file:我的 urls.py 文件:
from django.urls import path
from . views import HomeView, ProfileView, AddPetView, EditPetView, DeletePetView,
PetProfileView
urlpatterns = [
...
path('profile/pet/<int:pk>', PetProfileView.as_view(), name='pet_profile'),
path('', HomeView.as_view(), name='home'),
]
My views.py file:我的views.py 文件:
from django.shortcuts import render
from django.views.generic import TemplateView, ListView, UpdateView, CreateView, DeleteView,
DetailView
from django.urls import reverse_lazy
from . models import Pet
class PetProfileView(DetailView):
model = Pet
template_name = 'pet_profile.html'
#key = Pet.objects.values_list('birth_date').get(pk=1)
My Pet Model is:我的宠物模型是:
class Pet(models.Model):
pet_name = models.CharField(max_length=255)
pet_breed = models.CharField(max_length=255)
pet_type = models.CharField(max_length=255)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
pet_reg_date = models.DateField(auto_now_add=True) # when the pet is registered in the site
birth_date = models.DateField()
def __str__(self):
return self.pet_name + ' | ' + str(self.owner)
# def get_absolute_url(self):
# return reverse('article-detail', args=str(self.id))
def get_absolute_url(self):
return reverse('profile')
When I load profile for some pet, I need to get in my view class PetProfileView(DetailView): like a variable the birth date for this specific pet.当我为某些宠物加载配置文件时,我需要进入我的视图类 PetProfileView(DetailView): 就像这个特定宠物的出生日期的变量一样。 I will make some calculations with this date我会用这个日期做一些计算
I need to extract from database birh_date column for this specific pet.我需要从这个特定宠物的数据库 birh_date 列中提取。 How to get this pk=?如何得到这个 pk=? when I load pet_profile page?当我加载 pet_profile 页面时?
I'm not sure if I can describe the case well.我不确定我是否能很好地描述这个案例。 If something is not clear, ask me.如果有什么不清楚的,可以问我。 I will try to explain again.我将再次尝试解释。
On class based views:基于类的视图:
self.kwargs['pk']
So, in your case所以,在你的情况下
class PetProfileView(DetailView):
model = Pet
template_name = 'pet_profile.html'
def get_context_data(self, **kwargs):
context['key'] = self.get_object().birth_date
return context
I don't know what you want to do with the "key" variable.我不知道你想用“key”变量做什么。 This example is to use on your template.此示例用于您的模板。 You can access to "birth_date" writting {{key}}
or {{object.birth_date}}
您可以使用{{key}}
或{{object.birth_date}}
访问“birth_date”
As already mentioned you can access the URL parameters like self.kwargs["pk"]
.如前所述,您可以访问 URL 参数,例如self.kwargs["pk"]
。 But as you are already using a DetailView
which already provides the functionality to retrieve an object via its id you can customize the queryset it uses:但是由于您已经在使用DetailView
,它已经提供了通过其 id 检索对象的功能,您可以自定义它使用的查询集:
class PetProfileView(DetailView):
model = Pet
template_name = 'pet_profile.html'
queryset = Pet.objects.values_list('birth_date')
(Though without doing this you should also be able to access pet.birth_date
in your template) (虽然不这样做你也应该能够在你的模板中访问pet.birth_date
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.