Hi,
If you tries to access values from list using slice it returns you the subset of list not a single value.The query (Excuse.objects.order_by('?')) from your view returns a list and you are trying to access values from that list using slice which in turn returns subset of list and because of it when your trying to display output in template it is displaying as list.
If you want single object use index instead of slice for example if you want 1st object from list then
outout = Excuse.objects.order_by('?')[0]
This will return single object of Excuse.
In your template if you want to display only text from Excuse then use output.<fieldname> in this case your fieldname is text so use output.text instead of just output.
<body>
<h1> {{ output.text }} </h1>
</body>
Regards.
Abhijeet Shete
Software Engineer | mquotient | Mobile +91.9860219715
On Wed, Aug 7, 2013 at 2:30 PM, Muhammed TÜFEKYAPAN <muhammedtfk@gmail.com> wrote:
It looks like this:<body><h1> {{ output }} </h1></body>
On Wednesday, August 7, 2013 6:58:17 AM UTC+3, Bulkan-Savun Evcimen wrote:Hi there,What does your template index.html look like ?Cheers
On Wed, Aug 7, 2013 at 1:19 PM, Muhammed TÜFEKYAPAN <muham...@gmail.com> wrote:Hello everyone,I write a basic model in django 1.5.1My model is looks like this:class Excuse(models.Model):text = models.CharField(max_length=300)def __unicode__(self):return self.textAnd i have a basic function to get random excuse from this model.def home(request):output = Excuse.objects.order_by('?')[:1]template = loader.get_template('index.html')context = {'output': output}return HttpResponse(output)If i write my view looks like above everything works well. I get a random excuse and write it on the page. But when i change this like this:
def home(request):output = Excuse.objects.order_by('?')[:1]template = loader.get_template('index.html')context = {'output': output}return render(request, 'index.html', context)I get output like this: [<Excuse: BlaBla>]i just want to blabla section and how i get away from [<Excuse:>]Thanks for all!To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
--
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-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
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-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment