Py学习  »  Django

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

runnerpaul • 5 年前 • 714 次点击  

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

博客应用程序列出了以下所有博客项目:

模型:

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)])

VIEW

class NewsListView(ListView):
    model = News
    template_name = '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 %}

查看:

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

我在书页上没有模型。

我能用什么方法吗 NewsListView 从“我的博客”应用程序显示主页中的3个最新条目,还是必须在“我的网页”应用程序中创建类似的模型和视图才能获取博客条目?

我试过这个:

页面/视图

from news.models import News

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

首页

{% 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)])
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/31778
 
714 次点击  
文章 [ 1 ]  |  最新文章 5 年前