由于项目主urls.py中的配置错误,Django无法正确处理获取请求 [英] Django fails to process get request properly with a misconfiguration in the project’s main urls.py

查看:48
本文介绍了由于项目主urls.py中的配置错误,Django无法正确处理获取请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Django编写的网站,这是一个非常基本的基本CMS.现在,我要实现的功能涉及当网络用户输入其假的12位数字的丘奇奶酪会员卡号时,Django应该删除前8位数字并将其提供给用户.所有这些都在带有博客文章文本内容的主登录页面上进行.

I’ve got a website I’m writing using Django which is a very basic, rudimentary CMS. For now the feature I am trying to implement involves when a web user enters their fake 12-digit chuckee cheese membership card number, Django should redact the first 8 digits and present it back to the user. All of this takes place on the main landing page with blog post text content.

这是在我项目的父urls.py中声明的 urlspatterns 变量:

Here is the urlspatterns variable declared inside my project’s parent urls.py:

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)

使用该确切的网址模式,结果就是这样.您可以在该图片中看到,当网络用户在右侧网页上输入其会员卡号时,该卡号(绿色标题元素下方)将按预期方式呈现并进行处理(在此网址位置, http://127.0.0.1:8000/?ccEntry=111111111111 .问题是登录页面(如左侧在 http://127.0.0.1:8000/),在缺少博客文章内容的情况下呈现模板.

With that exact urlspatterns, the result is this. As you can see in that image, when the web user enters their membership card number, on the webpage to the right, the card number (below the green heading elements) renders and is processed as intended (which is at this web address location, http://127.0.0.1:8000/?ccEntry=111111111111. The problem is the landing page (as depicted on the left at http://127.0.0.1:8000/), renders the template with the blog post content missing.

一种解决方案是交换 path('',include('redactors.urls')), path('',include('posts.urls')),因为它们出现在urlpatterns列表中.进行此更改后,结果是.如您在第二张图片中所见,该博客文章的内容在输入和不输入卡号的情况下都会呈现,但是右侧网页下方的绿色标题元素下方没有处理,只是空白.我希望看到:"xxxx xxxx 1111".

One solution would be to swap the order in which path('', include('redactors.urls')), and path('', include('posts.urls')), as they appear inside urlpatterns list. With that change, the result is this. As you can see in this second image, the blog post content renders with and without the card number entered but then below the green heading element doesn’t process in the webpage to the right, it’s just blank. I’m expecting to see: ‘xxxx xxxx 1111’.

我的问题是:我如何获取 ccEntry 获取请求以进行处理,以便当用户登陆首页和当网络用户输入其12位数字的卡号时?

My question is: How do I get the ccEntry get request to process so that the blog post content text is rendered when the user lands on the home page and when the web user enters their 12-digit card number?

值得注意的是,我没有追溯到以前,我的服务器也没有崩溃,因此在Google上寻找其他解决类似或相关问题的开发人员方面,我的线索不多.

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.

以下是视图,模板和正在播放的urls.py.

Here are the views, the template and the urls.py in play.

主项目urls.py :

Main project 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('redactors.urls')),
   path('', include('posts.urls')),   
   path('', include('counters.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

注意 path('',include('redactors.urls')), path('',include('posts.urls')),当我担任两个职位时,我得到了不同的结果,但仍然没有实现我的目标(如上所述).

Take note of the positioning of path('', include('redactors.urls')), and path('', include('posts.urls')), When I the two positions, I get a different result but still does not achieve my goal (as described above).

redactors.views >:

redactors.views:

from django.shortcuts import render
from posts.models import Posts
 
def home(request):
   if 'ccEntry' in request.GET:
       number = request.GET['ccEntry']
       redacted_num = 'xxxx xxxx {}'.format(number[-4:])
       posts = Posts.objects.all().order_by('-pub_date')
       # context = {'posts':posts}
       return render(request, 'alls/landings.html', {'number':number, 'redacted_num':redacted_num, 'posts':posts, })
   else:
       return render(request, 'alls/landings.html')

posts.views >:

posts.views:

from django.shortcuts import redirect, render, get_object_or_404
from posts.models import Posts

def posts(request):
  posts = Posts.objects.all().order_by('-pub_date')
  context = {'posts':posts}
  return render(request, 'alls/landings.html', context)

templates/alls.html :

templates/alls.html:

 
   <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 -->
 
 <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" />
    
   <!-- Body text should go here :   -->
  
   <p>{{ post.body|safe }}</p>
 
 {% endfor %}

我认为上述三个文件是我的问题所在.但是如果您需要浏览其他项目文件,请这是静态快照(标记为作为v0.9.0)在我的GitHub上的完整源代码回购.

The above three files I think is where my problem is. But in case you people need to explore some of my other project files, here is a static snapshot (tagged as v0.9.0) of the full source code repo on my GitHub.

推荐答案

奇怪:片刻之前,我将我的 redactors 分支合并到了 master 中.有两个较小的合并冲突.

Strange: Moments ago I merged my redactors branch into master. There were two minor merge conflicts.

在我的编辑器应用程序的 urls.py 中,我的 urlpatterns 列表变量过去看起来像这样:

My urlpatterns list variable inside my redactor app's urls.py used to look like this:

urlpatterns = [
    path('', views.home, name='home'),
]

基本上,当我像这样将"home"`传递给第一个路径参数时:

Basically, when I passed in 'home'` to the first path argument like this:

urlpatterns = [
    path('home', views.home, name='home'),
]

...一切似乎都按预期运行.无论有没有表单输入,都会显示博客文章内容,并且POST请求处理将按预期进行.哇!

...everything seems to run as intended. The blog post content is present with or without form input and the POST request processes as expected. Hooray!

这篇关于由于项目主urls.py中的配置错误,Django无法正确处理获取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