Python 在另一个Django应用程序中使用来自一个Django应用程序的数据
我有一个Django博客网站。它包含两个应用程序:Python 在另一个Django应用程序中使用来自一个Django应用程序的数据,python,django,Python,Django,我有一个Django博客网站。它包含两个应用程序:blog和pages 博客应用程序列出所有博客项目,如下所示: 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
blog
和pages
博客应用程序列出所有博客项目,如下所示:
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)])
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'
新闻列表.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> ·
<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 %}
我没有在页面模型
我是否可以使用博客应用程序中的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 %}
你就快到了。试试这个:
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 %}
{%extends'base.html%}
{%block title%}主页{%endblock title%}
{%block content%}
莱克兰自行车俱乐部
费尔马纳的自行车之家
{%用于新闻中的新内容%}
{{new.title}}
{%endfor%}
{%endblock内容%}
谢谢@Walucas。当我尝试这样做时,我得到一个错误:文件/Users/paulcarron/git/lakelandcc/pages/url.py”,第6行,路径(“”,HomePageView.as_view(),name='home'),
。这是my pages/urls.pyfrom django.urls导入路径from.views导入HomePageView urlpatterns=[path(“”,HomePageView.as_view(),name='home'),]
您需要从以下位置更新url:HomePageView.as_view()
到HomePageView
谢谢您的帮助。这正是我要找的。
def get_absolute_url(self):
return reverse('news_detail', args=[str(self.id)])
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)
{% 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 %}