JQuery自定义验证错误(JQuery custom validation error)
我使用JQuery验证器面临以下问题:我有一个表单,我使用提供的默认验证规则,一切正常。 但是,只要我添加自定义验证,就会抛出以下错误:
- 在Chrome中:未捕获TypeError:无法调用未定义的方法'addMethod'
- 在IE8中:'validator'为null或不是对象
- FireFox:没有错误消息,但JQuery默认验证逻辑中断;
导致错误发生的代码是这个(它位于ready函数内):
$().validator.addMethod("defaultInvalid", function(value, element) { alert("validate"); } );
如您所见,我已经评论了回调函数的大部分代码,以消除任何其他错误。 如果我发表评论,一切都会恢复正常。 但是,我想在文本框上使用此自定义规则,必须具有默认值“名字”,并且在提交时,不应允许用户将“名字”作为值发送。
I am facing the following issue with the JQuery validator: I have a form where I am using the default validation rules provided and everything works fine. Howoever, as soon as I add a custom validation, the following error is thrown:
- in Chrome: Uncaught TypeError: Cannot call method 'addMethod' of undefined
- in IE8: 'validator' is null or not an object
- FireFox: no error message, but the JQuery default validation logic breaks;
The piece of code which is causing the error to occur is this (it sits inside the ready function):
$().validator.addMethod("defaultInvalid", function(value, element) { alert("validate"); } );
As you see I have commented most of the code of the callback function in order to eliminate any other error. if I comment it, everything goes back to normal. However, I would like to use this customized rule on a text box taht must have the default value of "First name" and on submit it should not allow the user to send "First name" as value.
原文:https://stackoverflow.com/questions/14626467
最满意答案
这不是很简单,但我能够解决它。 我从文件中导入了这个我的clean_sessions():
from importlib import import_module from django.conf import settings
然后,在函数内部,我加载了SessionStore对象:
SessionStore = import_module(settings.SESSION_ENGINE).SessionStore
从那里,很容易删除会话,留下这样的方法:
def clean_sessions(): stored_sessions = Session.objects.all() active_users = Request.objects.active_users(seconds=15) active_users_ids = [user.id for user in active_users] for session in stored_sessions: SessionStore = import_module(settings.SESSION_ENGINE).SessionStore s = SessionStore(session_key=session.session_key) session_uid = session.get_decoded().get('_auth_user_id') if not session_uid: s.delete() continue if session_uid not in active_users_ids: ## some code ## s.delete()
从你正在使用的任何会话引擎加载正确的SessionStore非常重要,否则它将无法从两个地方(DB和缓存)中删除它。
It wasn't very straightforward but I was able to fix it. I imported this from the file I have my clean_sessions():
from importlib import import_module from django.conf import settings
Then, inside the function, I loaded the SessionStore object:
SessionStore = import_module(settings.SESSION_ENGINE).SessionStore
From there, it was very easy to remove the sessions, leaving the method like this:
def clean_sessions(): stored_sessions = Session.objects.all() active_users = Request.objects.active_users(seconds=15) active_users_ids = [user.id for user in active_users] for session in stored_sessions: SessionStore = import_module(settings.SESSION_ENGINE).SessionStore s = SessionStore(session_key=session.session_key) session_uid = session.get_decoded().get('_auth_user_id') if not session_uid: s.delete() continue if session_uid not in active_users_ids: ## some code ## s.delete()
It's very important to load the right SessionStore from whatever session engine you're using, otherwise it will fail to remove it from both places (DB and cache).
相关问答
更多-
我目前使用django-redis作为Redis的缓存后端。 到目前为止,我还没有使用过django-redis-cache,但是让我决定使用django-redis的原因如下: 模块化客户端系统(可插拔客户端)。 一些可插拔的客户端开箱即用(碎片客户端,牧群客户端等) 主从支持默认客户端。 原始访问Redis客户端/连接池的设施(非常有用)。 更好地记录。 在django-redis文档网站上 ,您可以找到更多考虑它的理由。 根据我的经验,我可以告诉我的是,我对django-redis非常满意:)祝你好运 ...
-
Redis是存储会话的理想选择。 所有操作都在内存中执行,因此读取和写入将会很快。 第二个方面是持续的会话状态。 Redis为您提供了如何将会话状态保留到硬盘的灵活性。 您可以通过http://redis.io/topics/persistence了解更多信息,但在高层次,您可以选择这些选项 - 如果您无法承受丢失任何会话, appendfsync always在配置文件中设置appendfsync always 。 因此,Redis保证任何写入操作都保存到磁盘。 缺点是写操作会更慢。 如果你失去了大约1年 ...
-
Redis的这个Python模块在自述文件中有一个明确的用法示例: http : //github.com/andymccurdy/redis-py Redis被设计为RAM缓存。 它支持基本的GET和SET键,以及诸如字典的集合的存储。 您可以通过将其输出存储在Redis中来缓存RDBMS查询。 目标是加快您的Django网站。 在您需要速度之前,不要开始使用Redis或任何其他缓存 - 不要过早优化。 This Python module for Redis has a clear usage exam ...
-
我正在使用Django 1.9和django-redis版本4.3 ,我已将我的缓存设置为使用RedisCache : CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379/1", "OPTIONS": { "CLIENT_CLASS": "django_redis.clien ...
-
这不是很简单,但我能够解决它。 我从文件中导入了这个我的clean_sessions(): from importlib import import_module from django.conf import settings 然后,在函数内部,我加载了SessionStore对象: SessionStore = import_module(settings.SESSION_ENGINE).SessionStore 从那里,很容易删除会话,留下这样的方法: def clean_sessions(): ...
-
我很久以前就找到了解决我所有问题的方法,因为有人投了票,我想他们可能需要一个答案,所以我在这里发布一个答案。 服务器/ server.js var redis = require("redis"); var session = require('express-session'); var redisStore = require('connect-redis')(session); app.use(session({ secret: 'hello', store: new redisS ...
-
可以将相同的Redis实例用于Sessions和Laravel队列吗?(Can the same Redis instance be used for Sessions and Laravel Queues?)[2022-04-22]
是的,它们可以用于会话和队列。 Laravel使用不同的redis对象来存储会话数据,缓存数据和队列数据。 您应该将队列命名为不同的名称。 此外,在redis中,延迟队列数据和直接队列数据之间存在分离。 Laravel会话扫描不会触及队列数据。 Yes they can be used for both sessions and queues. Laravel uses different redis objects for storing session data, cache data and queu ... -
Redis是存储会话的快速而快捷的方式,而不是在SQL DB中添加它们(尝试监视查询会话表所花费的时间),如果您使用django-toolbar app之类的工具,这一点显而易见。 另一方面,唯一可能显示为问题的是会话数据持久性,Redis已经使用2种不同的方法处理,您可以根据需要选择,请参阅文档了解更多详情,以及制作群集或故障转移Redis服务器的选项。 我遇到了类似的情况,我需要将我的会话存储在NoSQL数据库中以获得速度和其他与项目相关的原因,这是因为我使用了django-redisession应用程 ...
-
django-redis只为你提供了一个redis 缓存后端 : django-redis是Django的BSD许可,全功能redis缓存/会话后端。 使用redis-py,你可以与redis服务器“对话”,它是一个python redis接口。 据我所知,问题是你想如何直接通过界面或使用django的缓存系统与redis进行交互。 如果您希望此数据“过期”或者您希望使用redis缓存其他实体,或者您希望以redis存储会话 - 请使用django-redis。 另外,直接使用redis-py或同时使用re ...
-
在Django中使用会话(Using sessions in Django)[2023-10-30]
是否有一个好的经验法则可以使用哪一个? 没有。 Cached_db似乎永远是一个更好的选择...... 没关系。 在某些情况下,有许多Django(和Apache)进程查询公共数据库。 mod_wsgi以这种方式允许很多可伸缩性。 缓存没有多大帮助,因为会话是在Apache(和Django)进程之间随机分布的。 是否可以同时使用,会话在浏览器关闭时到期并给出年龄? 不明白为什么不。 什么被认为是“不活跃”? 我假设你在开玩笑。 “活动”是 - 好 - 活动。 你懂。 发生在Django的东西。 Django ...