首页 > 解决方案 > Django 格式错误的 views.py 或 urls.py(编辑器应用程序)

问题描述

我正在尝试编写一个网络应用程序,它接受网站访问者的输入,这是一个 12 位数的“Chuckee Cheese”会员卡号码,然后编辑前 8 位数字并显示编辑后的号码。我已经编写了模板,并在我的应用程序的 views.py 中编写了基本逻辑。现在的问题是,在用户输入他或她的卡号后,Django 没有正确处理用户输入。就像,编辑后的数字没有按预期在模板中呈现和提供。

这是 imgur 上的一张图片,显示我的网站现在在我的开发服务器上运行。正如您在该图片中看到的那样,在 Web 地址栏中,Django 接收带有“123456789102”作为用户输入的“ccEntry”GET 请求。所以我想这种工作。但是在这两个h1元素下方(浅绿色),Django 应该显示卡号“123456789102”以及经过编辑的卡号“xxxx xxxx 9102”,但它是空白的。这里有什么问题?据我所知,我认为问题涉及我的redactorsviews.py 中的前两个函数或我的应用程序的urls.py 的排列方式。

这是我的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')

这是我的应用程序的urls.py

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

这是我认为问题所在的两个脚本。

对于它的价值,这里有一些其他相关的配置文件和脚本正在发挥作用:

略微缩写的 alls/landings.html模板:

{% 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>

urls.py路由器:

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)

我相信这些都是在发挥作用的所有相关文件。但是,如果问题出在其他地方,如果您想查看我的源代码的其余部分,这里是我的 GitHub 上标记为 v.0.7.0 的静态快照

还值得注意的是,我没有得到回溯,我的服务器也没有崩溃,所以我在谷歌上搜索解决类似或相关问题的其他开发人员方面没有太多线索。

标签: pythondjango

解决方案


似乎landings.html中的“表单”被提交到名为“ posts ”的路径,但在您的应用程序的urls.py中没有此名称的路径。

使用它<form action="{% url 'home' %}" method="get">而不是<form action="{% url 'posts' %}" method="get">.


推荐阅读