Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.3k views
in Technique[技术] by (71.8m points)

Can I get the property of a specific object in a Django template?

I am using django-filters in my project. Let's say I get this address after a search:

?title=my title&ms=8

As you can see, I've searched for 'my title' and for a manuscript which the user sees as 'my manuscript', but whose get parameter (and ID) is 8.

Now, I'd like to add to my template a courtesy 'you've searched for' field. How can I show the title of that manuscript, rather than its ID?

I already have the ms objects in my view:

mstunes = MsTune.objects.all()
ms = Manuscript.objects.all() <- manuscripts
f = MsTuneFilter(request.GET, queryset=MsTune.objects.all())
return render(request, 'manuscript/mstune_list.html', {'mstunes': mstunes, 'f': f, 'request':request, 'ms': ms})

and I can indeed access them with {{ms.all}}. Is it possible to do something, in pseudo code, like:

show the ms.title of the ms object with id = request.GET.ms

?

My view:

def MsTuneList(request):

mstunes = MsTune.objects.all()
ms = Manuscript.objects.all() # can I get the ms.id from the request, with, say, request.ms, and then use it with get(id=ms)?
f = MsTuneFilter(request.GET, queryset=MsTune.objects.all())
return render(request, 'manuscript/mstune_list.html', {'mstunes': mstunes, 'f': f, 'request':request, 'ms': ms})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This should get the manuscript object which should have the title:-

Manuscript.objects.get(id=ms)

Note ms is the id (8 in your example) from the get parameter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...