简体   繁体   English

在单个html页面上从多个Django应用程序模型获取数据

[英]Get data from multiple django app models on single html page

I have a project called Project_Name and an app called first_app with some articles in it. 我有一个名为Project_Name的项目和一个名为first_app的应用程序,其中包含一些文章。 I am displaying these article titles on my home page as links to the articles which are on the app's page. 我在首页上显示这些文章标题,作为指向该应用程序页面上文章的链接。

So at 127.0.0.1:8000/ I have index.html. 所以在127.0.0.1:8000/我有index.html。 Here I display the list of articles. 在这里,我显示文章列表。 Then if I click an article I go to 127.0.0.1:8000/first_app/1 , to display the first article for example. 然后,如果我单击文章,请转到127.0.0.1:8000/first_app/1 ,例如显示第一篇文章。

Here is my project-wide views.py: 这是我在整个项目范围内的views.py:

...
from first_app.models import Article

def home(request):
    latest_article_list = Article.objects.order_by('-pub_date')[:20]
    context = {'latest_article_list': latest_article_list}
    return render(request, 'index.html', context)

In my project-wide urls.py: 在我的整个项目范围内urls.py中:

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
        url(r'^$', 'Project_Name.views.home', name='home'),
        url(r'^admin/', include(admin.site.urls)),
        ...

Here is my models.py inside my first_app application: 这是我的first_app应用程序中的models.py:

from django.db import models
from datetime import datetime

class Article(models.Model):
            name = models.CharField(max_length=140)
            content = models.CharField(max_length=1000)
            pub_date = models.DateTimeField(default=datetime.now())

Here is my views.py inside my first_app application: 这是我的first_app应用程序中的views.py:

def article_detail(request, article_id):
    art = get_object_or_404(Article, pk=article_id)
    return render(request, 'first_app/detail.html', {'article': art})

Here is my detail.html inside my first_app templates folder: 这是我的first_app templates文件夹中的detail.html:

<h2><u>{{ article.name }}</u></h2>
<b>Published On: </b>{{article.pub_date }}
<b>Content: </b>
<ul>
    <li>{{ article.content }}</li>
</ul>

Here is my project homepage, index.html: 这是我的项目主页index.html:

{% if latest_article_list %}
<h2>Latest Articles</h2>
<ul>
    {% for article in latest_article_list %}
        <li><a href="/first_app/{{ article.id }}/">{{article.name }}</a></li>
    {% endfor %}
</ul>
{% else %}
    <p>No articles are available.</p>
{% endif %}

This is all working fine. 一切正常。

My Question: If I had two or more apps, each with their own articles (I am breaking up the articles by different apps for other reasons), how would I get those articles on the home page? 我的问题:如果我有两个或多个应用程序,每个应用程序都有自己的文章(出于其他原因,我将这些应用程序分解为不同的应用程序),那么如何在首页上获得这些文章? and how would I then build the urls so when I click an article from the home page it takes me to the correct app url? 以及如何构建URL,以便当我从主页单击文章时,可以带我到正确的应用URL?

So for example, I have apps: first_app, second_app, and third_app. 例如,我有以下应用程序:first_app,second_app和third_app。 Each app has several articles in it. 每个应用程序中都有几篇文章。 I want my home page to display all of the articles from every app. 我希望我的主页显示每个应用程序中的所有文章。 If I click on an article from first_app (say the third article posted on first_app), I am directed to url 127.0.0.1:8000/first_app/3 . 如果单击first_app上的文章(例如,发布在first_app上的第三篇文章),我将被定向到url 127.0.0.1:8000/first_app/3 Likewise, if I click on an article from the third_app (say the second article posted on third_app), I am directed to url 127.0.0.1:8000/third_app/2 . 同样,如果我单击third_app中的一篇文章(例如,第二篇文章发布在third_app中),我将被定向到URL 127.0.0.1:8000/third_app/2

Im not sure how to iterate over all of my app's models to get the Article table's data. 我不确定如何遍历我所有应用程序的模型以获取Article表的数据。 And im not sure how to generate urls to reflect the app which the articles came from. 而且我不确定如何生成网址以反映文章来源的应用。 I have tried a few things but nothing works. 我已经尝试了一些方法,但是没有任何效果。 Im stuck at this point. 我停留在这一点上。 I am pretty new to Django, so please give me some helpful comments or solutions rather then knock down my question. 我是Django的新手,所以请给我一些有用的评论或解决方案,而不要提出我的问题。

How should I change my views, urls, and html pages to do this? 我应该如何更改我的视图,URL和html页面来做到这一点? Thanks. 谢谢。

For the URL question, use the get_absolute_url functionality on the models, 对于网址问题,请在模型上使用get_absolute_url功能,

For the question about iterating all models in app, thats nothing you are ment to do in a template, you are supposed to gather the data you need in the view and present it to the template for rendering, so the answer is that you pick the models you need in your view, then send it to the template. 对于迭代应用程序中所有模型的问题,这就是您在模板中无需执行的任何操作,应该在视图中收集所需的数据并将其呈现给模板以进行渲染,因此答案是选择您在视图中需要的模型,然后将其发送到模板。

But apps are ment to be reusable components, and if your apps have a hard dependency on each other its hard to motivate them being separate apps. 但是,应将应用程序视为可重用的组件,并且,如果您的应用程序之间相互依赖,则很难激励它们成为单独的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM