在另一个Django应用程序中使用数据 [英] Using data from one Django application in another

查看:61
本文介绍了在另一个Django应用程序中使用数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django博客网站.它包含两个应用程序: blog pages .

I have a Django blog site. It contains two applications: blog and pages.

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

The blog app lists all blog items as follows:

models.py:

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

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:

My pages app has 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个最新条目,还是必须创建类似的模型并在页面应用程序中进行查看才能得到博客条目?

Is there any way I can use the NewsListView from my blog application to display the 3 most recent entries in my home page or must I create a similar model and view in my pages application in order to get the blog entries?

我尝试过:

pages/views.py

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

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

但是,当我尝试访问主页时,得到以下信息:

However, whenI try to access the home page I get this:

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:

I think it comes from having this in my news/models.py:

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

推荐答案

您快到了.试试这个:

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

这篇关于在另一个Django应用程序中使用数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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