0

I'm working on an app called Start the dark from the tutorial django from ground up. I have just started on the tutorial and I have difficulty displaying the result on html.

The app is suppose to display the description of the event . The creator of the event and the time it was posted and when I type the link . It's display nothing.

I spent few hours troubleshooting and so far I learn't the problem is not my html form either it's my URL cause I did some test. I think it's getting the results from the models.py into my views.py and executing it

My models.py are :

from django.db import models
from datetime import datetime, timedelta
from django.contrib.auth.models import User
from django.db.models.query import QuerySet


 class Event(models.Model):
    description = models.TextField()
    creation_date = models.DateTimeField(default = datetime.now)
    start_date = models.DateTimeField(null=True, blank= True)
    creator = models.ForeignKey(User , related_name = 'event_creator_set')
    attendees = models.ManyToManyField(User , through = "Attendance")
    latest = models.BooleanField(default=True)
    def __unicode__(self):
        return self.description
    def save(self, **kwargs):
        now = datetime.now()
        start = datetime.min.replace(year = now.year , month=now.month,day=now.day)
        end = (start + timedelta(days=1)) - timedelta.resolution
        Event.objects.filter(latest=True, creator=self.creator).filter(creation_date__range=(start,end)).update(latest=False)
    super(Event,self).save(**kwargs)

class Attendance(models.Model):
    user = models.ForeignKey(User)
    event = models.ForeignKey(Event)
    registration_date = models.DateTimeField(default=datetime.now)


    def __unicode__(self):
        return "%s is attending %s" %(self.user.username,self.event)

My views.py

 from events.models import Event
from django.shortcuts import render_to_response
from django.template import RequestContext

def tonight(request):
    events = Event.objects.filter(latest=True)
    return render_to_response('events/tonight.html',{'events':events},context_instance = RequestContext(request))

My tonight.html

 {% if events %}
 <ul>
{{events.description}}
{{events.start_date}}
{{events.creator}}
 </ul>
 {% endif %}

My events URL.CONF

 from django.conf.urls.defaults import *
 from django.contrib import admin
 from events import views
 urlpatterns = patterns('events.views',
         url(r'^tonight/$','tonight',name='ev_tonight')
 )

My main URLconf

  from django.conf.urls import patterns, include, url
  from django.contrib import admin
  from django.conf import settings
  from django.contrib.staticfiles.urls import staticfiles_urlpatterns
  from django.conf.urls.static import static
  # Uncomment the next two lines to enable the admin:
  # from django.contrib import admin
  admin.autodiscover()
  urlpatterns = patterns('',

      url(r'^events/',include('events.urls')),



  ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

  urlpatterns += staticfiles_urlpatterns()

Some other models.

 from django.db import models
 from django.contrib import admin
 class student(models.Model):
         First_name = models.CharField(max_length=30)
         Last_name = models.CharField(max_length=30)
     Age = models.BigIntegerField()
     Body = models.TextField()

     def __unicode__(self):
        return self.First_name
4
  • your def unicode in your views must be in the models Commented Feb 28, 2013 at 13:03
  • oh okay. I see that mistake. thank you @catherine Commented Feb 28, 2013 at 13:20
  • @catherine I'll send you my app soon and you can help me with it when your free . You said you were going to make a social auth? what do you mean by social auth? Thank you for helping me :] Commented Mar 21, 2013 at 13:31
  • login/register via facebook, twitter, google, and more Commented Mar 21, 2013 at 13:33

1 Answer 1

1

Here events is a queryset. Refer django docs on querysets. You need to iterate over the objects in it. Edit your template code like this;

{% if events %}
    {% for each_event in events %}
        <ul>
            {{each_event.description}}
            {{each_event.start_date}}
            {{each_event.creator}}
        </ul>
    {% endfor %}
{% endif %}
Sign up to request clarification or add additional context in comments.

4 Comments

Because it's a queryset . I need to alway iterate over it?@arulmr
Yes. Whenever a queryset is passed to the template, it will iterate to get the objects in it.
I posted another model.py at the bottom. Is that also an queryset?
Yes. Refer the link in my answer. Every list of objects fetched using .filter() or .all() is a queryset.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.