【问题标题】:Django DetailView not displaying the content from the databaseDjango DetailView 不显示数据库中的内容
【发布时间】:2021-03-24 21:15:24
【问题描述】:

我正在尝试开发一个工作门户,人们可以在其中发布工作。我为应用程序创建了一个 Listview 和一个 DetailView。列表视图正在运行,但内容未显示在详细信息页面上。

views.py:

from django.shortcuts import render
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from .models import Application_create
# Create your views here.

class AllApps(ListView):
    model = Application_create
    template_name = 'fundamentals/allapps.html'
    ordering = ['-id']

class ApplicationInDetail(DetailView):
    model = Application_create
    template_name = 'fundamentals/application_detail.html'

urls.py:

from django.urls import path
from .views import AllApps, ApplicationInDetail

urlpatterns = [
    path('allappslist', AllApps.as_view(), name='applist'),
   path('app_in_detail/<int:pk>', ApplicationInDetail.as_view(), name='application_detail')
]

models.py:

from django.db import models
from django.urls import reverse_lazy

# Create your models here.
class Application_create(models.Model):
    application_title = models.CharField(max_length=500, default='')
    application_opening = models.DateTimeField(auto_now=True)
    application_closing = models.DateField()
    application_description = models.TextField(default='')
    company_name = models.CharField(max_length=500, default='')
    skills_req = models.CharField(max_length=2000, default='')
    job_pay = models.IntegerField(default=10000)
    freelancing_pay = models.IntegerField(default=10000)
    job_type = models.CharField(max_length=500)

    def __str__(self):
        return self.application_title + self.company_name

    def get_absolute_url(self):
        return reverse('application_detail', args = [str(self.id)])
 

base.html:

<html>
    <head>
      {% load static %}
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<link rel="stylesheet" href="{% static 'css/allappslist.css' %}">        
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    </head>
    <body>
        <nav class="navbar navbar-expand-lg navbar-light navbar_custom">
          <a class="navbar-brand" href="#">
            <img src="{% static 'images/expertoblogo.png' %}" width="40" height="40" alt="">
          </a>
            <a class="navbar-brand" href="#" >EXPERTOB</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" style="background-color: white;">
              <span class="navbar-toggler-icon"></span>
            </button>
          
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
              <ul class="navbar-nav ml-auto">
                
                <li class="nav-item" style="margin-right: 15px;">
                  <a class="nav-link" href="#" >Link</a>
                </li>
                <li class="nav-item" style="margin-right: 15px;">
                    <a class="nav-link" href="#" >Link</a>
                  </li>
                  <li class="nav-item" style="margin-right: 15px;">
                    <a class="nav-link" href="#" >Link</a>
                  </li>
                </ul>
          </nav>

           {% block content %}

           {% endblock %}

           <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
           <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>


    </body>
</html>

allapps.html:

{% extends 'fundamentals/base.html' %}

{% block content %}

{% for app in object_list %}
<ul>
    <li class="list-group-item  app_title" style="margin-left: 30px; margin-bottom: 0px; margin-right:30px; margin-top: 30px; background-color:  #007FFF;">
        
       <span style="color:white;"> {{app.application_title}} </span> 

     </li>
    

     <li class="list-group-item list-group-item-light" style="margin-left: 30px; margin-bottom: 0px; margin-right:30px;">
        
        <p>Company name: <span style="font-weight: bold;">{{app.company_name}} </span> </p>
      
      </li>


     <li class="list-group-item list-group-item-light" style="margin-left: 30px; margin-bottom: 0px; margin-right:30px;">
        
      <p>  {{app.application_description | slice:"0:200"}} .......  </p>

      <p>  <a href="{% url 'application_detail' app.pk %}"><button type="button" class="btn btn-primary">View Details</button> </a> </p>
    
    </li>
    

</ul>
{% endfor %}

{% endblock %}

application_detail.html:

{% extends 'fundamentals/base.html' %}

{% block content %}

<h1> {{app.company_name}} </h1>

{% endblock %}

我也在视图中指定了模型,但内容没有出现在 application_detail.html 文件中。我正在使用一个 mongo 数据库,该数据库正在工作,因为内容出现在 allapps.html 文件中。请帮帮我

【问题讨论】:

    标签: python-3.x django django-models django-views django-templates


    【解决方案1】:

    我认为您必须添加context_object_name,如下更改您的代码

    class ApplicationInDetail(DetailView):
        model = Application_create
        template_name = 'fundamentals/application_detail.html'
        context_object_name = 'app'
    

    【讨论】:

    • 我以前也有类似的想法,它在那里工作得很好!你能说出为什么会出现这种差异吗?
    • DetailView 类有一个方法 (link) 此方法使用您在视图中定义的 context_object_name 或使用您的模型名称,因为“app”不是您的模型名称,也没有在您的视图中定义模板中没有任何“app”变量
    【解决方案2】:

    在您的列表视图中,您有{% for app in object_list %}object_list 是 Django 对视图中对象列表的默认命名。

    在您的详细视图中,您使用app,但这应该是object,因为这是 Django 在详细视图中对对象的默认命名。

    文档在这里https://docs.djangoproject.com/en/3.1/ref/class-based-views/generic-display/

    【讨论】:

    • 或者像 Reza 回答的那样添加 context_object_name。
    猜你喜欢
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    相关资源
    最近更新 更多