Python Django不提供文本内容(第二次尝试)

Python Django不提供文本内容(第二次尝试),python,html,django,Python,Html,Django,摘要: 我正在编写一个Django web应用程序,它的目的是展示一个写作样本(“验尸报告”),基本上就像一篇博客文章。Django没有提供内容,我不知道为什么。我认为问题在于我的views.py或模板(复制如下)。我不认为问题出在我的模型或url.py上,但无论如何我都包括了它们 详细信息: 这是我第二次尝试让我的Django项目正确地服务于这个模板。在我之前的第一次尝试中,我遇到了类似的问题: 在这个问题上,另一位SO成员指出了我犯的三个错误。这里的问题是,我在模板中缺少for循环,在vi

摘要:

我正在编写一个Django web应用程序,它的目的是展示一个写作样本(“验尸报告”),基本上就像一篇博客文章。Django没有提供内容,我不知道为什么。我认为问题在于我的views.py或模板(复制如下)。我不认为问题出在我的模型或url.py上,但无论如何我都包括了它们

详细信息:

这是我第二次尝试让我的Django项目正确地服务于这个模板。在我之前的第一次尝试中,我遇到了类似的问题:

在这个问题上,另一位SO成员指出了我犯的三个错误。这里的问题是,我在模板中缺少for循环,在
views.py
中缺少
all()
class方法,并且
views.py
中的
context
字典键值对没有复数

这些问题已经解决,但我的新问题涉及Django在我期待Lorem Ipsum内容显示时提供一个空白模板

Django应该从my models.py中提取所有类对象,然后将它们发送到
alls/landings.html
。我期待的标题,出版日期,图像和正文内容呈现在我的登录页。但是,我的“mortems”应用程序内容(博客帖子)没有显示在登录页上。唯一显示的是标题。为了演示以便您可以看到我看到的内容,下面是我的登录页现在的样子:

根据大家在下面看到的内容,谁能告诉我views/landings.html模板有什么问题

完整的源代码可以在我的GitHub页面上找到。我的项目状态(标记为v0.6.0)

下面是我怀疑存在问题的代码示例

这是我最新的视图。py

from django.shortcuts import redirect, render, get_object_or_404
from mortems.models import Mortem
 
def mortems(request):
   mortems = Mortem.objects.all().order_by('-pub_date')
   context = {'mortems':mortems}
   return render(request, 'alls/landings.html', context)
{% load static %}
 
<html>
<head>
 <title> BLOG </title>
 <style>    </style>
 <!-- <link rel="stylesheet" href="{% static 'redactors/style.css' %}"> -->
 </head>
 <body>
   {% block content %}   
  <h1> BLOG POST:</h1>
 
 {% for mortem in mortems %}
   <h1>{{ mortem.title }}</h1>
   <h4>Date: {{ mortem.pub_date_preference }}</h4>
   <br />
   Image: <img src="{{ mortem.image.url }}" class="img-responsive center-block" style="max-height:300px;" />
   <br />
    
   <!-- Body text should go here :   -->
   Body Text:
   <p>{{ mortem.body|safe }}</p>
   <br />
   <br />
 {% endfor %}
 
{% endblock %}
 
 </body>
 
