Django Template not being rendered correctly using Class based views -


i'm django newbie , wanted integrate singly django polls application. have used class based views allow models singly app passed along polls models.

the problem is, i'm unable data singly model when data present inside database.

for want display access_token , profile id of user profile.

here views.py code: (only view in question)

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 

this 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'), ) 

and here index.html:

{% load staticfiles %} <h1>polls application</h1>  <h2>profile info:</h2>      <div id="access-token-wrapper">         <p>here's access token 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>     {% poll in latest_poll_list %}         <li><a href="{% url 'polls:detail' poll.id %}">{{ poll.question }}</a></li>     {% endfor %}     </ul> {% else %}     <p>no polls available.</p> {% endif %} 

its able fetch polls correctly doesn't print in either textboxes i.e. user_profile.access_token , user_profile.profiles.

i think problem incorrect rendering of template. should pass context 'user_profile' not. or reason not taking data database, because there entry in userprofile database.

i grateful help, people.

the user_profile context variable contains list of userprofile objects. code:

context['user_profile'] = userprofile.objects.all() # return queryset, behaves list 

and in template accessed if single object:

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

so either put variable single userprofile object in view. example:

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

either iterate on profiles in template:

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

either access profile index in template:

{{ user_profile.0.access_token }} 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -