您好我正在尝试使用Ajax使用django-facebook connect为我的django应用程序实现facebook登录。我正在关注此链接以实现它facebook-django integration with ajax。在这样做时,我遇到了这个错误“TemplateSyntaxError at / registrationForm /
'facebookconnect'不是有效的标签库:找不到模板库facebookconnect,尝试过django.templatetags.facebookconnect,django.contrib.admin.templatetags.facebookconnect,facebookconnect.templatetags.facebookconnect“。我将把错误日志粘贴到这里环境:
Request Method: GET
Request URL: http://10.1.0.90:8080/registrationForm/
Django Version: 1.2.5
Python Version: 2.6.5
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'universityDetails',
'django.contrib.admin',
'captcha',
'facebookconnect',
'social_auth']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.csrf.CsrfResponseMiddleware',
'universityDB.FacebookConnectMiddleware.FacebookConnectMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'facebook.djangofb.FacebookMiddleware',
'facebookconnect.middleware.FacebookConnectMiddleware')
Template error:
In template /home/ravi/Desktop/universityDB/templates/registrationForm.html, error at line 75
'facebookconnect' is not a valid tag library: Template library facebookconnect not found, tried django.templatetags.facebookconnect,django.contrib.admin.templatetags.facebookconnect,facebookconnect.templatetags.facebookconnect
65 : <p style="clear:both;float:left;">
66 : <input type="submit" value="Log in" />
67 :
68 : <a HREF="http://10.1.0.90:8080/forgotPassword/">Forgotten your password?</a></p>
69 : </div>
70 : </form>
71 : <br>
72 :
73 : <p>Login via facebook!</p>
74 :
75 : {% load facebookconnect %}
76 : {% facebook_connect_login_button %}
77 :
78 : {% facebook_connect_script %}
79 :
80 :
81 :
82 : <br>
83 : <br>
84 : <br>
85 : <br>
Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
100. response = callback(request, *callback_args, **callback_kwargs)
File "/home/ravi/Desktop/universityDB/../universityDB/universityDetails/views.py" in registrationForm
50. return render_to_response("registrationForm.html")
File "/usr/local/lib/python2.6/dist-packages/django/shortcuts/__init__.py" in render_to_response
20. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader.py" in render_to_string
181. t = get_template(template_name)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader.py" in get_template
157. template, origin = find_template(template_name)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader.py" in find_template
134. source, display_name = loader(name, dirs)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader.py" in __call__
42. return self.load_template(template_name, template_dirs)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader.py" in load_template
48. template = get_template_from_string(source, origin, template_name)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader.py" in get_template_from_string
168. return Template(source, origin, name)
File "/usr/local/lib/python2.6/dist-packages/django/template/__init__.py" in __init__
158. self.nodelist = compile_string(template_string, origin)
File "/usr/local/lib/python2.6/dist-packages/django/template/__init__.py" in compile_string
186. return parser.parse()
File "/usr/local/lib/python2.6/dist-packages/django/template/__init__.py" in parse
282. compiled_result = compile_func(self, token)
File "/usr/local/lib/python2.6/dist-packages/django/template/defaulttags.py" in load
928. (taglib, e))
Exception Type: TemplateSyntaxError at /registrationForm/
Exception Value: 'facebookconnect' is not a valid tag library: Template library facebookconnect not found, tried django.templatetags.facebookconnect,django.contrib.admin.templatetags.facebookconnect,facebookconnect.templatetags.facebookconnect
我的目录结构是
universityDB/
universityDetails
models.py
views.py
urls.py
templates/
xd_receiver.htm
templatetags/
__init__.py
facebookconnect.py
我的facebookconnect.py是
from django import template
from django.conf import settings
from django.core.urlresolvers import reverse
register = template.Library()
class FacebookScriptNode(template.Node):
def render(self, context):
return """
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript"> FB.init("%s", "%s");
function facebook_onlogin() {
var uid = FB.Facebook.apiClient.get_session().uid;
var session_key = FB.Facebook.apiClient.get_session().session_key;
var expires = FB.Facebook.apiClient.get_session().expires;
var secret = FB.Facebook.apiClient.get_session().secret;
var sig = FB.Facebook.apiClient.get_session().sig;
fb_connect_ajax(expires, session_key, secret, uid, sig);
}
function fb_connect_ajax(expires, session_key, ss, user, sig) {
var post_string = 'expires=' + expires;
post_string = post_string + '&session_key=' + session_key;
post_string = post_string + '&ss=' + ss;
post_string = post_string + '&user=' + user;
post_string = post_string + '&sig=' + sig;
$.ajax({
type: "POST",
url: "%s",
data: post_string,
success: function(msg) {
window.location = '%s'; //.reload()
}
});
}
</script>
""" % (settings.FACEBOOK_API_KEY, reverse('xd_receiver'), reverse('facebook_connect_ajax'), settings.LOGIN_REDIRECT_URL)
def facebook_connect_script(parser, token): return FacebookScriptNode()
register.tag(facebook_connect_script)
class FacebookLoginNode(template.Node):
def render(self, context):
return "<fb:login-button onlogin='facebook_onlogin();'></fb:login-button>"
def facebook_connect_login_button(parser, token): return FacebookLoginNode()
register.tag(facebook_connect_login_button)
答案 0 :(得分:1)
您的facebookConnect.py
文件必须位于templatetags
包裹内(必须有__init__.py
才能将其作为包裹)。
在您的settings.INSTALLED_APPS
设置中,您似乎还添加了facebookconnect
,即使这不是应用(按目录结构进行)。相反,您应该将universityDB.universityDetails
列为应用。