Django - 显示从SQLite数据库,由窗体访问的图 [英] Django - Show plot from SQLite database, accessed by form
问题描述
所以我有一个使用Django编写的页面,有人可以将信息输入到表单中,并且提交它访问SQLite数据库以检索查询的信息。到目前为止,我已经得到了这么远,但是我无法在页面的形式下创建和显示一个情节。
So I have a page written using Django where someone can enter information into a form, and on submit it accesses a SQLite database to retrieve the queried information. So far I have gotten this far, but I am not able to create and show a plot below the form on the page.
我尝试过不同的软件包: ChartIt
, Graphos
和 nvd3
。无论哪种方式,我都会遇到错误,可能是因为我不了解Django中编码的全部细节。
I have tried different packages: ChartIt
, Graphos
and nvd3
. Either way I get an error, likely because I don't understand the full details of the coding in Django.
下面是我尝试用nvd3创建剧情。当我尝试加载页面时,我收到错误:
Below is my try at creating a plot with nvd3. When I try to load the page I get the error:
Exception Value: Invalid block tag: 'load_chart', expected 'elif', 'else' or 'endif'
在我的viewrun.html中,我假设我加载在nvd3中:
at the line in my viewrun.html where I assume I load in the nvd3:
{% load_chart charttype chartdata "linewithfocuschart_container" True "%d %b %Y %H" %}
这是我使用的文件。感谢您的帮助!
Here are the files I use. Thank you for any help!
views.py
def createplot(molecules, chemrun_id, database='laminar_idl.test.db'):
conn, status_db = conn_to_db(database)
if status_db and not molecules==None:
time, x, y, abunds, status_fetch = get_one_species_all_times(conn, str(molecules))
chartdata = {'x': time,
'name1': molecules, 'y1': abunds}
charttype = "lineWithFocusChart"
data = {
'charttype': charttype,
'chartdata': chartdata
}
return data
return None
@login_required
def run(request, chemrun_id=1, molecule=None):
if request.POST:
form = MolChoiceForm(request.POST)
if form.is_valid():
form.save()
molecule = form.cleaned_data['molecule']
return HttpResponseRedirect('/chemrun/run/'+chemrun_id+'/'+molecule+'/')
else:
form = MolChoiceForm()
args = {}
args.update(csrf(request))
args['form'] = form
args['chemrun'] = ChemRun.objects.get(id=chemrun_id)
if not molecule is None:
args['molecule'] = molecule
plotdata = createplot(molecule, chemrun_id)
args['plotdata'] = plotdata
return render_to_response('interface/viewrun.html', args, context_instance=RequestContext(request))
viewrun.html:
viewrun.html:
{% extends "interface/base.html" %}
{% block content %}
<article>
{% if user.is_authenticated %}
<header>
<p><font size="+2">Chemical run: <i>{{run.title}}</i> (<b>ID #{{chemrun.id}}</b>)</font></p>
{% load_chart charttype chartdata "linewithfocuschart_container" True "%d %b %Y %H" %}
</header>
<section>
<p>Outputs written for {{chemrun.n_times}} time steps between {{chemrun.times_min}} and {{chemrun.times_max}} years</p>
<p>Temperature: <b>{{chemrun.temperature}}</p>
<p>Density: <b>{{chemrun.density}}</p>
<hr>
<form action='/chemrun/run/{{chemrun.id}}/' method='post' style="display: inline-block;">{% csrf_token %}
{{form}}
<input type="submit" name="submit" value="Plot">
</form>
</section>
{% else %}
<h2>No data available because you are not logged in.
{% endif %}
{% endblock %}
base.html:
base.html:
{% load staticfiles %}
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<link rel="stylesheet" href="{% static 'css/normalize.min.css' %}">
<link rel="stylesheet" href="{% static 'css/main.css' %}">
<link media="all" href="{% static 'nvd3/src/nv.d3.css' %}" type="text/css" rel="stylesheet" />
<script type="text/javascript" src='{% static 'd3/d3.min.js' %}'></script>
<script type="text/javascript" src='{% static 'nvd3/nv.d3.min.js' %}'></script>
<!--<script src="js/vendor/modernizr-2.8.3-respond-1.4.2.min.js"></script>-->
<title>Chemicalizer</title>
</head>
<body>
<div class="header-container">
<header class="wrapper clearfix">
<h1 class="title"><b><a href='/'>Chemicalizer</a></b> </h1>
<!--<h2 class="subtitle">- You need it!</h2>-->
<nav>
<ul>
{% if user.is_authenticated %}
{% if user.is_superuser %}
<li><a href="/admin/">Admin</a></li>
<li><a href="/chemrun/all/">All Models</a></li>
{% else %}
<li><a href="/chemrun/create/">Start model</a></li>
<li><a href="/chemrun/all/">My Models</a></li>
{% endif %}
<li><a href="/accounts/logout/">Logout</a></li>
{% else %}
<li><a href="/accounts/login/">Login</a></li>
<li><a href="/accounts/register/">Register</a></li>
<li><a href="/contact/">Contact</a></li>
{% endif %}
</ul>
</nav>
</header>
</div>
<div class="main-container">
<div class="main wrapper clearfix">
{% block content %}
{% endblock %}
</div> <!-- #main -->
</div> <!-- #main-container -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.11.2.min.js"><\/script>')</script>
<!--<script src="js/main.js"></script>-->
</body>
</html>
推荐答案
对于未来的人来说,同样的问题,我正在使用没有解释使nvd3工作的所有步骤的页面(或者他们可能已经过时)。
For people in the future fighting the same problem, I was using pages that did not explain all steps for getting nvd3 to work (or they were maybe outdated).
以下是安装它并使其正常工作的步骤:
Here are the steps to follow to install it and get it working:
1)安装django-nvd3使用 pip install django-nvd3
1) Install django-nvd3 using pip install django-nvd3
2)由于这依赖于python-nvd3,我们需要安装bower使用 npm install -g bower
。 (如果你没有npm,只需用macports或任何其他方式安装它。)
2) Since this is dependent on python-nvd3, we need to install bower using npm install -g bower
. (If you don't have npm, just install it with macports, or any other way you like.)
3)接下来,使用命令
3) Next, install django-bower with the command pip install django-bower
4)然后运行 bower install nvd3
还将安装依赖关系 d3
4) Then run bower install nvd3
which will also install the dependency d3
5)将您的view.py编辑为这样的东西,示例是forlineChart:
5) Edit your view.py to something like this, this example is for a lineChart:
charttype = "lineChart"
chartcontainer = 'linechart_container' # container name
data = {
'charttype': charttype,
'chartdata': chartdata,
'chartcontainer': chartcontainer,
'extra': {
'x_is_date': False,
'x_axis_format': '',
'tag_script_js': True,
'jquery_on_ready': True,
'chart_attr': {
'xScale':'(d3.scale.log())', #for logscale on x-axis
'yScale':'(d3.scale.log())', #for logscale on y-axis
'xAxis.axisLabel':'"Time, yrs"',
'yAxis.axisLabel':'"n(X)/n(H)"',
}
}
}
return data
6)使用以下内容更新您的settings.py:
6) Update your settings.py with the following:
BOWER_COMPONENTS_ROOT = BASE_DIR
BOWER_PATH = '/usr/local/bin/bower'
BOWER_INSTALLED_APPS = (
'd3',
'nvd3',
)
,并将'djangobower.finders.BowerFinder',
添加到您的 STATICFILES_FINDERS =(
7)现在你的html代码必须包含这些(例如头):
7) Now your html code has to include these (in the head for example):
<link media="all" href="{% static 'nvd3/src/nv.d3.css' %}" type="text/css" rel="stylesheet" />
<script type="text/javascript" src='{% static 'd3/d3.min.js' %}'></script>
<script type="text/javascript" src='{% static 'nvd3/nv.d3.min.js' %}'></script>
在主体的某个地方:
{% include_chart_jscss %}
{% load_chart charttype chartdata chartcontainer extra %}
最后,无论你想要的情节如何:
Finally, for wherever you want the plot to appear:
{% include_container chartcontainer %}
这对我有用。如果有人更了解这个错误,请帮我纠正错误:)
This worked for me. If anyone more knowledgeable about this finds any mistakes, please help me correct them :)
这篇关于Django - 显示从SQLite数据库,由窗体访问的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!