我项目中的所有模板都有一个注册按钮,只有当您以匿名用户身份输入时才能看到该按钮。该按钮打开一个带有两个注册表单的模式窗口。问题是我必须在我的所有视图中都有这个代码才能工作。有什么方法可以避免在我所有的视图中重复与注册表相关的代码?
澄清:在我的所有观点中重复的代码被注释为#USER REGISTRATION FORM RELATIVE 和#MERCHANT REGISTRATION FORM RELATIVE
我的模式窗口:
我的观点之一(75 行):
def patrimonio_view(request, backend_registro_usuario, backend_registro_comerciante, success_url=None, form_class_usuario=None, form_class_comerciante=None,
disallowed_url='registration_disallowed',
template_name='home/patrimonio.html',
extra_context=None):
#PATRIMONIO QUERYS
pat = patrimonio.objects.all()
ciu = ciudad.objects.all()
#USER REGISTRATION FORM RELATIVE
backend_registro_usuario = get_backend(backend_registro_usuario)
if not backend_registro_usuario.registration_allowed(request):
return redirect(disallowed_url)
if form_class_usuario is None:
form_class_usuario = backend_registro_usuario.get_form_class(request)
if "Registrar_usuario" in request.POST:
form_class_usuario = form_class_usuario(data=request.POST, files=request.FILES)
if form_class_usuario.is_valid():
new_usuario = backend_registro_usuario.register(request, **form_class_usuario.cleaned_data)
if success_url is None:
to, args, kwargs = backend_registro_usuario.post_registration_redirect(request, new_usuario)
return redirect(to, *args, **kwargs)
else:
return redirect(success_url)
else:
form_class_usuario = form_class_usuario()
if extra_context is None:
extra_context = {}
context = RequestContext(request)
for key, value in extra_context.items():
context[key] = callable(value) and value() or value
#MERCHANT REGISTRATION FORM RELATIVE
backend_registro_comerciante = get_backend(backend_registro_comerciante)
if not backend_registro_comerciante.registration_allowed(request):
return redirect(disallowed_url)
if form_class_comerciante is None:
form_class_comerciante = backend_registro_comerciante.get_form_class(request)
if "Registrar_comerciante" in request.POST:
form_class_comerciante = form_class_comerciante(data=request.POST, files=request.FILES)
if form_class_comerciante.is_valid():
new_comerciante = backend_registro_comerciante.register(request, **form_class_comerciante.cleaned_data)
if success_url is None:
to, args, kwargs = backend_registro_comerciante.post_registration_redirect(request, new_comerciante)
return redirect(to, *args, **kwargs)
else:
return redirect(success_url)
else:
form_class_comerciante = form_class_comerciante()
if extra_context is None:
extra_context = {}
context = RequestContext(request)
for key, value in extra_context.items():
context[key] = callable(value) and value() or value
#WE OBTAIN THE USERS FOR EACH GROUP
usuario_group = Group.objects.get(name="usuario").user_set.all()
comerciante_group = Group.objects.get(name="comerciante").user_set.all()
#RENDER
return render_to_response(template_name,
{'form_registro_usuario': form_class_usuario,
'form_registro_comerciante': form_class_comerciante,
'patrimonio':pat,
'ciudad':ciu,
'usuario_group': usuario_group,
'comerciante_group': comerciante_group,
},
context_instance=context)
解决方案 - 重构代码(29 行):
def patrimonio_view(request,
template_name='home/patrimonio.html'):
#PATRIMONIO QUERYS
pat = patrimonio.objects.all()
ciu = ciudad.objects.all()
if request.method == 'POST':
if "Registrar_usuario" in request.POST:
#USER REGISTRATION FORM RELATIVE
return register_usuario()
if "Registrar_comerciante" in request.POST:
#MERCHANT REGISTRATION FORM RELATIVE
return register_comerciante()
#WE OBTAIN THE USERS FROM EACH GROUP
usuario_group = Group.objects.get(
name="usuario").user_set.all()
comerciante_group = Group.objects.get(
name="comerciante").user_set.all()
#RENDER
return render_to_response(template_name,{
'patrimonio':pat,
'ciudad':ciu,
'usuario_group': usuario_group,
'comerciante_group': comerciante_group,
}, context_instance=RequestContext(request))