Reputation: 11
I am using Django as a webframework for a project. I am having trouble displaying data that was saved into models.py to my html page. I was able to get something displayed by querying the db with the objects.all() function, but it is not displayed in a user-friendly manner. I am not sure how to format it. For a little background we are doing sentiment analysis with Python's textblob package. The analysis is performed in analysis.py and is called in our views.py by using the call_command(analysis) that Django provides.
Below is what we have:
analysis.py
pt_terrible = SentimentPercentage(pt_terrible = (len(terrible_list)/len(activity_text_list)))
pt_terrible.save()
pt_bad = SentimentPercentage(pt_bad = (len(bad_list)/len(activity_text_list)))
pt_bad.save()
pt_neutral = SentimentPercentage(pt_neutral = (len(neutral_list)/len(activity_text_list)))
pt_neutral.save()
pt_good = SentimentPercentage(pt_good = (len(good_list)/len(activity_text_list)))
pt_good.save()
pt_excellent = SentimentPercentage(pt_excellent = (len(excellent_list)/len(activity_text_list)))
pt_excellent.save()
models.py
class SentimentPercentage(models.Model):
pt_terrible = models.FloatField(null=True, blank=True)
pt_bad = models.FloatField(null=True, blank=True)
pt_neutral = models.FloatField(null=True, blank=True)
pt_good = models.FloatField(null=True, blank=True)
pt_excellent = models.FloatField(null=True, blank=True)
def __str__(self):
return '%f' % (self.pt_terrible)
At this point I am only trying to return the data stored in pt_terrible... however, I would like to return each value stored in these attributes eventually.
views.py
from django.http import HttpResponse
from django.http import HttpRequest
from django.shortcuts import render
from django.core.management import call_command
from sawa.models import Sentiment, SentimentPercentage
def results(request):
sentiment = call_command('analysis')
pt_sentiment = SentimentPercentage.objects.all()
context = {'pt_sentiment': pt_sentiment, 'sentiment': sentiment}
return render(request, 'sawa/results.html', context)
results.html
<form>
<fieldset>
<!-- RESULTS WILL BE POSTED HERE -->
<div>{{pt_sentiment}}</div>
<p>RESULTS WILL BE POSTED HERE</p>
</fieldset>
</form>
Here are the results I keeping getting in a link:
How do I get these results to display in a more user-friendly manner? Something along the lines of:
Percent of Terrible Reviews: 0.30% Percent of Bad Reviews: 4% .... etc.
Please let me know if you have any suggestions, I have a feeling it has something to do with the how I am querying the data, then how I am formatting it within results.html.
I have a better formatted result! But now I need to remove all of the duplicates from the list and remove items without values.. below is part of the result I am receiving in an image:
EDIT TO QUESTION
Result with for loop in results.html template
Result without for loop and just variable reference in results.html template
Upvotes: 0
Views: 186
Reputation: 25539
The display you have is as if you are printing list of SentimentPercentage
objects, which is expected.
To get rid of the square brackets []
, you need to know that it represents python list notation, so looping through the list would be the first step:
{% for item in pt_sentiment %}
{{ item }}
{% endfor %}
Secondly, <SentimentPercentage: 0.003021>
is the __unicode__
representation of your SentimentPercentage
model objects. If you need to print a percentage of your pt_terrible
or pt_bad
, you need to have methods in your model to show them. A good practice would be python property methods:
class SentimentPercentage(models.Model)
# your fields
@property
def show_pt_bad(self):
return '%f' % self.pt_bad
Then you would do:
{% for item in pt_sentiment %}
Percent of Bad Reviews: {{ item.show_pt_bad }}
{% endfor %}
Edit:
To remove duplicates you do:
pt_sentiment = SentimentPercentage.objects.all().distinct()
You should've read how to make queries in django.
Upvotes: 1