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


Messages In This Thread
Django serving wrong template at the wrong address with malformed urls.py (redactor a - by Drone4four - Aug-11-2020, 12:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  What am I doing wrong? Larz60+ 2 1,615 Apr-29-2022, 12:11 PM
Last Post: Larz60+
  BeautifulSoup not parsing other URLs giddyhead 0 1,168 Feb-23-2022, 05:35 PM
Last Post: giddyhead
  django - reset password template rwahdan 2 1,951 Dec-26-2021, 09:09 PM
Last Post: Jeff900
  what is wrong with my code? greenpine 0 1,296 Nov-08-2021, 10:01 PM
Last Post: greenpine
  Need logic on how to scrap 100K URLs goodmind 2 2,569 Jun-29-2020, 09:53 AM
Last Post: goodmind
  Don't know what went wrong Tejas 1 1,533 Jun-23-2020, 11:45 AM
Last Post: nuffink
  Wrong number of google results in a date range Val 0 1,818 Mar-15-2020, 02:29 PM
Last Post: Val
  What i do wrong? In response i get home page code aruzo 1 1,546 Feb-23-2020, 11:32 PM
Last Post: micseydel
  Why use HTML in Django Template ift38375 2 2,773 Dec-09-2019, 02:28 AM
Last Post: snippsat
  Scrape multiple urls LXML santdoyle 1 3,512 Oct-26-2019, 09:53 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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