I have a problem: I can’t understand how I can pass additional data to the redirect
I read the answers: this, this, this but these links did not give me clarity
in HTML file script:
function cmb_pos() {
var data = {
'dep_id': dep_id, 'div_id': div_id,
}
fetch('/job-history/add-new/intermediate/{{persona_id}}', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(response => console.log(JSON.stringify(response)))
};
first: (a function that accepts a POST, performs actions and redirects to another function)
from starlette.datastructures import URL
@router.post(path="/add-new/intermediate/{persona_id}",
response_class=responses.RedirectResponse,
)
async def my_test(request: Request, item: Item, persona_id: int, service: Related):
staff_schedule = await service.get_item_by_where(....)
context_ext = {}
context_ext["request"] = request
context_ext["staff_schedule"] = staff_schedule
context_ext["flag"] = True
redirect_url = URL(request.url_for("create_job_history_init", persona_id=persona_id))
.include_query_params(context_ext=context_ext)
return responses.RedirectResponse(
url=redirect_url,
status_code=status.HTTP_303_SEE_OTHER,
)
second: (function to which the redirect occurs)
@router.get(path="/persona_id={persona_id}/add-new",
response_class=responses.HTMLResponse,
)
async def create_job_history_init(request: Request, persona_id: int,
schedule_service: CommonsDep, context_ext: Optional[dict] = None,
):
context = {}
schedule = await schedule_service.get_all_item()
related = ......
context = schedule | related
context["request"] = request
context["persona_id"] = persona_id
context["context_ext"] = context_ext
return backend.webapps.templates.TemplateResponse(
name="job_history/create_job_history.html",
context=context,
)
I got this error:
File "/Users/pas/------/backend/webapps/job_history.py", line 276, in my_test ).include_query_params(context_ext=context_ext)
File "/Users/pas/-------/.my_pers/lib/python3.11/site-packages/starlette/datastructures.py", line 143, in include_query_params params = MultiDict(parse_qsl(self.query, keep_blank_values=True))
File "/Users/pas/---------/.my_pers/lib/python3.11/site-packages/starlette/datastructures.py", line 83, in query return self.components.query
File "/Users/pas/---------/.my_pers/lib/python3.11/site-packages/starlette/datastructures.py", line 66, in components self._components = urlsplit(self._url)
TypeError: unhashable type: 'URL'
what am I doing wrong? Is it possible to transfer additional data via a redirect?
thank you very much in advance
update: in this This solution is proposed, but I get an error in it
return tuple(x.decode(encoding, errors) if x else '' for x in args)
AttributeError: 'URL' object has no attribute 'decode'
parsed = list(urllib.parse.urlparse(redirect_url))
for me the right solution is this:
parsed = list(urllib.parse.urlparse(redirect_url ._url )) after adding ._url I was able to parse
after brainstorming I found this solution for myself
the task: There are three comboboxes on the page, I select the first and second, the values of the third depend on the values of the first two. The values for the third i are obtained from the database. After receiving the values of the first and second, I am redirected where I create a dictionary and pass it to the initial method. Then I open the page with the combobox again and fill the third combobox with the values from the database
I hope I explained my task clearly
1) initial method:
2) in the browser we get
unfortunately I don't have enough rights to post pictures
there are 3 comboboxes in the picture
3) using JS I create a redirect so it’s seamless
4) method that accepts a redirect
variable parsed is:
after adding the dictionary:
the address will look like this:
but I still have one problem I can't reload the page with new data