简体   繁体   中英

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. 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. 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.

Here is my project-wide 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:

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:

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:

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:

<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:

{% 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?

So for example, I have apps: first_app, second_app, and 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 . 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 .

Im not sure how to iterate over all of my app's models to get the Article table's data. 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.

How should I change my views, urls, and html pages to do this? Thanks.

For the URL question, use the get_absolute_url functionality on the models,

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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