django-likes:设置和错误

时间:2013-07-21 23:19:46

标签: django packages

对于Django来说还是比较新的我正在尝试实现我的第一个包,django-like,我一直在遇到错误。下面列出的文档和项目代码。非常感谢任何建议和见解。

注意:模型正被用作ModelForm

文档: https://crate.io/packages/django-likes/

错误:

Exception Type: KeyError        
Exception Value: 'request'       
Exception Location: /home/alxmrph/webapps/dearmusic/lib/python2.7/django/template/context.py in __getitem__, line 57   
Python Executable: /usr/local/bin/python

settings.py:

TEMPLATE_CONTEXT_PROCESSORS =(
    'django.contrib.auth.context_processors.auth',      
    'django.core.context_processors.debug',     
    'django.core.context_processors.i18n',     
    'django.core.context_processors.media',     
    'django.core.context_processors.static',     
    'django.core.context_processors.tz',     
    'django.contrib.messages.context_processors.messages',    
    'django.core.context_processors.request',     
)

MIDDLEWARE_CLASSES = (    
    'django.middleware.common.CommonMiddleware',   
    'django.contrib.sessions.middleware.SessionMiddleware',    
    'django.middleware.csrf.CsrfViewMiddleware',     
    'django.contrib.auth.middleware.AuthenticationMiddleware',    
    'django.contrib.messages.middleware.MessageMiddleware',     
    'secretballot.middleware.SecretBallotIpMiddleware',     
    'secretballot.middleware.SecretBallotUserIpUseragentMiddleware',     
    'likes.middleware.SecretBallotUserIpUseragentMiddleware',     
)

INSTALLED_APPS = (
    ...    
    'secretballot',    
    'likes',
)

models.py:

from django.db import models     
import secretballot     
...

class Letter(models.Model):  
...

    def __unicode__(self):
        return self.name

secretballot.enable_voting_on(Letter)

views.py:

def submission(request, pk):    
    submission = get_object_or_404(Letter, pk=int(pk))     
    pk = int(pk)     
    return render_to_response('post.html',     
            {'submission':submission},     
            context_instance=RequestContext(request))

post.html:

{% load likes_inclusion_tags %}    
<some HTML>    
  {% likes submission %}    
<some HTML>

回溯

Environment:
Request Method: GET`

Django Version: 1.5
Python Version: 2.7.5
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'taggit',
'letter',
'south',
'django_social_share',
'secretballot',
'likes')

Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'secretballot.middleware.SecretBallotIpUseragentMiddleware',
'likes.middleware.SecretBallotUserIpUseragentMiddleware')


Template error:
In template /home/alxmrph/webapps/dearmusic/dearmusic/letter/templates/post.html, error at line 15
request
5 :  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

6 : <html>

7 :  <head>

8 :     <link href="/static/css/letter.css" rel="stylesheet" type="text/css">

9 :         <link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>

10 : 

11 : </head>

12 : 

13 : <body>

14 :    <title class="title">Music Morphs - The Story of {{ submission.name }}: {{ submission.letter_summary }}</title>

15 :      {% likes submission %} 

16 :    <header class="header">

17 :        <div class="wrapper_header">

18 :            <div class="title">

19 :                <img src="/static/images/logo.png">

20 :            </div>

21 :            <nav><ul>

22 :                <li><a href="#">Write Your Story</a></li>


23 :            </ul></nav>


24 :        </div>


25 :    </header>


Traceback:
File "/home/alxmrph/webapps/dearmusic/lib/python2.7/django/core/handlers/base.py" in get_response
115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/alxmrph/webapps/dearmusic/dearmusic/letter/views.py" in submission
  13.     return render_to_response('post.html',
File "/home/alxmrph/webapps/dearmusic/lib/python2.7/django/shortcuts/__init__.py" in render_to_response
  29.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/home/alxmrph/webapps/dearmusic/lib/python2.7/django/template/loader.py" in render_to_string
 172.         return t.render(Context(dictionary))
File "/home/alxmrph/webapps/dearmusic/lib/python2.7/django/template/base.py" in render
 140.             return self._render(context)
File "/home/alxmrph/webapps/dearmusic/lib/python2.7/django/template/base.py" in _render
 134.         return self.nodelist.render(context)
File "/home/alxmrph/webapps/dearmusic/lib/python2.7/django/template/base.py" in render
 830.                 bit = self.render_node(node, context)
File "/home/alxmrph/webapps/dearmusic/lib/python2.7/django/template/debug.py" in render_node
 74.             return node.render(context)
File "/home/alxmrph/webapps/dearmusic/lib/python2.7/django/template/base.py" in render
 1185.                     _dict = func(*resolved_args, **resolved_kwargs)
File "/home/alxmrph/lib/python2.7/django_likes-0.0.11-py2.7.egg/likes/templatetags/likes_inclusion_tags.py" in likes
 14.     request = context['request']
File "/home/alxmrph/webapps/dearmusic/lib/python2.7/django/template/context.py" in __getitem__
 57.         raise KeyError(key)

Exception Type: KeyError at /letter/testing/15/what/
Exception Value: 'request'`

1 个答案:

答案 0 :(得分:1)

您似乎在Django 1.5上运行django-likestakes_context has changed

inclusion_tag参数的使用
  

请注意,当您使用takes_context = True时,则无需执行此操作   将参数传递给模板标记。它会自动获得访问权限   背景。

likes模板代码is

@register.inclusion_tag('likes/inclusion_tags/likes_extender.html', takes_context=True)
def likes(context, obj, template=None):
    if template is None:
        template = 'likes/inclusion_tags/likes.html'
    request = context['request']
    import_js = False
    if not hasattr(request, '_django_likes_js_imported'):
        setattr(request, '_django_likes_js_imported', 1)
        import_js = True
    context.update({
        'template': template,
        'content_obj': obj,
        'likes_enabled': likes_enabled(obj, request),
        'can_vote': can_vote(obj, request.user, request),
        'content_type': "-".join((obj._meta.app_label, obj._meta.module_name)),
        'import_js': import_js
    })
    return context

Django 1.4

中没问题

更新

最简单的解决方案是降级Django。如果你想继续使用Django 1.5,你必须修改

def likes(context, obj, template=None):

to and lern it to get obj from context

def likes(context):

    obj = context['likes_object']
    ...

在模板中,您可以使用with代码

传递变量
{% with likes_object = submission %}
    {% likes %}
{% endwith %}