</html>
 
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('counters.urls')),
   path('', include('mortems.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django.urls import path, include
from . import views
 
 
urlpatterns = [
   path('', views.mortems, name='mortems'),
]
from django.db import models
import datetime
from django.utils import timezone
from django.utils.deprecation import MiddlewareMixin
 
# Create your models here.
 
class Mortem(models.Model):
   title = models.CharField(max_length=161)
   pub_date = models.DateTimeField()
   image = models.ImageField(upload_to='media/')
   body = models.TextField()
   now = datetime.datetime.now()
 
   def __str__(self):
       return self.title
 
   def pub_date_preference(self):
       # a = self.pub_date.timezone.now("US")
       b = self.pub_date.strftime("%A %d %B %Y @ %-I:%M:%S %p")
       # c = pytz.timezone("US")
       return (b)
 
   def summary(self):
       return self.body[:1024]
这是我的模板/alls/landings.html

from django.shortcuts import redirect, render, get_object_or_404
from mortems.models import Mortem
 
def mortems(request):
   mortems = Mortem.objects.all().order_by('-pub_date')
   context = {'mortems':mortems}
   return render(request, 'alls/landings.html', context)
{% load static %}
 
<html>
<head>
 <title> BLOG </title>
 <style>    </style>
 <!-- <link rel="stylesheet" href="{% static 'redactors/style.css' %}"> -->
 </head>
 <body>
   {% block content %}   
  <h1> BLOG POST:</h1>
 
 {% for mortem in mortems %}
   <h1>{{ mortem.title }}</h1>
   <h4>Date: {{ mortem.pub_date_preference }}</h4>
   <br />
   Image: <img src="{{ mortem.image.url }}" class="img-responsive center-block" style="max-height:300px;" />
   <br />
    
   <!-- Body text should go here :   -->
   Body Text:
   <p>{{ mortem.body|safe }}</p>
   <br />
   <br />
 {% endfor %}
 
{% endblock %}
 
 </body>
 
</html>
 
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('counters.urls')),
   path('', include('mortems.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django.urls import path, include
from . import views
 
 
urlpatterns = [
   path('', views.mortems, name='mortems'),
]
from django.db import models
import datetime
from django.utils import timezone
from django.utils.deprecation import MiddlewareMixin
 
# Create your models here.
 
class Mortem(models.Model):
   title = models.CharField(max_length=161)
   pub_date = models.DateTimeField()
   image = models.ImageField(upload_to='media/')
   body = models.TextField()
   now = datetime.datetime.now()
 
   def __str__(self):
       return self.title
 
   def pub_date_preference(self):
       # a = self.pub_date.timezone.now("US")
       b = self.pub_date.strftime("%A %d %B %Y @ %-I:%M:%S %p")
       # c = pytz.timezone("US")
       return (b)
 
   def summary(self):
       return self.body[:1024]
这是我的应用程序的(mortems)url.py

from django.shortcuts import redirect, render, get_object_or_404
from mortems.models import Mortem
 
def mortems(request):
   mortems = Mortem.objects.all().order_by('-pub_date')
   context = {'mortems':mortems}
   return render(request, 'alls/landings.html', context)
{% load static %}
 
<html>
<head>
 <title> BLOG </title>
 <style>    </style>
 <!-- <link rel="stylesheet" href="{% static 'redactors/style.css' %}"> -->
 </head>
 <body>
   {% block content %}   
  <h1> BLOG POST:</h1>
 
 {% for mortem in mortems %}
   <h1>{{ mortem.title }}</h1>
   <h4>Date: {{ mortem.pub_date_preference }}</h4>
   <br />
   Image: <img src="{{ mortem.image.url }}" class="img-responsive center-block" style="max-height:300px;" />
   <br />
    
   <!-- Body text should go here :   -->
   Body Text:
   <p>{{ mortem.body|safe }}</p>
   <br />
   <br />
 {% endfor %}
 
{% endblock %}
 
 </body>
 
</html>
 
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('counters.urls')),
   path('', include('mortems.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django.urls import path, include
from . import views
 
 
urlpatterns = [
   path('', views.mortems, name='mortems'),
]
from django.db import models
import datetime
from django.utils import timezone
from django.utils.deprecation import MiddlewareMixin
 
# Create your models here.
 
class Mortem(models.Model):
   title = models.CharField(max_length=161)
   pub_date = models.DateTimeField()
   image = models.ImageField(upload_to='media/')
   body = models.TextField()
   now = datetime.datetime.now()
 
   def __str__(self):
       return self.title
 
   def pub_date_preference(self):
       # a = self.pub_date.timezone.now("US")
       b = self.pub_date.strftime("%A %d %B %Y @ %-I:%M:%S %p")
       # c = pytz.timezone("US")
       return (b)
 
   def summary(self):
       return self.body[:1024]
这是我的应用程序的模型。py

from django.shortcuts import redirect, render, get_object_or_404
from mortems.models import Mortem
 
def mortems(request):
   mortems = Mortem.objects.all().order_by('-pub_date')
   context = {'mortems':mortems}
   return render(request, 'alls/landings.html', context)
{% load static %}
 
<html>
<head>
 <title> BLOG </title>
 <style>    </style>
 <!-- <link rel="stylesheet" href="{% static 'redactors/style.css' %}"> -->
 </head>
 <body>
   {% block content %}   
  <h1> BLOG POST:</h1>
 
 {% for mortem in mortems %}
   <h1>{{ mortem.title }}</h1>
   <h4>Date: {{ mortem.pub_date_preference }}</h4>
   <br />
   Image: <img src="{{ mortem.image.url }}" class="img-responsive center-block" style="max-height:300px;" />
   <br />
    
   <!-- Body text should go here :   -->
   Body Text:
   <p>{{ mortem.body|safe }}</p>
   <br />
   <br />
 {% endfor %}
 
{% endblock %}
 
 </body>
 
</html>
 
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('counters.urls')),
   path('', include('mortems.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django.urls import path, include
from . import views
 
 
urlpatterns = [
   path('', views.mortems, name='mortems'),
]
from django.db import models
import datetime
from django.utils import timezone
from django.utils.deprecation import MiddlewareMixin
 
# Create your models here.
 
class Mortem(models.Model):
   title = models.CharField(max_length=161)
   pub_date = models.DateTimeField()
   image = models.ImageField(upload_to='media/')
   body = models.TextField()
   now = datetime.datetime.now()
 
   def __str__(self):
       return self.title
 
   def pub_date_preference(self):
       # a = self.pub_date.timezone.now("US")
       b = self.pub_date.strftime("%A %d %B %Y @ %-I:%M:%S %p")
       # c = pytz.timezone("US")
       return (b)
 
   def summary(self):
       return self.body[:1024]

由于注释中的@MeL,解决方案是将
'mortems'
作为mortems应用程序URL.py中我的
path()
函数的第一个参数。此文件现在看起来像这样:

urlpatterns = [
   path('mortems', views.mortems, name='mortems'),
]
现在当我导航到
http://127.0.0.1:8000/mortems
landings.html
模板完全按照预期呈现,所有内容都存在。现在,这是我的尸检页面:

我想这就是解决办法。但我仍然感到困惑和好奇:为什么?使用路径参数empty
'
,我希望Django在主页/登录页的以下位置提供
landings.html
模板:
http://127.0.0.1:8000/
。Django提供模板,但缺少所有内容。仅在添加字符串作为参数后,内容才会显示。为什么呢


如果有人能回答为什么,我很乐意在这里用更多信息更新这个答案。

您在redactors.url中使用相同的路径查看views.homemortems.url中的views.mortems即'/'

CC\u Redact\u Iter3/url.py您是按此顺序配置的

urlpatterns = [
   #Django serves urls in the order you gave
   path('admin/', admin.site.urls),  #1st
   path('', include('redactors.urls')), #2nd
   path('', include('counters.urls')), #3rd
   path('', include('mortems.urls')), #4th
]
redactors/url.py

urlpatterns = [
    path('', views.home, name='home'), #here you have to change the home path
    path('alls/results', views.results, name='results'),
]
  urlpatterns = [
        path('', views.mortems, name='mortems'), #As you can see views.home is also having same path so change the path for home
    ]
mortems/url.py

urlpatterns = [
    path('', views.home, name='home'), #here you have to change the home path
    path('alls/results', views.results, name='results'),
]
  urlpatterns = [
        path('', views.mortems, name='mortems'), #As you can see views.home is also having same path so change the path for home
    ]
就你而言

def home(request):
    if 'ccEntry' in request.GET:
        number = request.GET['ccEntry']
        redacted_num = 'xxxx xxxx xxxx {}'.format(number[-4:])
        return render(request, 'alls/results.html', {'number':number, 'redacted_num':redacted_num})
    else:
        return render(request, 'alls/landings.html') #this is being rendered instead mortem
更改您的CC\u Redact\u Iter3/url.pyredactors/url.py,如下所示

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('mortems.urls')),
    path('', include('redactors.urls')),
    path('', include('counters.urls')),
] 
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

  urlpatterns = [
        path('home/', views.home, name='home'), #here you have to change the home path
        path('alls/results', views.results, name='results'),
    ]
并且您mortems/models.py中的日期格式无效(%-I)


在你的url.py中,如果你使用唯一的单词而不是全部为空(“”),会发生什么?它打印什么(Mortem.objects.all())打印什么?谢谢,@MeL!这就解决了问题。我在回答中写到了这一点,但我不确定为什么在
path()
中添加一个字符串作为第一个参数解决了我的问题。如果你能看一下我的答案并解释原因,我很乐意用你的进一步澄清更新我的答案。Django将匹配它能找到的第一个url,因此如果你将所有内容留空,它将把所有内容重定向到主页。。。或者至少翻到同一页。谢谢@MeL。如果我将
path()
参数留空,Django应该将所有内容重定向到主页,如您所说。这就是我最初想要的:主页应该是
landings.html
,但是为什么只有当
path()
参数是字符串时才缺少内容呢?谢谢@yogendra!您是对的,问题在于在我的两个URL.py中定义路径的顺序。现在一切都按计划进行。我还更正了日期/时间格式。再次感谢你,我的朋友!:)