简体   繁体   中英

How can I create a webpage with Django that displays two different queries?

For my Django project I decided to make a mock news website. I created a class for the various news articles. I made one of the variables “type” to designate whether the article was a news story or a podcast. I then created two templates: news.html and podcasts.html. Then, in views.py, I created a function that returns only the news stories for news.html and another function that returns only the podcasts for podcasts.html.

def allArticles(request):
    articles = Article.objects.filter(type="news").order_by("-date")
    return render(request, 'article/allArticles.html', {'articles': articles})

def podcasts(request):
    articles = Article.objects.filter(type="pod").order_by("-date")
    return render(request, 'article/podcasts.html', {'articles': articles})

I am trying to create a third page with two sections, one that displays news stories and the other will display podcasts. I cannot just call the entire database because it will not be separated into the proper sections and there are other types of articles besides news stories and podcasts.
I tried combining the code of news.html and podcasts.html into one html file, however it will only return one of the two. I also tried using snippets, it returned just the news articles twice. I feel like the solution is somewhere in the URL patters. Its like each path will only trigger one view so if I try to add another it is simply ignored.

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.allArticles, name='allArticles'),
    path('<int:blog_id>/', views.articlePage, name='articlePage'),
    path('contributors/', views.contributors, name='contributors'),
    path('podcasts/', views.podcasts, name='podcasts'),
    path('<int:blog_id>/', views.podcastPage, name='podcastPage'),
]

Well, I thought typing this out might help me figure out what is going but alas I still need help.
Ultimately, what I am trying to figure out is how can I create a webpage with Django that displays two different queries from the same database. I think I could restructure the models to make this easier, but I am trying to work with what I have. I am really just looking for guidance or someone to point me to the documentation that might address these issues.
Thanks in advance.

Instead of having 2 functions, each with one variable; I needed one function with two variables. I added a variable called podcasts to the allArticles function and passed it into the context.

def allArticles(request):
    articles = Article.objects.filter(type="news").order_by("-date")
    podcasts = Article.objects.filter(type="news").order_by("-date")

    return render(request, 'article/allArticles.html', {'articles': articles, 'podcasts ': podcasts})

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