将上下文传递给Django注册的观点 [英] Passing context to django-registration's views

查看:52
本文介绍了将上下文传递给Django注册的观点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 django-registration 与我在Github上发现的一套预制模板,用于执行两步(注册激活)工作流

I'm utilizing django-registration with a set of premade templates I found on Github for doing a two-step (registration-activation) workflow using HMAC.

我想将像我的网站名称这样的全局变量(在上下文处理器中定义)传递给django-registration发送的电子邮件.例如,发送给新注册人的激活电子邮件或更改了密码.

I want to pass global variables (defined in context-processors) like my website's name to the emails sent by django-registration. the activation email sent to a new registrant, for example, or the password change one.

问题"是我没有直接访问那些视图的权限.这就是django注册的重点,您将其路径包含在 urls.py 文件中,并且一切正常:

The "problem" is I don't directly have access to those views. That's kinda the point of django-registration, you include its path in the urls.py file, and everything works:

urlpatterns = [
    url(r'^', include('core.urls')),
    url(r'^admin/', admin.site.urls),
    url(r'^accounts/', include('registration.backends.hmac.urls')),
]

向这些视图添加上下文的最省力方法是什么?我已经创建并成功传递上下文以我自己的观点发送电子邮件(使用上下文处理器):

What's the minimum effort way of adding context to those views? I've already created and am successfully passing context to emails in my own views (using context processors):

def send_some_email_view(request):

    msg_plain = render_to_string('email_change_email.txt', context, request=request)
   msg_html = render_to_string('email_change_email.html', context, request=request)

那我没有创建的视图呢?

But what about views I didn't create?

因此,我取得了一些进展,找到了django-registration的注册视图,并在其中找到了该方法:

So I made some progress, finding django-registration's registration view, and this method inside of it:

def send_activation_email(self, user):
    """
    Send the activation email. The activation key is simply the
    username, signed using TimestampSigner.

    """
    activation_key = self.get_activation_key(user)
    context = self.get_email_context(activation_key)
    context.update({
        'user': user
    })
    subject = render_to_string(self.email_subject_template,
                               context)
    # Force subject to a single line to avoid header-injection
    # issues.
    subject = ''.join(subject.splitlines())
    message = render_to_string(self.email_body_template,
                               context)
    user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)

我不想在源代码中覆盖它,因为那样会阻止我进行更新.所以现在的问题变成了:我唯一的出路是编写一个对该视图进行子类化并覆盖该方法的视图吗?这意味着我为django-registartion提供的每个需要发送电子邮件的视图编写单独的视图...

I don't want to override it inside the source code because that would prevent me from updating. So now the question becomes: Is my only way out writing a view that subclasses this view, and overriding the method? This means I'm writing separate views for every view provided by django-registartion that needs to send an email...

推荐答案

这就是我最终要做的事情,这要归功于达伦斯的回答将我定向到:

Here's what I ended up doing, Thanks to the direction dahrens' answer sent me to:

# myapp/processors.py
def get_website_name(request):
    website_name = 'ExcitingWebsiteThatsComingSoon'
    return {'mysite_name': website_name}

# some views.py file
from myapp.processors import get_website_name

class RegistrationViewWithContext(RegistrationView):
    def get_email_context(self, user):
        context = super().get_email_context(user)
        context['req'] = get_website_name(self.request)
        return context

基本上,我只是在使用自定义处理器来注入网站的名称.它不像我希望的那样干净:虽然在模板中我可以简单地使用 {{mysite_name}} ,但是在电子邮件模板中我必须使用 {{req.mysite_name}} .但这确实具有我要实现的DRY风格:如果函数中的变量发生更改,则所有模板都会相应地更新.

Basically, I'm simply using my custom processor to inject the website's name. It isn't as clean as I hoped it would be: While in my templates I can simply use {{ mysite_name}}, in the email template I have to use {{req.mysite_name}}. But this does have the DRY-ness I aimed for: all templates updating accordingly if the variable in the function changes.

我会暂时将我的答案标记为正确,如果有任何新答案,我会相应地更新.

I'll mark my answer as correct for now and will update accordingly if any new answers come in.

这篇关于将上下文传递给Django注册的观点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