添加新记录时,数据不会出现在我的html页面中-Django [英] data doesn't appear in my html page when i add new record - Django

查看:39
本文介绍了添加新记录时,数据不会出现在我的html页面中-Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从 admin面板添加新记录时,它应该出现在html页面中,但不会这样做

when i add new record from admin panel it should appear in html page , but it doesn't do that

如何修复

models.py:

models.py :

class BestArticals(models.Model):
    name = models.CharField(max_length=240)
    url = models.URLField(default="",max_length=240)
    image = models.ImageField(upload_to='images/',null=True, blank=True)

    def get_image(self):
        if self.image and hasattr(self.image, 'url'):
            return self.image.url
        else:
            return '/path/to/default/image'

    def __str__(self):
        return self.name

views.py:

from .models import BestArticals

def Best_Articals(request):
    best_posts = BestArticals.objects.all()
    context = {'best_posts' : best_posts}
    return render(request,'android/side_bar_good_posts.html',context=context)

html页面:

  {% for post in best_posts %}

  <div class="card rounded mx-auto d-block" style="width: 18rem;margin-bottom:50px;border-color:#7952b3">
    
  <div class="card-body" id="mobile_design_card">
    <a href="{{post.url}}"><p class="card-text" id="mobile_design_card_name">{{ post.name }}</p></a>
  </div>
  <a href="{{post.url}}"><img src="{{ post.get_image }}" class="card-img-top" alt="..." height="290px" width="300px"></a>

  </div>
&nbsp;&nbsp;
  {% endfor %}

  • 我没有在urls.py中添加 Best_Articals ,因为我不想为其创建url,我只想显示 side_bar_good_posts.html
    • i didn't add Best_Articals in urls.py because i don't want create url for it , i just want show data in other html page from side_bar_good_posts.html
    • 例如,我将这一行添加到其他html页面中:

      for example i add this line in other html page :

      {% include 'android/side_bar_good_posts.html' %}
      

      推荐答案

      根据您的最新评论...您应该分析 include标签查看功能的工作方式.让我尝试向我解释一下,当您请求某个URL时,它会调用特定的 view函数,并且为此原因,当您设置url时,该视图会返回经过处理的数据.包含标签仅在其中使用包含标签的文件中添加其中的所有文本,为此,当您添加 include'android/side_bar_good_posts.html'仅添加包含 android/side_bar_good_posts.html 的纯文本(html,css,js),但在这一点上,此文件不包含有关帖子的信息,因为您的变量 best_posts 为空,没有调用Best_Articals视图以返回 best_post .您需要将其他上下文(best_post)传递到模板.IE. {%include"name_snippet.html"与人=简"greeting ="Hello"%}

      Based on your last comment... You should analyze how to works include tag and view function. Let me try to explain me, when you make request to some url, it calls to specific view function and the view return the processed data for this reason when you set url it works. Include tag only add all text from in it in your file where you use include tag, for this when you add include 'android/side_bar_good_posts.html' only add plain text (html, css, js ) that contains android/side_bar_good_posts.html but in this point this file doesn't contains information about the post because your variable best_posts is empty, nothing call to your Best_Articals view to return best_post. You need to pass the additional context (best_post) to your template. I.e. {% include "name_snippet.html" with person="Jane" greeting="Hello" %}

      或者您可以使用上下文处理器,然后可以在所有模板中使用{{best_post}}.

      Or you could use context processor and then you can use {{best_post}} in all the templates.

      这篇关于添加新记录时,数据不会出现在我的html页面中-Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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