简体   繁体   中英

Django Python - Displaying data from models.py to html page … error too many values to unpack

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. 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()

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)

views.py

def results(request):
    sentiment = call_command('analysis')
    terrible_sentiment = SentimentPercentage.objects.get('**pt_terrible')
    context = {'sentiment': sentiment, 'terrible_sentiment': terrible_sentiment}
    return render(request, 'sawa/results.html', context)

results.html

<section>
            <form>
                <fieldset>

                    <!-- RESULTS WILL BE POSTED HERE -->

                    <p>{{terrible_sentiment.pt_terrible}}</p>

                    <p>RESULTS WILL BE POSTED HERE</p>
                </fieldset>
            </form>
        </section>

We are receiving the error "too many values to unpack (expected 2)"... Below is the Traceback:

Environment:

Request Method: GET
Request URL: http://localhost:8000/sawa/results/

Django Version: 1.9.5
Python Version: 3.4.3
Installed Applications:
['sawa.apps.SawaConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response 149.response = self.process_exception_by_middleware(e, request)

File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response 147.response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\...\Documents\Spring Semester 2016\ISA 406\Project\Django Project\isa406\sawa\views.py" in results 17.terrible_sentiment = SentimentPercentage.objects.get('**pt_terrible')

File "C:\Python34\lib\site-packages\django\db\models\manager.py" in manager_method 122.return getattr(self.get_queryset(), name)(*args, **kwargs)

File "C:\Python34\lib\site-packages\django\db\models\query.py" in get 378.clone = self.filter(*args, **kwargs)

File "C:\Python34\lib\site-packages\django\db\models\query.py" in filter 790.return self._filter_or_exclude(False, *args, **kwargs)

File "C:\Python34\lib\site-packages\django\db\models\query.py" in _filter_or_exclude 808.clone.query.add_q(Q(*args, **kwargs))

File "C:\Python34\lib\site-packages\django\db\models\sql\query.py" in add_q 1243.clause, _ = self._add_q(q_object, self.used_aliases)

File "C:\Python34\lib\site-packages\django\db\models\sql\query.py" in _add_q

We have tried a bunch of things with the views.py to get it working with no luck. If you have any suggestions, we would very much appreciate it.

You haven't provided the traceback which would enable us to easily diagnose the problem, but this line looks suspicious:

 terrible_sentiment = SentimentPercentage.objects.get('**pt_terrible')

The argument should not be in quotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM