0

I am little bit confused about creating full url. I have such code :

    def flats(self):
        return [JsonFlatPage(property_data = flat, url = flat['propertyUrl']) 
                for flat in self.data['properties']]

in flat['propertyUrl'] I have '/properties/75599853', but I need to get like this one:

'https://www.rightmove.co.uk/properties/75599853#/' 

with full path and # at the end. I know that I should make constant URI in settings file, but then how I can convert it? Should I use f-strings?

3
  • Does flat['url'] contain domain name ? Commented Feb 3, 2021 at 7:36
  • No, only /properties/75599853 Commented Feb 3, 2021 at 7:37
  • If domain is static, then u can put it in a variable like domain_name = 'https://...' then url=domain_name+flat['propertyUrl'] Commented Feb 3, 2021 at 7:40

1 Answer 1

1

I think since the base url https://www.rightmove.co.uk/ is fixed, you can do something like below to get what you need:

def flats(self):
        baseUrl = 'https://www.rightmove.co.uk/'
        return [JsonFlatPage(property_data = flat, url = baseUrl + flat['propertyUrl'] + "#/") 
                for flat in self.data['properties']]

You can also use f-strings as you mentioned as:

def flats(self):
        baseUrl = 'https://www.rightmove.co.uk/'
        return [JsonFlatPage(property_data = flat, url = f"{baseUrl}{flat['propertyUrl']}#/") 
                for flat in self.data['properties']]
Sign up to request clarification or add additional context in comments.

2 Comments

But if I want to make domain as attribute class and put as well in settings.py?
you can use that to update the baseUrl at runtime if that is present in some other dictionary.

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.