1
print full_url = request.get_host()+request.get_full_path()

Result:

127.0.0.1:8000/test/en/

or

127.0.0.1:8000/test/ru/

How to check if the end of the string is /en/ or /ru/

1
  • You can use find function of string to verify the string ends with '/en/' or '/ru/' Such as url = '127.0.0.1:8000/test/ru/' url.find('/ru/') Commented Jul 19, 2013 at 10:47

2 Answers 2

6

Use endswith:

full_url.endswith('/en/') or full_url.endswith('/ru/')

If you find that there are more extensions you have to cover, you may consider using any:

any(s.endswith(ext) for ext in ['/ru/', '/en/'])
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, what if ru is not on the end of the string 127.0.0.1:8000/ru/test/?
Then u can use find function full_url.find('ru')
@Raman: In that case you would use in, e.g. '/en/' in full_url or '/ru/' in full_url.
You don't need to use any or or here: .endswith accepts a tuple. For example: full_url.endswith(('/en/','/ru/')).
@DSM: Ah, that's pretty cool, I didn't know that! You should make it an answer so that I could upvote it, I think that's a much nicer solution than what I suggested.
0

You can test the four final characters:

full_url[-4:]

or use the ends_with string method:

if full_url.ends_with("/en/"):
  print 'en'
elif full_url.ends_with("/ru/"):
  print 'ru'

Comments

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.