使用Dygraph时Django的奇怪行为

时间:2018-10-28 18:03:47

标签: javascript python django dygraphs

我有一个用django编写的服务器,我想在模板上使用dygraph显示一些图形。我编写了一个视图及其关联的url,还将一些变量通过上下文传递给模板。使用这些上下文变量,我想显示图形。但问题是,该图未显示,并且网址中有异常行为。我在下面发布模板,URL和查看代码。

@login_required
def profile(request, mid):
    user = request.user
    context = {}

    delta_T = 1000
    data = subprocess.check_output("tail -n {0} {1}".format(
                                    delta_T, filename
                                    ), 
                                    shell=True)
    data = data.split("\n".encode())

    heatcsv = ""
    fancsv = ""
    tempcsv = ""

    plot = []
    for t in range(len(data)):
        line = data[t].decode("utf-8")
        entry = line.strip().split(" ")
        try:
            plot.append([int(float(i)) for i in entry[0:-1] \
                                                + [float(entry[-1])]])
            heatcsv += "{0},{1}".format(t+1, entry[1])
            fancsv += "{0},{1}".format(t+1,entry[2])
            tempcsv += "{0},{1}".format(t+1, entry[3])
        except:
            continue

    plot = zip(*plot)
    context["mid"] = mid
    context["delta_T"] = delta_T
    context["heat"] = heatcsv
    context["fan"] = fancsv
    context["temp"] = tempcsv        

    return render(request,"dashboard/profile.html",context)

模板代码:

{% load staticfiles %}
{% block title %}
    Dashboard - Profile
{% endblock %}
{% block headerfiles %}

        <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js" type="text/javascript"></script>
        <script type="{% static 'js/jquery-ui.js' %}"></script> 
        <link rel="stylesheet" type="text/css" href="{% static 'css/jquery.datetimepicker.css' %}"/>
        <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}"/>
        <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/dygraph/2.1.0/dygraph.min.css" />
        <script src="//cdnjs.cloudflare.com/ajax/libs/dygraph/2.1.0/dygraph.min.js"></script>
{% endblock %}
{% block main %}
    <div class = "row">
        <div class = "span12">
            <h2>{{ mid }} <small>Profile for past {{ delta_T }} instances</small></h2>
            <h3>Heat</h3>
            <div id="heat" style="width:800px; height: 80px"></div>
            <h3>Fan</h3>
            <div id="fan" style="width:800px; height: 80px"></div>
            <h3>Temperature</h3>
            <div id="temp" style="width:800px; height: 80px"></div>
        </div>
    </div>
    {{heat}}
    {{fan}}
    {{temp}}
    <script>
    (function(){
        var h = new Dygraph(
            document.getElementById("heat"),
            "{{ heat }}",
            {
                valueRange: [0,100]
            }
        );
        var f = new Dygraph(
            document.getElementById("fan"),
            "{{ fan }}",
            {
                valueRange: [0,100]
            }
        );
        var t = new Dygraph(
            document.getElementById("temp"),
            "{{ temp }}"
        );
        console.log(h);
    })();
</script>
{% endblock %}

网址模式:

url(r'^moderator/profile/(?P<mid>\d+)/$',views.profile, 
            name='profile'),

当我访问上述URL时,我还将在终端中包括输出。我不明白我的代码到底出了什么问题。有人可以指出错误的地方吗?

端子输出:

