亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Django 不提供文本內容(第一次嘗試)

Django 不提供文本內容(第一次嘗試)

米脂 2023-03-01 16:30:20
概括:這個特定的 Django 網絡應用程序的目的是在主頁上顯示一些 lorem ipsum 文本,如博客文章。Django 不提供我的博文內容。我知道問題出在我的 views.py 或 urls.py(或兩者)上。細節:我已經在我的 models.py 中聲明了數據。我有 views.py 來實例化模型。我遷移了 sqlite 并成功登錄到管理儀表板并輸入了一些占位符數據。我試圖讓 Django 提供我在管理儀表板中輸入的占位符內容,但它是空白的。這是我的測試用例的樣子: https: //i.imgur.com/IuOl3G4.jpg 為了描述它,您可以看到已解析的博客文章、日期、圖像和正文 HTML 標題元素,但沒有內容正在顯示。這是我的應用程序的 urls.py:from django.urls import path, includefrom . import viewsurlpatterns = [   path('', views.mortems, name='home'),]我嘗試將第一個 path() 參數的引號替換為 alls/landings. 我試過將 name 參數從交換home到mortems. 我也嘗試使用mortem(沒有s)。這些變化都沒有幫助。我也試過谷歌搜索(有變化):'正文django不顯示模板''django 不顯示文本內容'其中出現了(除其他外)SO 問題和答案哪種聲音相關但與我的問題完全不同:Django 消息框架未在模板中顯示消息為什么 django 模板沒有顯示任何輸出?這是我的應用程序的 views.py:from django.shortcuts import redirect, render, get_object_or_404from mortems.models import Mortemdef mortems(request):   mortem = Mortem.objects.order_by('-pub_date')   context = {'mortem':mortem}   return render(request, 'alls/landings.html', context)對于它的價值,以下是我模型中的相關行: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):<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 上。這是具體的“mortems”應用程序源代碼。對于有時間的慷慨 SO 用戶,我想我正在接受拉取請求。哈哈
查看完整描述

1 回答

?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

我認為您需要將 views.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') # returns an iterable queryset

    context = {'mortems':mortems}  # using plural as it's a list like object

    return render(request, 'alls/landings.html', context)

在模板代碼中,您需要遍歷列表以一次顯示一個對象。IE


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


查看完整回答
反對 回復 2023-03-01
  • 1 回答
  • 0 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號