我项目中的所有模板都有一个注册按钮,只有在您作为无用用户输入时才能看到。该按钮打开一个带有两个注册表单的模态窗口。问题是我必须在我的所有视图中都有这个代码才能工作。有一些方法可以避免在我的所有视图中重复相对于注册表单的代码吗?
澄清:我在所有观看中重复的代码被评为#USER REGISTRATION FORM RELATIVE和#MERCHANT REGISTRATION FORM RELATIVE
MY MODAL WINDOW :
我的一个观点(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))
答案 0 :(得分:2)
如果我错过了什么,请告诉我,我会解决它。抱歉,我无法理解其他代码。
def patrimonio_view(request,
backend_registro_usuario, backend_registro_comerciante,
form_class_usuario=None, form_class_comerciante=None,
disallowed_url='registration_disallowed', template_name='home/patrimonio.html',
extra_context=None, success_url=None):
#PATRIMONIO QUERYS
pat = patrimonio.objects.all()
ciu = ciudad.objects.all()
form_class_usuario = form_class_usuario()
form_class_comerciante = form_class_comerciante()
if request.method == 'POST':
if "Registrar_usuario" in request.POST:
#USER REGISTRATION FORM RELATIVE
return user_registration(request,
backend_registro_usuario,
disallowed_url,
form_class_usuario,
success_url
)
if "Registrar_comerciante" in request.POST:
#MERCHANT REGISTRATION FORM RELATIVE
return merchant_registration(request,
backend_registro_comerciante,
disallowed_url,
form_class_comerciante,
success_url
)
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
#Obtenemos los usuarios de cada grupo
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)
def user_registration(request,
backend_registro_usuario, form_class_usuario,
disallowed_url, success_url):
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)
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)
def merchant_registration(request,
backend_registro_comerciante, form_class_comerciante,
disallowed_url, success_url):
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)
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)
答案 1 :(得分:1)
在多个视图中包含代码至少有三个选项:包含标记,上下文处理器和模板继承。这取决于哪一个对您的设置最有意义。
context processor可能是将条件逻辑保留在模板之外的最佳选择,因为您需要检查用户是否是匿名用户,您可以在处理器中执行服务器端:
from django.template.loader import render_to_string
def your_context_processor(request):
if request.user.is_anonymous():
return {'extra_context': render_to_string('anonymous-only-content.html')}
else:
return {}
然后在你的模板中,你可以这样做:
{{ extra_context }}
如果填充了该变量,则需要匿名代码才能呈现。这些都是简单的例子,但希望能给你一些想法。