1

我有一个 Django 应用程序,我正在向它添加 Django 日志对象,我遵循此文档https://code.osuosl.org/projects/django-object-log/wiki/Usage#Registering-Action-Types

我在文档中的(自动缓存数据)部分,这是如何添加构建缓存和注册的示例:

def build_cache(user, obj1, obj2, obj3, data):
     return {'foo':obj.foo}

 register('MY_EVENT', 'template.html', build_cache)

 def my_view(request, pk):
     """ example view that retrieves an object by its pk """ 
     obj = SomeModel.objects.get(pk=pk)
     log('MY_EVENT', request.user, obj) 

现在我的代码在添加上述部分时如下所示:

from models import *
from django.template import Context, loader, RequestContext
from django.shortcuts import render_to_response , get_object_or_404
from django.http import HttpResponseRedirect, HttpResponse, Http404
from django.core.urlresolvers import reverse
from django.shortcuts import redirect
from django.views.decorators.csrf import csrf_protect
from django.core.mail import EmailMessage
#from django.contrib.formtools.tests.wizard.forms import request

from object_log.models import LogAction
from object_log.models import LogItem

register('MY_EVENT', 'template.html', build_cache)  

def build_cache(user, obj1, obj2, obj3, data):
     return {'foo':obj.foo}



LogAction.objects.register('MY_EVENT','path/to/my/template.html')
LogAction.objects.register('MY_EVENT','path/to/my/template.html')

 # store log_action for faster access
log = LogItem.objects.log_action



def create_task(request):
    #Employer.objects.filter(pk=request.session[user_id])
    employer_id = request.user.id #Take the employer in the current session

    getcategory = request.POST['cat_dd']
    getsubcategory = request.POST['subcat_dd']
    getname = request.POST['task_name']
    getdesc = request.POST['task_desc']
    getcountry = request.POST['country_dd']
    getcity = request.POST['task_city']
    getaddress = request.POST['task_address']
    getstatus = request.POST['status_dd']
    if (employer_id == NULL or getcategory == "Please select a category" or getsubcategory == "Please select a subcategory" or getname == "" or getdesc == "Write a short description about your task.." or getcountry == "(please select a country)" or getcity == "" or getaddress == "" or getstatus == ""):
        return render_to_response('createTask.html',RequestContext(request))
    else:
        t = Task.objects.create(employer = employer_id, category = getcategory, subcategory = getsubcategory, name = getname, description = getdesc, country = getcountry, city = getcity, address = getaddress, status = getstatus, contractor = NULL)
        t.save()
        return render_to_response('task.html',RequestContext(request))

def edit_task(request, task_id):
    t = get_object_or_404(Task, pk=task_id)
    t.category = request.POST['cat_dd']
    t.subcategory = request.POST['subcat_dd']
    t.name = request.POST['task_name']
    t.description = request.POST['task_desc']
    t.country = request.POST['country_dd']
    t.city = request.POST['task_city']
    t.address = request.POST['task_address']
    t.status = request.POST['status_dd']

    log('MY_EVENT', request.user, t,data={'foo':obj.foo})

    if (employer_id == NULL or getcategory == "Please select a category" or getsubcategory == "Please select a subcategory" or getname == "" or getdesc == "Write a short description about your task.." or getcountry == "(please select a country)" or getcity == "" or getaddress == "" or getstatus == ""):
        return render_to_response('editTask.html',RequestContext(request))
    else:
        t = Task.objects.create(employer = employer_id, category = getcategory, subcategory = getsubcategory, name = getname, description = getdesc, country = getcountry, city = getcity, address = getaddress, status = getstatus, contractor = NULL)
        t.save()
        #log('MY_EVENT', request.user, obj, data={'foo':obj.foo}) 
        return render_to_response('task.html',RequestContext(request))

但我得到了一个错误名称'register' is not defined

错误有什么解决办法吗??

4

2 回答 2

0

尝试:

LogAction.objects.register('MY_EVENT', 'template.html', build_cache)

代替:

register('MY_EVENT', 'template.html', build_cache)  
于 2012-04-23T21:22:25.610 回答
0

如果我们使用 LogAction.objects.register('MY_EVENT', 'template.html', build_cache) 错误将变为 NameError: name 'LogAction' is not defined

于 2021-04-24T06:15:46.007 回答