Re: Django view not working for practice project

2018-01-16 Thread Matemática A3K
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


> 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
>
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/CA%2BFDnh%2B6EWrryQE47qGAPZbvwEF0Zjt5oAS_4aukQQBe%2B8FzKg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django view not working for practice project

2018-01-16 Thread Jani Tiainen
Hi.

Could you show your template also?

17.1.2018 6.03 "NorDe"  kirjoitti:

> 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"
>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@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.
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/CAHn91ofS3m0FNZTcUWBBZnZtO%3DeqS4pb66mn%3DLDnhtMQV_mQdQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Django view not working for practice project

2018-01-16 Thread NorDe
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"

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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.