Handle Form Submission

55 views
Skip to first unread message

Tawfeeq Zaghlool

unread,
May 27, 2024, 10:14:04 AMMay 27
to Django users

Hello Djangooos:

I am building a simple Django application, that shows products in items/employee_id url, the customer opens the link and start selecting the items by clicking on them, then an action button appears to prepare for submission to display a modal asking the customer to enter his data name, phone number location and email, then the customer either clicks on submit the order or cancel, if he clicks on submit the order button it will redirect to messenger chat page with order details where he can send the order which he selects, this popup window (handle_form_submission) asks the customer to either send the message or cancel sending, send the message button will open the Chat Page on The Agent chat page with a copy of the Order details and wait for the customer to send the order, the cancel will redirect the customer to the items/user_id url the same where he starts, if the user clicks on send the order, and then clicks on a back button from the browser it should redirect him also to the items/user_id url where he starts, to start a new order or close the page.

I have two issues as follows:

1- Cancel button in the popup redirect message doesn't redirect to the items/user_id url link, it redirects to the items/

2- After Sending the message, if clicks the back button on the browser it will take to the previous step which is the popup message, this back button should redirect to the items/user_id


The order cycle is as follows:

Selection > Prepare for submission > Submit the order > Send the message.

I use session in Django project to store the user_id.

Faisal Mahmood

unread,
May 29, 2024, 7:33:56 AMMay 29
to Django users

I'd be glad to craft a solution that addresses the issues you've outlined:

Solutions:

1. Cancel Button Redirect Issue:

  • Problem: The cancel button in the popup redirects to items/ instead of items/user_id.
  • Solution:
    • Pass user_id in the form: Modify your form in the popup to include a hidden field containing the user_id retrieved from the session. This ensures the user_id persists even after form submission.
    • Access user_id in the view: In the view handling the cancel button form submission, access the user_id from the form data (hidden field) or the session.
    • Redirect with user_id: Use the retrieved user_id to construct the correct redirect URL (items/user_id) and redirect the user there.

Code Example (assuming a Django form):

from django.shortcuts import redirect def handle_form_submission(request): if request.method == 'POST': form = YourForm(request.POST) if form.is_valid(): # Handle order submission logic (if applicable) if 'cancel' in request.POST: # Check for cancel button submission user_id = request.POST.get('user_id') # Get user_id from hidden field return redirect('items/{}'.format(user_id)) # Redirect with user_id # ... handle submit logic return render(request, 'your_template.html', {'form': form})

2. Back Button Redirect Issue:

  • Problem: Clicking the browser's back button after sending the message redirects to the popup instead of items/user_id.
  • Solution:
    • Server-Side Session Flag: Set a session flag (order_submitted or similar) after successfully sending the message.
    • Check Session Flag in View: In the view handling the request after clicking back, check for the presence of the session flag.
    • Conditional Redirect: If the flag is present, redirect to items/user_id. Otherwise, display the normal page content.

Code Example:

from django.shortcuts import render, redirect def after_message_view(request): if request.session.get('order_submitted'): del request.session['order_submitted'] # Remove flag to avoid confusion later return redirect('items/{}'.format(request.session.get('user_id'))) # ... display normal page content return render(request, 'your_template.html')

Additional Considerations:

  • Messenger Chat Page: Consider using Django's built-in URL reverse functionality (reverse('messenger_chat_page')) to construct the URL for the chat page.
  • Error Handling: Implement robust error handling in both views to gracefully handle potential issues like missing session data or invalid form submissions.
  • Frontend UX: You might want to provide a more user-friendly experience by disabling the browser's back button or displaying a confirmation message when the user attempts to navigate back using the button.

By implementing these solutions and considerations, you should be able to achieve the desired behavior for your Django application, ensuring that the cancel button and back button both redirect the user to the items/user_id URL as expected.

don't hesitate to ask further info.

Tawfeeq Zaghlool

unread,
May 29, 2024, 7:51:24 AMMay 29
to django...@googlegroups.com
Thanks Faisal, I really appreciate it.
If I have any other questions, I will ask you then.
Thanks again mate :) :) 

Tawfeeq Zaghlool

IoT Full Stack Developer || Project Manager & Youth Leader


+962 77 8891479

     




--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/7mnPNB69lUU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a43d2c4d-e6b8-4a64-9f45-9a7d34bf5f54n%40googlegroups.com.

Faisal Mahmood

unread,
May 29, 2024, 7:55:01 AMMay 29
to Django users
YOU ARE WELCOME SIR,
HAVE A GOOD DAY AHEAD.

Reply all
Reply to author
Forward
0 new messages