Reputation: 563
Facing some constraints in a website, I was obligated to try to post data to some view from another view (I suppose it does make sense), like:
def view1(request):
if request.method == 'POST':
value = request.POST.get('h1')
''' '''
And in my view2, I would do something like:
def view2(request):
if constraint:
python.post(/url/view1/,data={'h1':1}) # Doesn't exist
# Just a demonstration
Is there a way to do what I want?
Upvotes: 4
Views: 10705
Reputation: 11
Take a look at Python Requests: I found it in in Python forum to print which could be modified to post.
import requests
req = requests.Request('POST','http://stackoverflow.com',headers={'X-Custom':'Test'},data='a=1&b=2')
prepared = req.prepare()
def pretty_print_POST(req):
"""
At this point it is completely built and ready
to be fired; it is "prepared".
However pay attention at the formatting used in
this function because it is programmed to be pretty
printed and may differ from the actual request.
"""
print('{}\n{}\n{}\n\n{}'.format(
'-----------START-----------',
req.method + ' ' + req.url,
'\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items()),
req.body,
))
pretty_print_POST(prepared)
Upvotes: -1
Reputation: 1860
you could use the requests package to send request to other URLs, the question is "why" ??.
Why not extract the code of view1 in a utility function and call int from view2?
If you need to use an new HTTP request, I suggest to use the Django reverse()
function to the the URL from the urls.py
configuration (refer to the official documentation )
Upvotes: 4