Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chartit is not a valid tag library:Django

My views.py file is as follows:

from django.shortcuts import render, render_to_response
from chartit import DataPool, Chart
from chartit.chartdata import DataPool
from weather.models import MonthlyWeatherByCity
import simplejson
from chartit import DataPool, Chart

def weather_chart_view(request):
    ds=DataPool(series=[{'options': {'source': MonthlyWeatherByCity.objects.all()},'terms': ['month','houston_temp','boston_temp']}])    
    cht = Chart(datasource = ds, series_options =[{'options':{'type': 'line','stacking': False},'terms':{'month': ['boston_temp','houston_temp']}}],chart_options ={'title': {'text': 'Weather Data of Boston and Houston'},'xAxis': {'title': {'text': 'Month number'}}})
    return render_to_response('chart.html',{'weatherchart': cht})

The urls.py file inside the app is as follows:

from django.conf.urls import include, url
from django.contrib import admin
from weather import views

urlpatterns = [
    url(r'^$', views.weather_chart_view , name='weather_chart_view')
]

The models.py file is as follows:

from django.db import models

class MonthlyWeatherByCity(models.Model):
    month = models.IntegerField()
    boston_temp = models.DecimalField(max_digits=5, decimal_places=1)
    houston_temp = models.DecimalField(max_digits=5, decimal_places=1)

The chart.html file is as follows:

<head>
    <!-- code to include the highcharts and jQuery libraries goes here -->
    <!-- load_charts filter takes a comma-separated list of id's where -->
    <!-- the charts need to be rendered to                             -->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <script src="http://code.highcharts.com/highcharts.js" type="text/javascript"></script>
    <script src="/highcharts.js" type="text/javascript"></script>
    {% load chartit %}
    {{ weatherchart|load_charts:"container" }}
</head>
<body>
    <div id='container'> {{ weatherchart|load_charts:"container" }}</div>
</body>

On running the server and opening the app I am getting the error:

TemplateSyntaxError at /weather/
'chartit' is not a valid tag library: ImportError raised loading chartit.templatetags.chartit: cannot import name simplejson

I have also included the app, chartit and json in the INSTALLED_APPS.

As you can see I have also imported simplejson in views. Where am I going wrong?

Please suggest if I need to post anything else to make the problem lucid.

like image 912
praxmon Avatar asked May 09 '14 12:05

praxmon


3 Answers

There is a fix for the problem on the github page of the project. Do a pip install simplejson and then locate the file chartit/templatetags/chartit.py in the chartit module and replace the line of simplejson import as shown below.

from django import template
-from django.utils import simplejson
+import simplejson
from django.utils.safestring import mark_safe

It looks like a hack but works till the fix is merged.

like image 154
akoshodi Avatar answered Sep 25 '22 16:09

akoshodi


  1. You need to add chartit to INSTALLED_APPS in "settings.py"

    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'chartit',
    )
    
  2. Then, follow @akoshodi 's response.

like image 24
Miae Kim Avatar answered Sep 23 '22 16:09

Miae Kim


In latest version of chartit (0.2.8) issue is fixed, so the only thing that you should do is to add 'chartit' in INSTALLED_APPS exactly as @MiaeKim mentions.

like image 23
alexopoulos7 Avatar answered Sep 21 '22 16:09

alexopoulos7