Python Forum
Django serving wrong template at the wrong address with malformed urls.py (redactor a
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Django serving wrong template at the wrong address with malformed urls.py (redactor a
#1
I’ve got a small experimental Django project which accepts a user’s fake Chuckee Cheese membership card number on the landing page, redacts the first 8 digits and shows it back to the user in redacted form.

Here is my website’s landing page showing http://127.0.0.1:8000/: https://i.imgur.com/yHlML9c.jpg

When a web user enters their 12 digit card number (for example) ‘111111111111’ into the entry box and clicks the “Redact!” button, Django takes the user to the http://127.0.0.1:8000/home/ location. The redacted card number is present, as expected (below the green <h3> font elements): https://i.imgur.com/vGFp5Op.jpg

The problem there is that the rest of the content is missing. The redaction functionality works but my intention is for Django to re-serve the same landing page with the redacted card number (rather than taking the user to a separate page). The problem now is either with my app’s views.py, urls.py and template (copied below). I’ve been going back and forth swapping out some variables and replacing them with others but I can’t get it to work.

Here is my urls.py:

from django.urls import path, include
from . import views


urlpatterns = [
   path('home', views.home, name='home'),
   # path('results', views.results, name='results'),
]
I’ve tried swapping out the first path variable from ’home’ to ’’ intending Django to serve the results to the parent landing page. I also changed the name variable to ’posts’ and changed the form url hook from ‘home’ to ‘posts’ inside my template. With these changes, when the “Redact!” button is pressed, Django serves the landing page with the GET request data showing in the web address bar as intended, but then the membership card number doesn’t show below the green heading <h3> elements as redacted.

My question for all of you is: How do I get Django to process the client’s 12 digit number and serve it on the landing page (rather than taking the user to a separate page)?

Here are the relevant lines in my template: (full template page source code can be found here):

   <div class="card-processor">
 
   <h3>Enter your fake Chuckee Cheese Neptune membership card number!</h3>
  
   <form action="{% url 'home' %}" method="get">
    
     <div> 
       <label for="password">Enter Card Number:</label>
       <input type="text" id="password" name="ccEntry" pattern="[0-9]{12}" maxlength="12"/>
       <div class="requirements">Must be a 12 digit number and no letters. </div>
       <input type="submit" value="Redact!" class="button"/>
     </div>
 
   </form>
  
   <h1>Here is your fake Chuckee Cheese Neptune membership card number!</h1>
   <h3 style="color:lime">This was the original number that you entered:</h3>
   <div class="field">{{ number }}</div>
   <h3 style="color:lime">Here it is redacted:</h3>
   <div class="field">{{ redacted_num }}</div>    
   <a href="{% url 'posts' %}"><div class="field"><strong>Again? Click here!</strong></div></a>
  
 </div> <!--- END card-processor -->
Here is my app’s views.py:

from django.shortcuts import render
 
# Create your views here.
def redactors(request):
   return render(request, 'alls/landings.html')
 
def home(request):
   if 'ccEntry' in request.GET:
       number = request.GET['ccEntry']
       redacted_num = 'xxxx xxxx {}'.format(number[-4:])
       return render(request, 'alls/landings.html', {'number':number, 'redacted_num':redacted_num})
   else:
       return render(request, 'alls/landings.html')
 
def results(request):
    return render(request, 'alls/landings.html')
Here is the main urls.py in the project directory:

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
 
urlpatterns = [
   path('admin/', admin.site.urls),
   path('', include('posts.urls')),
   path('', include('redactors.urls')),
   path('', include('counters.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In case any of you need to explore some of my other files, here is a stratic snapshot of all my source code in its current state on GitHub (tagged as v0.8.0).

It's also worth noting that I'm not getting a trace back and my server is not crashing so I don't have many leads in terms of searching on Google for other developers resolving similar or related issues.

I’m running Python v3.8, Django v2.2 on Manjaro stable.
Reply
#2
Why do you need to go back to the server to get the entire page? Couldn't the server just respond with some JSON containing the new number and you just use JavaScript to make the request and update the right part of the page with the data?
Reply
#3
I arrived at the solution by making three changes.

Firstly, I swapped the order of lines 8 and 9 in my parent urls.py so that it looks like this now:

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
  
urlpatterns = [
   path('admin/', admin.site.urls),
   path('', include('redactors.urls')),
   path('', include('posts.urls')),
   path('', include('counters.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Next I pass in 'posts':posts, as part of the context inside the home function inside the redactors’ app views.py.

Thirdly I changed ‘home’ to ’’ inside the redactors app urls.py.

It now runs as intended.

ndc85430: Perhaps JavaScript is better suited to implement my feature however right now I am focused on learning Python and Django. Once I become competent enough in Django maybe I will take up some JavaScript courseware.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Don't know what went wrong Tejas 1 617 Jun-23-2020, 11:45 AM
Last Post: nuffink
  Wrong number of google results in a date range Val 0 807 Mar-15-2020, 02:29 PM
Last Post: Val
  What i do wrong? In response i get home page code aruzo 1 762 Feb-23-2020, 11:32 PM
Last Post: micseydel
  Why use HTML in Django Template ift38375 2 1,299 Dec-09-2019, 02:28 AM
Last Post: snippsat
  what is wrong with this code? markayala 1 810 Aug-06-2019, 06:18 PM
Last Post: Gribouillis
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 1,456 Jun-30-2019, 12:21 AM
Last Post: scidam
  malformed header from script 'main.py': Bad header: * Serving Flask app "main" anuragsapanbharat 2 2,350 Jun-12-2019, 07:26 AM
Last Post: anuragsapanbharat
  Regex URLs Django 2.1 sterion66 0 1,602 Nov-04-2018, 10:22 AM
Last Post: sterion66
  Serving mixed filenames with send_from_directory() errors out. nikos 0 1,109 Sep-20-2018, 03:11 AM
Last Post: nikos
  I can't understand what's wrong with me? please help me!!! 573462273 1 2,028 Jul-07-2018, 08:08 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020