私信  •  关注

Reinstate Monica

Reinstate Monica 最近创建的主题
Reinstate Monica 最近回复了
6 年前
回复了 Reinstate Monica 创建的主题 » 如何从EJS、Javascript写入MongoDB数据库?

澄清一下,ejs是

ejs用于:

  • 将变量从服务器传递到html文件

做你想做的事,你需要 请求 从javascript文件到app.js页面上设置的特定路由,然后从服务器向db发出请求。假设您正在使用express,请按照以下步骤操作。如果您不使用express,请告诉我,我可以指导您完成设置。

首先,您需要页面底部的jQuery脚本:

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> 

<!-- make sure this script is above all your other scripts! -->

$.ajax({url:'/somePathHere', success: function(data) {

    //code you want to execute in the clients browser after the request is made goes here

}});

最后,在app.js页面上:

app.get('/somePathHere', function(req, res) {
    //make your call to the db here
}):
5 年前
回复了 Reinstate Monica 创建的主题 » python在字典中附加一个带有字典的数组

您可以像附加任何其他数组一样将 .append :

maindict['array'].append(d1)

maindict['array'].append(d2)
5 年前
回复了 Reinstate Monica 创建的主题 » 在Django中下载文件时如何在网页中显示内容

您不应该在后端执行此操作,而是添加一个事件侦听器,该侦听器将触发 confirm :

$('#download_button_id').on('click', function(e) {
  if (!confirm('This will be an attachment to download!')) {
    e.preventDefault();
  };
});

现在,当用户尝试按下下载按钮时,会弹出一个对话框,显示带有确认/取消按钮的消息,如果按下取消按钮, e.preventDefault() 触发器。

5 年前
回复了 Reinstate Monica 创建的主题 » Python 3.7中通过逐段计数单词的自定义数据结构

试试这个:

import re
from nltk.tokenize import word_tokenize, RegexpTokenizer

def normalize_text(file):
    file = re.sub('<P ID=(\d+)>', '', file)
    file = re.sub('</P>', '', file)
    tokenizer = RegexpTokenizer(r'\w+')
    all_words = tokenizer.tokenize(file)
    lower_case = []
    for word in all_words:
        curr = word.lower()
        lower_case.append(curr)

    return lower_case

def find_words(filepath):
    with open(filepath, 'r') as f:
        file = f.read()
    word_list = normalize_text(file)
    data = file.replace('</P>','').split('<P ID=')
    result = {}
    for word in word_list:
        result[word] = {}
        for p in data:
            if p:
                result[word][f'paragraph_{p[0]}'] = p[2:].count(word)
    print(result)
    return result

find_words('./test.txt')

如果要按段落分组,则按单词出现次数分组:

def find_words(filepath):
    with open(filepath, 'r') as f:
        file = f.read()
    word_list = normalize_text(file)
    data = file.replace('</P>','').split('<P ID=')
    result = {}
    for p in data:
        if p:
            result[f'paragraph_{p[0]}'] = {}
            for word in word_list:
                result[f'paragraph_{p[0]}'][word] = p[2:].count(word)


    print(result)
    return result 

不过还是有点难读。如果漂亮的打印对象对您很重要,您可以尝试使用 pretty printing package .

要查找单词出现的段落数,请执行以下操作:

def find_paragraph_occurrences(filepath):
    with open(filepath, 'r') as f:
        file = f.read()
    word_list = normalize_text(file)
    data = file.replace('</P>','').lower().split('<P ID=')
    result = {}
    for word in word_list:
        result[word] = 0
        for p in data:
            if word in p:
                result[word] += 1

    print(result)
    return result
6 年前
回复了 Reinstate Monica 创建的主题 » 用mysql单击按钮后隐藏div

解决问题的最简单方法是使用客户端cookie。如果你还没有饼干包装,我建议你 js-cookie .

在html页面上:

<div id="promotion-container" hidden> //you want it hidden by default
//if it is shown by default there will be an annoying screen flicker when the pages loads

<script src="/path/to/js.cookie.js"></script>

在jQuery页面上:

$(document).ready( function() {
    if (!Cookies.get('name1') || !Cookies.get('name2')) {
       $('#promotion-container').show();
    };

    Cookies.set('name1', customerId, {expires:14} ); //must include expiration else the cookie will expire when the browser closes.
    Cookies.set('name2', eventName, {expires:14} ); //you might need to make an eventId if eventName is too large       
});

的第二个输入 Cookies.set 'value' 为了饼干。如果 'value' = null ,然后 Cookies.get('name') = null .

这是假设你已经有办法 customerId eventName 对于每个用户。此外,您可能需要根据在页面上创建customerId的时间修改Cookies的设置位置。

编辑:

有两种方法可以按您描述的方式运行查询,但这两种方法都不能按您希望的方式运行 除非 使用会话cookie。

(一) 你有 res.render 在查询的内部。

这将确保 div 从未显示给已单击它的用户,但会严重损害站点的性能。无论客户机是否具有customerId,每次呈现页面时都将运行查询。你的网站将是痛苦的缓慢与大量的流量。

2个) 使用ajax通过客户端js运行POST请求,并将customerId与db进行比较;如果找到结果,则删除div。

这将按您所希望的方式运行,并且不会影响性能,但没有什么能阻止客户使用 burp 拦截POST请求。他们可以将数据参数更改为他们想要的任何参数,并确保为他们加载div。

我看到的解决这些问题的唯一方法是在用户单击div和服务器上的会话cookie时验证用户。(对于用户验证,我使用 passport express-session ).

我可以告诉你我如何设置这个,但要使它具体到你的需要,我需要知道更多关于你的网站是如何设置的。

我误解了为什么你需要隐藏div,事后看来使用客户端cookie是个糟糕的主意。