0

i want to concatenate string and variable in views.py file.

i tried {{}} but this is not working on views.py file.

    try:
         filename= file.cv #file name comes from db
         path = "media/" filename
         return FileResponse(open("path, 'rb'), content_type='application/pdf')
    except FileNotFoundError:
         raise Http404()

i need to save a string in path variable like "media/cv.pdf" or "media/mycv.pdf" but can't do that.

2
  • path = "media/" + filename or path = os.path.join("media", filename) Commented Jul 9, 2019 at 22:50
  • 2nd one is working fine. Commented Jul 9, 2019 at 23:03

1 Answer 1

1

Because filename is a string so you could conacatenate like two strings

path = "media/" + filename 

but preferred is

path = os.path.join("media", filename)

which will use / on Linux/MacOS and \ on Windows

Sign up to request clarification or add additional context in comments.

2 Comments

1st one is not working fine and the second one is working fine.
all may depends what text you have in variable filename - if there is only filename without folders then they should generate the same string. you can always check it with print( path ) and see if both gives the same. But as I wrote in answer second is preferred because it may do something more then only concatenate strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.