我是Django的新手并使用Django 1.6,
我有用户注册表单我正在使用Django Messaging
模块。
但是当我提交表格时,我收到了一个错误:
You cannot add messages without installing django.contrib.messages.middleware.MessageMiddleware
我在INSTALLED_APPS中有django.contrib.messages
,在MIDDLEWARE_CLASSES中有django.contrib.messages.middleware.MessageMiddleware
。
我已经查看了此Thread中提供的解决方案,但它对我没有用。
以下是代码:
settings.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'south',
'usersapp',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
)
Views.py:
class RegisterView(FormView):
template_name = 'register_form.html'
form_class = RegisterForm
def form_valid(self, request):
f = RegisterForm(self.request.POST)
f.save()
messages.info(request, "User added.")
return redirect('/usersapp')
如果有人遇到此问题并有解决方案。请发帖。
答案 0 :(得分:0)
我遇到了同样的问题,这就是我找到的解决方案: 我有一个CBV方法的装饰者;这个装饰器会检查权限并发出消息,如果无效则重定向。解决方法是使用method_decorator装饰器来调用装饰器函数,所以:
class MyView(FormView):
...
@custom_permission_required('my.permission')
def dispatch(self, *args, **kwargs):
成为:
from django.utils.decorators import method_decorator
...
class MyView(FormView):
...
@method_decorator(custom_permission_required('my.permission'))
def dispatch(self, *args, **kwargs):