在另一个Django应用程序中使用数据

时间:2019-04-02 10:38:15

标签: python django

我有一个Django博客网站。它包含两个应用程序:blogpages

博客应用程序列出了所有博客项目,如下所示:

models.py:

class News(models.Model):
    title = models.CharField(max_length=255)
    body = models.TextField()
    date = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(
        get_user_model(),
        on_delete=models.CASCADE,
    )
    thumb = models.ImageField(blank=True, null=True)

    def __str__(self):
        return self.title

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

views.py

class NewsListView(ListView):
    model = News
    template_name = 'news_list.html'

news_list.html

{% extends 'base.html' %}

{% block title %}News{% endblock title %}

{% block content %}
  {% for news in object_list %}
    <div class="card" style="width: 300px; display: inline-block; margin: 5px; vertical-align: top;">
       <div class="card-header">
        <span class="font-weight-bold">
          <a href="{% url 'news_detail' news.pk %}" style="color:black">{{ news.title }}</a>
        </span> &middot;
        <span class="text-muted">by {{ news.author }} | {{ news.date }}</span>
      </div>
      <div class="card-body">
        {% if news.thumb %}
          <p align="center"><img src="{{ news.thumb.url }}" /></p>
        {% endif %}
        <p>{{ news.body | linebreaks | truncatewords:30 }}
          <a href="{% url 'news_detail' news.pk %}">Full story</a></p>
      </div>
      <div class="card-footer">
        {% if user.is_authenticated %}
          <a href="{% url 'news_edit' news.pk %}">Edit</a>
          <a href="{% url 'news_delete' news.pk %}">Delete</a>
        {% endif %}
      </div>
    </div>
  {% endfor %}
{% endblock content %}

我的页面应用具有home.html:

{% extends 'base.html' %}

{% block title %}Home{% endblock title %}

{% block content %}
    <div class="jumbotron">
        <h1 class="display-4">Lakeland Cycle Club</h1>
        <p class="lead">The home of cycling in Fermanagh.</p>
        <p class="lead">
            <a class="btn btn-primary btn-lg" href="{% url 'news_list' %}" role="button">View All Club News</a>
        </p>
    </div>
{% endblock content %}

views.py:

class HomePageView(TemplateView):
    template_name = 'home.html'

页面中没有模型。

有什么方法可以使用博客应用程序中的NewsListView在主页中显示3个最新条目,还是必须创建类似的模型并在页面应用程序中进行查看才能获得博客?条目?

我尝试过:

pages / views.py

from news.models import News

class HomePageView(TemplateView):
    model = News
    template_name = 'home.html'
    queryset = News.objects.order_by('-date')[:3]

home.html

{% extends 'base.html' %}

{% block title %}Home{% endblock title %}

{% block content %}
    <div class="jumbotron">
        <h1 class="display-4">Lakeland Cycle Club</h1>
        <p class="lead">The home of cycling in Fermanagh.</p>
        <p class="lead">
            <a class="btn btn-primary btn-lg" href="{% url 'news_list' %}" role="button">View All Club News</a>
        </p>
         <span class="font-weight-bold">
          <a href="{% url 'news_detail' news.pk %}" style="color:black">{{ news.title }}</a>
        </span>
    </div>
{% endblock content %}

但是,当我尝试访问主页时,我得到了:

File "/Users/paulcarron/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/urls/resolvers.py", line 622, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'news_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['news/(?P<pk>[0-9]+)/$']

我认为它来自于我的news / models.py:

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

1 个答案:

答案 0 :(得分:1)

您快到了。试试这个:

views.py

from news.models import News
from django.shortcuts import render

def HomePageView(request):
    context = {}
    news = News.objects.order_by('-date')[:3]  
    context['news']=news 
    return render(request,'home.html',context)

home.html

    {% extends 'base.html' %}

    {% block title %}Home{% endblock title %}

    {% block content %}
        <div class="jumbotron">
            <h1 class="display-4">Lakeland Cycle Club</h1>
            <p class="lead">The home of cycling in Fermanagh.</p>
            <p class="lead">
                <a class="btn btn-primary btn-lg" href="{% url 'news_list' %}" role="button">View All Club News</a>
            </p>
             <span class="font-weight-bold">
               {%for new in news%}
{{new.title}}
{%endfor%}
            </span>
        </div>
    {% endblock content %}