Django不提供文字内容(首次尝试) [英] Django not serving text content (First attempt)

查看:40
本文介绍了Django不提供文字内容(首次尝试)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要:

此特定Django Web应用程序的目的是仅在主页上显示一些lorem ipsum文本,例如博客文章.Django不提供我的博客文章内容.我知道问题出在我的views.py或urls.py(或两者都有).

The purpose of this particular Django web app is to just show some lorem ipsum text on the home page, like a blog post. Django is not serving my blog post content. I know the problem is either with my views.py or urls.py (or both).

详细信息:

我已经在我的models.py中声明了数据.我有views.py可以实例化模型.我迁移了sqlite,并成功登录到管理控制台,并输入了一些占位符数据.

I’ve got the data declared inside my models.py. I’ve got my views.py to instantiate the model. I migrated sqlite and successfully logged into the Admin Dashboard and entered some placeholder data.

我正试图让Django提供我在管理控制台中输入的占位符内容,但它是空白.

I’m trying to get Django to serve the placeholder content that I entered into the Admin Dashboard, but instead it’s blank.

这是我的测试用例: https://i.imgur.com/IuOl3G4.jpg 要对其进行描述,您可以看到已解析博客文章",日期",图像"和正文" HTML标题元素,但没有显示任何内容.

Here is my what my test case looks like: https://i.imgur.com/IuOl3G4.jpg To describe it, you can see The Blog Post, Date, Image, and Body Text HTML heading elements parsed, but none of the content is showing.

这是我的应用的urls.py:

Here is my app’s urls.py:

from django.urls import path, include
from . import views


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

我尝试用 alls/landings 替换第一个path()参数的引号.我尝试将name参数从 home 交换为 mortems .我还尝试使用 mortem (没有 s ).这些更改都无济于事.

I’ve tried swapping out the quotation marks for the first path() parameter with alls/landings. I’ve tried swapping the name parameter from home to mortems. I also tried using mortem (without the s). None of these changes help.

我还尝试了谷歌搜索(有变体):

I've also tried Googling (with variations):

  • 正文django未显示模板"
  • 'django不显示文本内容'

(其中包括)与哪种声音相关但与我的问题完全不同的SO问题和答案:

Which turned up (among other) SO questions and answers which kind of sound related but are completely different from my issue:

这是我的应用的views.py:

Here is my app’s views.py:

from django.shortcuts import redirect, render, get_object_or_404
from mortems.models import Mortem

def mortems(request):
   mortem = Mortem.objects.order_by('-pub_date')
   context = {'mortem':mortem}
   return render(request, 'alls/landings.html', context)

关于它的价值,这是我的模型中的相关行:

For what it is worth, here are the relevant lines in my model:

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()

另外,这是我的模板,上面有相关的问题代码行(42-50):

Also, here is my template with the relevant problematic lines (42-50):

<h1> BLOG POST:</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>

完整的源代码在GitHub上.这里是验尸"应用程序源代码.

The full source code is up on GitHub. Here is the 'mortems' app source code specifically.

对于拥有大量时间的大量SO用户,我想我正在接受请求请求.哈哈

For generous SO users with time on their hands, I guess I’m accepting pull requests. haha

推荐答案

我认为您需要将views.py更新为:

I think you need to update the views.py as:

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') # returns an iterable queryset
    context = {'mortems':mortems}  # using plural as it's a list like object
    return render(request, 'alls/landings.html', context)

在模板代码中,您需要遍历列表以一次显示一个对象.即

In the template code, you need to iterate over the list to display a single object one at a time. i.e

<h1> BLOG POSTs:</h1>
{% for moertm in mortems} 
  <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>
{% endfor %}

这篇关于Django不提供文字内容(首次尝试)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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