如何在使用Django和Python上传页面后在客户端获取查询字符串值

时间:2017-09-13 08:44:40

标签: javascript python django

我需要从客户端的URL获取查询字符串值,并从Django模板传递该URL。我在下面解释我的代码。

  

base.html文件:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    {% load static %}
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  </head>
  <body>
    <header>
      <h1>Nuclear Reactor</h1>
      {% if count > 0  %}
      <b>Hi, {{ user.username }}</b>
        <a href="{% url 'home' %}?file=//cdnjs.cloudflare.com/ajax/libs/velocity/1.5.0/velocity.min.js">Home</a>
        <a href="{% url 'view_reactor' %}?file=//cdnjs.cloudflare.com/ajax/libs/velocity/1.5.0/velocity.min.js">View Reactor status</a>
        <a href="{% url 'logout' %}">logout</a>
      {% else %}
        <a href="{% url 'login' %}">login</a> / <a href="{% url 'signup' %}">signup</a>
      {% endif %}
      <hr>
    </header>
    <main>
      {% block content %}
      {% endblock %}
    </main>
  </body>
</html>

这里我传递了一些查询字符串值home.html loading。

  

home.html的:

{% extends 'base.html' %}

{% block content %}
<center><h1>Welcome</h1>
<p>This App allow to control the life cycle of the Nuclear Reactor and Retrive the status report </p>
<p><a href="{% url 'status' %}">Status report</a><a href="{% url 'control' %}">Control panel</a></p>
</center>
<script type="text/javascript">
    window.onload=function(){

    }
</script>
{% endblock %}

我需要在呈现主页时使用JavaScript获取查询字符串。

1 个答案:

答案 0 :(得分:0)

如果您想在客户端执行此操作,我认为您可以使用javascript位置对象:

  DECLARE @t TABLE(
    Asset nvarchar(100),
    StartDate datetime,
    EndDate datetime,
    Code nvarchar(100),
    Reason nvarchar(100)
  );

  DECLARE @StartDate DATETIME = '2017-01-01 00:00:00';
  DECLARE @EndDate DATETIME = '2017-01-02 20:00:00';

  INSERT INTO @t VALUES  ('Asset 1', '2017-01-01 06:00:00', '2017-01-01 09:00:00', 'Code 1', 'Reason 1')
                        ,('Asset 1', '2017-01-01 15:00:00', '2017-01-01 16:00:00', 'Code 1', 'Reason 2')
                        ,('Asset 1', '2017-01-02 12:00:00', '2017-01-02 13:00:00', 'Code 2', 'Reason 2')
                        ,('Asset 2', '2017-01-01 07:00:00', '2017-01-01 08:00:00', 'Code 1', 'Reason 1')
                        ,('Asset 2', '2017-01-01 11:00:00', '2017-01-01 14:00:00', 'Code 1', 'Reason 2')
                        ,('Asset 2', '2017-01-02 15:00:00', '2017-01-02 18:00:00', 'Code 2', 'Reason 2')
                        ,('Asset 2', '2017-01-02 18:00:00', '2017-01-02 19:00:00', 'Code 2', 'Reason 2');

  WITH cte AS(
    SELECT *
          ,EndDate AS StartDateNew
          ,ISNULL(LEAD(StartDate) OVER (PARTITION BY Asset ORDER BY StartDate), @EndDate) AS EndDateNew
          ,CASE WHEN EndDate = LEAD(StartDate) OVER (PARTITION BY Asset ORDER BY StartDate) OR EndDate = @EndDate THEN 0 ELSE 1 END AS HasGap
          ,ROW_NUMBER() OVER (PARTITION BY Asset ORDER BY StartDate) AS rn
      FROM @t t
    WHERE StartDate >= @StartDate
  )
  SELECT Asset, StartDate, EndDate, Code, Reason
    FROM cte
  UNION ALL
  SELECT Asset, StartDateNew, EndDateNew, 'Code10', 'Reason10'
    FROM cte
    WHERE HasGap = 1
  UNION ALL
  SELECT Asset, @StartDate AS StartDate, StartDate AS EndDate, 'Code10', 'Reason10'
    FROM cte
    WHERE rn = 1
      AND StartDate > @StartDate
  ORDER BY 1, 2  

否则,您可以从服务器端(在视图中)获取请求中的参数,并将对象注入模板。