I have not seen this question asked before and I want to start out by saying that I accept this might be an impossible task.
I’m rendering a pdf, and the name of the pdf in the URL should be the name from a database field called “ncr number” which is actually a string. I’m using a primary key to get the current context of everything. For example if the ncr number was “A109”, then when I open the pdf, the url should be http://localhost:8000/A109.pdf
I should mention that this RecordDetailPage is a html view revealing the information of the database record. There is a button on this page which opens the PDF. The fact that this page opens prior to the pdf led me to believe that this should be possible.
In my views.py file, I am trying to pull this NCR string out of the generic view function “get()”
Code:
class RecordDetailPage(LoginRequiredMixin, DetailView):
model = DashboardModel
template_name = “dashboard/record_detail.html”
context_object_name = ‘record’
def get(self, record, *args, **kwargs):
pk = self.kwargs.get('pk')
record = DashboardModel.objects.get(pk=pk)
self.object = self.get_object()
print("test....", self.object) # "self.object" is the string of text I need
print("test....", record.ncr_number) # I can also get the string I need this way
global var
var = record.ncr_number #I am trying to assign a global variable to extract the string from here
return super().get(request, *args, **kwargs)
print(var) # This doesn’t work outside the function
Thank you for your reply. I too felt that I was approaching this the wrong way.
I’ve promply taken your advice just now and I can see the url working - although what I’m doing on the html button/link is guess work. I may have my jinja2 syntax wrong.
I also need the primary key on the button as I’m retrieving some database record data on the pdf. I will try and look into this further, thank you Ken.
Side note - when you’re posting single lines of code here, use the backtick - ` at the beginning and end of each line to keep the forum software from removing the < and > symbols (among others) from the post.
I made a reply saying that I got things working, but an automated spam filter removed my post. An image I posted was showing the “NCR number”, but it was a mock number I made up myself. The spam filter must have assumed I was infringing on a companies rights.
I will post again exactly what I’ve done to make this work. Thanks Ken for guiding me in the right direction, I was digging myself into a hole with how I was initially trying to do this.
Now, on my function which returns the HttpResponse, I needed to pass the arguments for the primary key and ncr_number database field by using the names I defined in the url path:
Final notes on this - Everything is now working perfectly.
Django is particular about needing the function argument names exactly as what I wrote in the url path. They can be named anything.
This http response header [‘url-parameter’] is something I defined myself, I could remove this and it would still work.