Django: Change default parameter appended in the URL in forms - python

I have a form like this:
<form method="get">
<button name="load" type="submit" value="{{game.game_id}}|{{game.champion}}">Show user's stats</button>
</form>
When the user submits the form, the parameter will automatically be appended into the URL like this
www.example.com/?load=5293733926|99
However, I want to add manually another parameter to the URL making the URL look like this:
www.example.com/?load=5293733926|99&page=2
If I manually type this to the URL, it goes to where I want. How should I do this? I tried adding:
action="./?load={{game.game_id}}|{{game.champion}}&page={{game.page}}"
and multiple other variants.
But it doesn't work, it redirects to www.example.com/?load=5293733926|99.
Context: I have a huge list of dictionaries and if I show them all at the same time and process them, it takes a lot of time. I divided the list with Django's paginator. The forms act as a load more. By default, I show some data from the dictionary and when the user clicks the form button, some extra information is showed. However, if someone clicks the form button in page 2 www.example.com/?page=2, Django would redirect the user to the first page after processing the data. In order to redirect the user to the submitted button's page, I would need to redirect the user to www.example.com/?load=5293733926|99&page=2
It's my first time asking a question here, if some more data needs to be provided, please let me know. Thanks!
P.D: This is a piece of the list of dictionaries:
{"matches":[{"platformId":"EUW1","gameId":5304134881,"champion":99,"queue":420,"season":13,"timestamp":1622752495039,"role":"SOLO","lane":"MID"},{"platformId":"EUW1","gameId":5303158955,"champion":99,"queue":420,"season":13,"timestamp":1622727838679,"role":"SOLO","lane":"MID"},{"platformId":"EUW1","gameId":5302981978,"champion":99,"queue":420,"season":13,"timestamp":1622718693646,"role":"SOLO","lane":"MID"},{"platformId":"EUW1","gameId":5302393136,"champion":64,"queue":400,"season":13,"timestamp":1622666939087,"role":"NONE","lane":"JUNGLE"},{"platformId":"EUW1","gameId":5301715149,"champion":103,"queue":420,"season":13,"timestamp":1622640835828,"role":"DUO","lane":"MID"},{"platformId":"EUW1","gameId":5301720723,"champion":99,"queue":420,"season":13,"timestamp":1622638646725,"role":"SOLO","lane":"MID"},{"platformId":"EUW1","gameId":5301040032,"champion":497,"queue":420,"season":13,"timestamp":1622625968514,"role":"DUO_SUPPORT","lane":"BOTTOM"},
With the form, I would get the champion number and the gameId in order to process the data of the game. A piece of the game's data looks like this:
{"gameId":5304134881,"platformId":"EUW1","gameCreation":1622752495039,"gameDuration":1983,"queueId":420,"mapId":11,"seasonId":13,"gameVersion":"11.11.377.6311","gameMode":"CLASSIC","gameType":"MATCHED_GAME","teams":[{"teamId":100,"win":"Win","firstBlood":false,"firstTower":false,"firstInhibitor":true,"firstBaron":true,"firstDragon":false,"firstRiftHerald":true,"towerKills":11,"inhibitorKills":3,"baronKills":1,"dragonKills":4,"vilemawKills":0,"riftHeraldKills":1,"dominionVictoryScore":0,"bans":[{"championId":63,"pickTurn":1},{"championId":111,"pickTurn":2},{"championId":25,"pickTurn":3},{"championId":55,"pickTurn":4},{"championId":235,"pickTurn":5}]},{"teamId":200,"win":"Fail","firstBlood":true,"firstTower":true,"firstInhibitor":false,"firstBaron":false,"firstDragon":true,"firstRiftHerald":false,"towerKills":2,"inhibitorKills":0,"baronKills":0,"dragonKills":1,"vilemawKills":0,"riftHeraldKills":0,"dominionVictoryScore":0,"bans":[{"championId":235,"pickTurn":6},{"championId":62,"pickTurn":7},{"championId":234,"pickTurn":8},{"championId":25,"pickTurn":9},{"championId":777,"pickTurn":10}]}],"participants":[{"participantId":1,"teamId":100,"championId":98,"spell1Id":4,"spell2Id":12,"stats":{"participantId":1,"win":true,"item0":1054,"item1":3075,"item2":3047,"item3":3068,"item4":3748,"item5":3001,"item6":3340,"kills":10,"deaths":5,"assists":20,"largestKillingSpree":5,"largestMultiKill":2,"killingSprees":2,"longestTimeSpentLiving":936,"doubleKills":1,"tripleKills":0,"quadraKills":0,"pentaKills":0,"unrealKills":0,"totalDamageDealt":163173,"magicDamageDealt":58700,"physicalDamageDealt":81055,"trueDamageDealt":23417,"largestCriticalStrike":0,"totalDamageDealtToChampions":28604,"magicDamageDealtToChampions":15265,"physicalDamageDealtToChampions":13021,"trueDamageDealtToChampions":317,"totalHeal":7157,"totalUnitsHealed":1,"damageSelfMitigated":49148,"damageDealtToObjectives":7345,"damageDealtToTurrets":7345,"visionScore":16,"timeCCingOthers":36,"totalDamageTaken":33907,"magicalDamageTaken":9592,"physicalDamageTaken":18466,"trueDamageTaken":5848,"goldEarned":14812,"goldSpent":13775,"turretKills":3,"inhibitorKills":1,"totalMinionsKilled":163,"neutralMinionsKilled":8,"neutralMinionsKilledTeamJungle":4,"neutralMinionsKilledEnemyJungle":4,"totalTimeCrowdControlDealt":264,"champLevel":18,"visionWardsBoughtInGame":1,"sightWardsBoughtInGame":0,"wardsPlaced":10,"wardsKilled":0,"firstBloodKill":false,"firstBloodAssist":false,"firstTowerKill":false,"firstTowerAssist":false,"firstInhibitorKill":false,"firstInhibitorAssist":false,"combatPlayerScore":0,"objectivePlayerScore":0,"totalPlayerScore":0,"totalScoreRank":0,"playerScore0":0,"playerScore1":0,"playerScore2":0,"playerScore3":0,"playerScore4":0,"playerScore5":0,"playerScore6":0,"playerScore7":0,"playerScore8":0,"playerScore9":0,"perk0":8437,"perk0Var1":2259,"perk0Var2":1714,"perk0Var3":0,"perk1":8446,"perk1Var1":3620,"perk1Var2":0,"perk1Var3":0,"perk2":8444,"perk2Var1":2027,"perk2Var2":0,"perk2Var3":0,"perk3":8451,"perk3Var1":269,"perk3Var2":0,"perk3Var3":0,"perk4":9111,"perk4Var1":2635,"perk4Var2":600,"perk4Var3":0,"perk5":9104,"perk5Var1":12,"perk5Var2":40,"perk5Var3":0,"perkPrimaryStyle":8400,"perkSubStyle":8000,"statPerk0":5005,"statPerk1":5008,"statPerk2":5002},"timeline":{"participantId":1,"creepsPerMinDeltas":{"10-20":5.199999999999999,"0-10":4.3,"20-30":5.7},"xpPerMinDeltas":{"10-20":562.5,"0-10":446.4,"20-30":638.2},"goldPerMinDeltas":{"10-20":418,"0-10":304.1,"20-30":514.8},"csDiffPerMinDeltas":{"10-20":-0.8000000000000003,"0-10":-2.2,"20-30":1.1999999999999997},"xpDiffPerMinDeltas":{"10-20":56.50000000000003,"0-10":40.79999999999998,"20-30":232.3},"damageTakenPerMinDeltas":{"10-20":666.8,"0-10":686,"20-30":1504.1},"damageTakenDiffPerMinDeltas":{"10-20":-519.2,"0-10":18.00000000000003,"20-30":-1596.5},"role":"SOLO","lane":"TOP"}},

Related

How to redirect a user to a different page if he's logging in for the first time after registration in django?

I have created a Getting Started page to which I have to send the users if they are logging in for the first time after registration.
I also referred to this question but didn't understand anything
If not I have to redirect them to the Home page.
I have set the LOGIN_REDIRECT_URL to 'home' in settings.py
I have not tried anything yet because I have no idea about how I should do it. Please assume a simple html for both the pages with an <h1> tag containing their respective names
There are multiple ways to achieve this, and this are three that I usually do:
you can put additional field in user model, for example seen_getting_started that is boolean and during processing of home template in views.py you can check for this field and redirect accordingly to getting started page. I have an option that user needs to select his language and date formats, so if it not changed and stored in his profile than "select language and date format" page pops out
you could also check his creation time, if it is created in last 10 minutes it is safe to assume that the user is new, but there might be some issues with that if you use for example mail check during registration
if you are using mail check during registration, than you need to lend user on "email confirmed" page that could also lead to your getting started page.
Hope it helps. Sure that other options might work too.

How to save data to the same model from forms in different pages...Registration multiple fields? Django

i'm trying to make a user registration with multiple fields to save in the same model. The idea is have a form in the home page with username and password and when press button continue the user is redirected to another page with multiple fields to insert personal info for the user profile.
I imagine i must have two different views for that, but i don't understand how put all this data together.
Thanks!
You could use the FormWizard (https://django-formtools.readthedocs.io/en/latest/wizard.html)
Basically you would have one form for user/password and another form for the other fields.
Take a look
In your case i would prefer manipulate HTML content with JavaScript and after everything is ready, just send an ajax call.

Redirect to the POST form page

I'm developing an application using Django. The main page contains a textarea input where you must enter a list of IDs(this list can be large, so I submit the form using POST). After submiting the form, the server will render a page with a table where you can find informations about every ID. If you click on an ID, you will go to a page where you can modify some fields. For saving the modified fields into the database, the form action is: 'manage_ids/<>/save' and on the server I have a view for this url. How can I redirect the user back to the page with the table? I'm submiting the form using POST so a simple redirect wouldn't work. Or, to remain on the page where you can modify the fields(in this case, if you press the 'Back' browser button, to be redirected to the table page).
Workflow:
/
POST
/manage_ids
GET
/manage_ids/<<md5>>
POST(save)
/manage_ids/<<md5>>/save
Now, I should be redirected to:
/manage_ids
If you need to preserve a long list of IDs across multiple page requests, you should use the session.
I'd like to recommend you take a look at Django Form wizards

Performing sensitive Django actions while masking/hiding underlying URL

I have a simple Django view that confirms the deletion of an instance. On this page is a simple form containing two buttons, "Cancel" and "Delete."
The Cancel button simply returns the user to the page from which the original delete button was pressed.
The Delete button jumps to a second view that performs the actual action. Thus, my URLs are defined as follows:
url(r'^confirmDeleteItem/(?P<item_key>\w+)$', 'confirm_delete_item'), # Confirms
url(r'^deleteItem/(?P<item_key>\w+)$', 'delete_item'), # Performs the action
On the confirmation page, the form is defined with a POST action that visits the second URL:
<form action="/squash/deleteItem/{{ item.key }}/" method="POST">
...
</form>
The problem I have with this is that the Items are fairly large (they store lots of data) and sensitive, so I'd like to force the user to jump through the confirmation hoop every time.
I would like to either prevent the User from visiting the /deleteItem/ page manually, or just hide the browser's loading of this page to avoid from it becoming stored in the history, accidentally bookmarked, etc.
Is wrapping the action in an AJAX call the best way to solve this problem, or are there more standard/preferred solutions? Thanks!
How about setting a session variable in confirmDeleteItem view (i.e. prepareToDelete = item.key) and checking in deleteItem view whether this session variable exists and whether the value match the item.key? Then you'd just need to remove it after actual delete occurred.
A quick idea which I'm not sure of, but should work in your case.
Check your referer in your deleteItem view. Like in this snippet of code. If user didn't come to the delete view from confirmDelete view, redirect him to the appropriate confirmDelete view.

Passing form parameters between views in Pyramid

I am creating a form that requires user confirmation before submitting the data. I would like a seperate confirmation page because I need to display quite a bit information about how the form data will be processed. I was wondering if there was a pythonic way to pass data between forms in Pyramid.
Submitting the form takes the user to the confirmation page. Thus, the view for the confirmation has the form data stored in request.POST. I was wondering if there was a clean way to pass along all of this data to the final view once the user hits 'submit' on the confirmation page. I would also like to add a boolean variable, confirmed, to the dictionary of parameters.
This is not a Pyramid-specific answer, but two common approaches to this problem are:
Store the data in a session.
Store the data as a hidden form on the confirmation page, and resubmit with "confirmed"
I like 2 much better because it's a stateless method. You can also use the exact same form processing logic, and just check for the presence of your "confirmed" POST variable to decide which action to take and view to show (i.e, either the "please confirm" view, or processing and the "processed" view.)

Categories

Resources