Not Found: /moderator/profile/1/1,29.72,29.73,29.74,29.75,29.76,29.87,30.28,30.39,30.410,30.711,30.812,31.213,31.414,31.615,31.816,31.917,32.218,32.419,32.520,32.721,33.022,33.123,33.224,33.325,33.426,33.627,33.728,33.829,33.930,33.931,34.032,34.133,34.134,34.335,34.336,34.437,34.438,34.539,34.440,34.541,34.542,34.743,34.644,34.745,34.746,34.847,34.848,34.849,34.850,35.051,34.852,35.053,35.054,35.055,35.056,35.157,35.158,35.059,35.160,35.161,35.162,35.163,35.164,35.165,35.266,35.267,35.268,35.169,35.270,35.271,35.272,35.273,35.274,35.275,35.276,35.277,35.278,35.379,35.380,35.381,35.282,35.383,35.384,35.385,35.386,35.387,35.388,35.389,35.390,35.491,35.392,35.393,35.494,35.495,35.496,35.497,35.598,35.499,35.4100,35.4101,35.5102,35.4103,35.5104,35.5105,35.5106,35.4107,35.5108,35.5109,35.5110,35.5111,35.5112,35.5113,35.5114,35.5115,35.5116,35.5117,35.5118,35.7119,35.5120,35.7121,35.5122,35.7123,35.7124,35.7125,35.7126,35.7127,35.7128,35.8129,35.7130,35.8131,35.7132,35.7133,35.7134,35.8135,35.8136,35.7137,35.8138,35.7139,35.7140,35.7141,35.7142,35.7143,35.8144,35.8145,35.8146,35.7147,35.8148,35.7149,35.8150,35.7151,35.7152,35.8153,35.8154,35.9155,36.0156,35.9157,36.0158,36.1159,36.2160,36.2161,36.2162,36.4163,36.5164,36.6165,36.6166,36.7167,36.8168,36.8169,36.9170,36.8171,36.9172,36.9173,37.0174,37.2175,37.2176,37.2177,37.3178,37.3179,37.3180,37.3181,37.3182,37.4183,37.4184,37.5185,37.5186,37.4187,37.4188,37.5189,37.5190,37.5191,37.5192,37.5193,37.5194,37.6195,37.6196,37.5197,37.6198,37.6199,37.6
[28/Oct/2018 23:15:27] "GET /moderator/profile/1/1,29.72,29.73,29.74,29.75,29.76,29.87,30.28,30.39,30.410,30.711,30.812,31.213,31.414,31.615,31.816,31.917,32.218,32.419,32.520,32.721,33.022,33.123,33.224,33.325,33.426,33.627,33.728,33.829,33.930,33.931,34.032,34.133,34.134,34.335,34.336,34.437,34.438,34.539,34.440,34.541,34.542,34.743,34.644,34.745,34.746,34.847,34.848,34.849,34.850,35.051,34.852,35.053,35.054,35.055,35.056,35.157,35.158,35.059,35.160,35.161,35.162,35.163,35.164,35.165,35.266,35.267,35.268,35.169,35.270,35.271,35.272,35.273,35.274,35.275,35.276,35.277,35.278,35.379,35.380,35.381,35.282,35.383,35.384,35.385,35.386,35.387,35.388,35.389,35.390,35.491,35.392,35.393,35.494,35.495,35.496,35.497,35.598,35.499,35.4100,35.4101,35.5102,35.4103,35.5104,35.5105,35.5106,35.4107,35.5108,35.5109,35.5110,35.5111,35.5112,35.5113,35.5114,35.5115,35.5116,35.5117,35.5118,35.7119,35.5120,35.7121,35.5122,35.7123,35.7124,35.7125,35.7126,35.7127,35.7128,35.8129,35.7130,35.8131,35.7132,35.7133,35.7134,35.8135,35.8136,35.7137,35.8138,35.7139,35.7140,35.7141,35.7142,35.7143,35.8144,35.8145,35.8146,35.7147,35.8148,35.7149,35.8150,35.7151,35.7152,35.8153,35.8154,35.9155,36.0156,35.9157,36.0158,36.1159,36.2160,36.2161,36.2162,36.4163,36.5164,36.6165,36.6166,36.7167,36.8168,36.8169,36.9170,36.8171,36.9172,36.9173,37.0174,37.2175,37.2176,37.2177,37.3178,37.3179,37.3180,37.3181,37.3182,37.4183,37.4184,37.5185,37.5186,37.4187,37.4188,37.5189,37.5190,37.5191,37.5192,37.5193,37.5194,37.6195,37.6196,37.5197,37.6198,37.6199,37.6 HTTP/1.1" 404 14396

我认为django将所有读数附加在url中,而不是以图形的形式显示在模板中。

0 个答案:

没有答案