enoren5
enoren5

Reputation: 433

Django malformed views.py or urls.py (redactor app)

I’m trying to write a web app which accepts a website visitor’s input which is a 12 digit “Chuckee Cheese” membership card number and then redacts the first 8 digits and presents the redacted number. I’ve got my template written and the basic logic inside my app’s views.py. The problem now is that after the user enters his or her card number, Django is not processing the user input properly. Like, the redacted number is not being presented and served in the template as intended.

Here is a pic on imgur showing my website running on my dev server now. As you can see in that pic, in the web address bar Django receives the ‘ccEntry’ GET Request with ‘123456789102’ as user input. So I guess that kind of works. But below the two h1 elements (in lime green), Django should show the card number ‘123456789102’ as well as the redacted card number ‘xxxx xxxx 9102’ but instead it’s blank. What is wrong here? As far as I can tell, I believe the problem involves either the first two functions inside my redactors views.py or the way my app’s urls.py is arranged.

Here is my 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 my app’s urls.py:

from django.urls import path, include
from . import views
 
 
urlpatterns = [
   path('home', views.home, name='home'),
   path('results', views.results, name='results'),
]

Those are the two scripts where I believe the problem is.

For what it is worth, here are some other related configuration files and scripts that are in play:

Lightly abbreviated alls/landings.html template:

{% load static %}
 
<!doctype html>
 
<html lang="en">
<head>
 <meta charset="utf-8">
 <meta name="description" content="The HTML5 Herald">
 <meta name="robots" content="noindex,nofollow" />
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <!-- Custom -->
 <link rel="stylesheet" href="{% static 'css/style.css' %}">
 
</head>
  
<body>
  
 {% block content %}   
  
   <div class="card-processor">
   <h3>Enter your fake Chuckee Cheese Neptune membership card number!</h3>
   <form action="{% url 'posts' %}" 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 memnership 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 -->
 
 <div class="post-content">
 {% for post in posts %}
   <h1> Blog post title: <em>{{ post.title }}</strong></em>
   <h4>Publication Date: {{ post.pub_date_preference }}</h4>
   <img src="{{ post.image.url }}" class="authors-pic" style="" />
    
   <!-- Body text should go here :   -->
  
   <p>{{ post.body|safe }}</p>
 {% endfor %}
 
 
{% endblock %}
 
 </body>
 
</html>

Parent urls.py router:

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)

I believe those are all the relevant files in play. However in case the problem is elsewhere, if you want to see the rest of my source code, here is a static snapshot tagged as v.0.7.0 on my GitHub.

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.

Upvotes: 0

Views: 124

Answers (1)

Ayush Kumar
Ayush Kumar

Reputation: 81

It seems like the 'form' in landings.html is being submitted to the path with name "posts", But there is no path with this name in your app's urls.py.

Use this <form action="{% url 'home' %}" method="get"> instead of <form action="{% url 'posts' %}" method="get">.

Upvotes: 1

Related Questions