Django view not working for practice project

15 views
Skip to first unread message

NorDe

unread,
Jan 16, 2018, 11:03:45 PM1/16/18
to Django users

Context:

I am creating a website to house some webcomics I made as a project to practice Django. I am adapting Django's tutorial to create the site (https://docs.djangoproject.com/en/2.0/intro/tutorial03/ About halfway down the page under "Write views that actually do something"). I am having some difficulty getting part of my view to work as expected.

Expectation:

What I see when I go to http://127.0.0.1:8000/futureFleet/ : latest_comic

What I want to see: A dictionary of my 2 comics.

Question:

I think I am doing something wrong at this line

context = {'latest_comic': latest_comic}. I am adapting this line from the tutorial. I think the line needs to be run to connect to the template. What do I do? What am I missing?

Models.py

class Comic(models.Model):
    #title
    comic_title_text = models.CharField(max_length=200)
    #date
    comic_pub_date = models.DateTimeField('comic date published')
    #image
    comic_location = models.CharField(max_length=200)
    #explanation
    comic_explanation_text = models.CharField(max_length=400, blank=True)

    def __str__(self):
        return self.comic_title_text

    def was_published_recently(self):
        return self.comic_pub_date >= timezone.now() - datetime.timedelta(days=1)

views.py

  def index(request):
    latest_comic = Comic.objects.order_by('-comic_pub_date')[:2]
    context = {'latest_comic': latest_comic}
    return HttpResponse(context)
    # return render(request, 'futureFleet/index.html', context) This sends to the template but doesn’t work at the moment

Database

"Welcome Aboard" "2018-01-15 21:02:54" "/home/user/Desktop/django/djangoFutureFleet/mysite/futureFleet/static/futureFleet/images/1.JPG" "this is the first comic"

"Space Vaccine" "2018-01-15 23:02:22" "/home/user/Desktop/django/djangoFutureFleet/mysite/futureFleet/static/futureFleet/images/2.JPG" "This is comic 2"

Jani Tiainen

unread,
Jan 16, 2018, 11:40:23 PM1/16/18
to django...@googlegroups.com
Hi.

Could you show your template also?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/31674ff4-a2ab-4f2d-b6cb-9fc7ca9cf9cc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matemática A3K

unread,
Jan 17, 2018, 12:56:11 AM1/17/18
to django...@googlegroups.com
views.py
  def index(request):
    latest_comic = Comic.objects.order_by('-comic_pub_date')[:2]
This has an implicit .all(), it's the same than doing
Comic.objects.all().order_by('-comic_pub_date')[:2]
Then you are slicing it for the first 2 records, that's why you see 2 records. Use .first() instead the slicing to get only one record (or slice 1, [:1] or [0]). Your template code also support several elements, you should check it
Reply all
Reply to author
Forward
0 new messages