Django动态主页

时间:2015-07-30 05:00:47

标签: python django

我有很多应用,但我想在我的主页上生成所有应用的数据。这是我的应用:博客,成员

我想在我的主页上显示博客和我的会员 我知道如果我想生成一个应用程序的数据,我可以这样做: 博客/ urls.py:

urlpatterns = [
url(r'^$', ListView.as_view(
    queryset=Post.objects.all().order_by("-date")[:10],
    template_name='blog1.html')),

并在blog1.html中:

{% extends "index.html" %}
{% block blog %}
{% for post in blog_post %}
<div id="blog-wrapper" class="bgrid-third s-bgrid-half mob-bgrid-whole     group">
<article class="bgrid">
            <h5>{{ post.date }} </h5>
               <h3><a href="/{{ post.id }}"><div class = "entry-title">{{ post.title }}</div></a></h3>
                <p>{{ post.user }}</p>
               <p><div class = "post_body">{{ post.body|safe|linebreaks }}</div></p>
            </article>
</div>
{% endfor %}
{% endblock %}

{% block member %}

在这里,当我访问网址时,我可以看到我写的所有博客,但现在我想在一个页面上看到博客和成员(另一个应用),那么我该怎么做呢?

2 个答案:

答案 0 :(得分:0)

我建议您使用ListViewget_context_data方法。

在项目的views.py

中创建视图
from django.views.generic import ListView

# Import your models here.


class HomepageView(ListView):
    model = Post
    ordering = '-date'
    template_name = 'blog1.html'
    context_object_name = 'posts'

    def get_context_data(self, **kwargs):
        context = super(HomepageView, self).get_context_data(**kwargs)
        context['members'] = Member.objects.all()
        return context

    def get_queryset(self):
        return super(HomepageView, self).get_queryset()[:10]

然后,更改urls.py

from django.conf.urls import url

# Import ``HomepageView`` here.


urlpatterns = [
    url(r'^$', HomepageView.as_view(), name='homepage'),
    # Other patterns here.
]

现在,您可以使用posts变量和使用members变量的成员来访问帖子。

答案 1 :(得分:0)

from .models import Post
from member.models import UserProfile


def get_data():
return {
    "post": Post.objects.all().order_by("-date")[:10],
    "user": UserProfile.objects.all()[:10]
}

然后在我的blog1.html:

{% extends "index.html" %}
{% block blog %}
{% for post in object_list.post %}
<div id="blog-wrapper" class="bgrid-third s-bgrid-half mob-bgrid-whole group">
            <article class="bgrid">
            <h5>{{ post.date }} </h5>
               <h3><a href="/{{ post.id }}"><div class = "entry-title">{{ post.title }}</div></a></h3>
                <p>{{ post.user }}</p>
               <p><div class = "post_body">{{ post.body|safe|linebreaks }}</div></p>
            </article>
</div>
            {% endfor %}


{% endblock %}

{% block member %}
{% for post in object_list.user  %}
                <div class="bgrid member">

                   <div class="member-header">

                        <div class="member-pic">
                                <img src="{{ post.portrait }}" alt=""/>
                        </div>

                      <div class="member-name">
                         <h3>{{ post.user }}</h3>
                         <span>Creative Director</span>
                      </div>

                   </div>

                   <p>{{ post.intro }}</p>

                   <ul class="member-social">
                      <li><a href="{{ post.github }}"><i class="fa fa-google-plus"></i></a></li>
                       <li><a href="{{ post.google_plus }}"><i class="fa fa-github"></i></a></li>
                   </ul>

                </div> <!-- /member -->
{% endfor %}
{% endblock %}