2

我项目中的所有模板都有一个注册按钮,只有当您以匿名用户身份输入时才能看到该按钮。该按钮打开一个带有两个注册表单的模式窗口。问题是我必须在我的所有视图中都有这个代码才能工作。有什么方法可以避免在我所有的视图中重复与注册表相关的代码?

澄清:在我的所有观点中重复的代码被注释为#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))
4

2 回答 2

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)
于 2013-03-14T03:40:38.987 回答
1

您至少有三个选项可以在多个视图中包含代码:包含标记、上下文处理器和模板继承。这仅取决于哪一个对您的设置最有意义。

上下文处理器可能是将条件逻辑排除在模板之外的最佳选择,因为您需要检查用户是否是匿名的,您可以在处理器中执行服务器端操作:

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 }}

如果填充了该变量,则需要在其中呈现仅匿名代码。这些都是简单的例子,但希望能给你一些想法。

于 2013-03-14T03:20:57.953 回答