Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...