【问题标题】:Django Template not being rendered correctly using Class based views没有使用基于类的视图正确呈现 Django 模板
【发布时间】:2013-05-13 07:06:27
【问题描述】:

我是 django 新手,想将 Singlely 集成到 django Polls 应用程序中。我使用基于类的视图来允许来自单个应用程序的模型与投票模型一起传递。

问题是,即使数据库中存在数据,我也无法从单一模型中获取数据。

现在我只想显示用户个人资料的 access_token 和个人资料 ID。

这是我的 Views.py 代码:(只有有问题的视图)

class IndexView(ListView):
    context_object_name='latest_poll_list'
    queryset=Poll.objects.filter(pub_date__lte=timezone.now) \
            .order_by('-pub_date')[:5]
    template_name='polls/index.html'

    def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data(**kwargs)
        context['user_profile'] = UserProfile.objects.all()
        return context

这是我的 urls.py:

urlpatterns = patterns('',
    url(r'^$',
        IndexView.as_view(),
        name='index'),
    url(r'^(?P<pk>\d+)/$',
        DetailView.as_view(
            queryset=Poll.objects.filter(pub_date__lte=timezone.now),
            model=Poll,
            template_name='polls/details.html'),
        name='detail'),
    url(r'^(?P<pk>\d+)/results/$',
        DetailView.as_view(
            queryset=Poll.objects.filter(pub_date__lte=timezone.now),
            model=Poll,
            template_name='polls/results.html'),
        name='results'),
    url(r'^(?P<poll_id>\d+)/vote/$', 'polls.views.vote', name='vote'),
)

这是我的 index.html:

{% load staticfiles %}
<h1>Polls Application</h1>

<h2>Profile Info:</h2>

    <div id="access-token-wrapper">
        <p>Here's your access token for making API calls directly: <input type="text" id="access-token" value="{{ user_profile.access_token }}" /></p>
        <p>Profiles: <input type="text" id="access-token" value="{{ user_profile.profiles }}" /></p>
    </div>

<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />

{% if latest_poll_list %}
    <ul>
    {% for poll in latest_poll_list %}
        <li><a href="{% url 'polls:detail' poll.id %}">{{ poll.question }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

它能够正确获取民意调查,但它不会在两个文本框中打印任何内容,即 user_profile.access_token 和 user_profile.profiles。

我认为问题在于模板的不正确渲染。它应该传递上下文'user_profile',但不是。或者由于某种原因它没有从数据库中获取数据,因为在 UserProfile 数据库中有一个条目。

感谢您的帮助,各位。

【问题讨论】:

    标签: django templates django-models views


    【解决方案1】:

    user_profile 上下文变量包含 UserProfile 对象的列表。来自代码:

    context['user_profile'] = UserProfile.objects.all() # will return a QuerySet, that behaves as list
    

    在模板中它被当作一个单一的对象来访问:

    {{ user_profile.access_token }}
    {{ user_profile.profiles }}
    

    因此,要么将单个 UserProfile 对象放入视图中。例如:

    if self.request.user.is_authenticated()
        context['user_profile'] = UserProfile.objects.get(user=self.request.user)
    else:
        # Do something for unregistered user
    

    要么迭代模板中的配置文件:

    {% for up in user_profile %}
        {{ up.access_token }}
    {% endfor %}
    

    通过模板中的索引访问配置文件:

    {{ user_profile.0.access_token }}
    

    【讨论】:

    • 哈哈。谢啦。那些我们忘记的小事总是会回来困扰我们。
    猜你喜欢
    • 1970-01-01
    • 2015-08-22
    • 2020-05-17
    • 1970-01-01
    • 2018-03-25
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-04
    相关资源
    最近更新 更多