thebeancounter
thebeancounter

Reputation: 4829

ways to process a post request in django view and re-send it to another view

I have a view that receives a post request from HTML form, and i like to do some process and then send another post request to another view with some of the original data, and some more data that i add to it,

i read that HTTP does not allow to pass the same post request you received.

i read the official documentation and don't find a way of doing so, and i read about the library requests, and when i tried using it and then send the request i made by httpresponseredirect, it keeps sending it as post by writing tons of things in the address bar, and it kind of looks like it tries to use get instead of post.

i found here these questions:

Which function in django creates a HttpRequest instance and hands to a view?

Simulating a POST request in Django

but i don't understand from it how can i create a request in an efficient way, from my django app, and send it to another view of even another app.

post_data = {'name': 'something'}
response = requests.post('http://localhost:8000/polls/thanks/', name =  "somthing")
content = response.content
return HttpResponseRedirect(content)

Upvotes: 0

Views: 2898

Answers (1)

Sayse
Sayse

Reputation: 43300

Views are nothing more than functions in python so there isn't anything stopping you from just passing the parameters into a separate view. Whether or not thats always a good thing is a different thing

def view_a(request):
    # Logic here
    return view_b(request)

Based on your comment:

What you're trying to do seems like a long winded way of something that can be achieved with the .get method

last_name = request.POST.get('last_name', generate_name())

What this would do is use the last name if its provided, otherwise it will generate one.

If you're using a form, you can do the same kind of thing, just in the clean method

def clean_last_name(self):
    last_name = self.cleaned_data.get('last_name')
    if last_name:
        return last_name
    else:
        return generate_name()

Upvotes: 1

Related Questions