LetsPlayYahtzee
LetsPlayYahtzee

Reputation: 7581

How can I send a request to a view from an admin command without hard coding the url

I am trying to create an admin command that will simulate some api calls associated with a view but I don't want to hard code the url, for example like that url='http://127.0.0.1:8000/api/viewname', in order to send the request.

If I use the reverse option I can obtain half the url /api/viewname. If I try to post the request that way

url = reverse('name-of-view')
requests.post(url, data=some_data)

I get

requests.exceptions.MissingSchema: Invalid URL '/api/viewname/': No schema supplied. Perhaps you meant http:///api/viewname/?

Do I have to look whether the server is running on the localhost or is there a more generic way?

Upvotes: 0

Views: 69

Answers (1)

doniyor
doniyor

Reputation: 37846

requests module needs the absolute url to post to. you need

url = 'http://%s%s' % (request.META['HTTP_HOST'], reverse('name-of-view'))     
requests.post(url, data=some_data)

Upvotes: 1

Related Questions