首页 \ 问答 \ 使用Django模板标签的Mako模板(Mako templates using Django template tags)

使用Django模板标签的Mako模板(Mako templates using Django template tags)

我们的Django站点使用Mako模板构建。 我们想使用名为django-socialregistration的第三方项目,但其模板标签使用Django的模板。 如果我们使用Django模板,我们可以

{% load facebook_tags %}
{% facebook_button %}
{% facebook_js %}

我怎样才能在Mako做同样的事情? 你可以在Mako中将python内联,但我还没有想出如何这样做。

最终修复

<%! from django.template import Template, Context %>
<% tpl = "{% load facebook_tags %}{% facebook_button %}{% facebook_js %}" %>
${Template(tpl).render(Context(dict_=dict(request=request)))}

Our Django site is built using Mako templates. We want to use a third party project called django-socialregistration, but its template tags use Django's templates. If we used Django templates we could just

{% load facebook_tags %}
{% facebook_button %}
{% facebook_js %}

How can I do the same thing in Mako? You can inline strait up python in Mako, but I haven't figured out how to do it that way either.

Final Fix

<%! from django.template import Template, Context %>
<% tpl = "{% load facebook_tags %}{% facebook_button %}{% facebook_js %}" %>
${Template(tpl).render(Context(dict_=dict(request=request)))}

原文:https://stackoverflow.com/questions/3790854
更新时间:2021-08-24 09:08

最满意答案

我想你将使用Django的默认用户模型作为OneToOneField与您的自定义用户模型。 用户模型不需要其他序列化程序。 由于额外的字段将在CustomUser中,因此仅为自定义用户编写序列化程序就足够了。

from django.contrib.auth.models import User
class CustomUser(models.Model):
    user = models.OneToOneField(User)
    # Add your extra fields here.

这是你如何做到的:

CustomUserSerializer(serializers.ModelSerializer):
    username = Serializers.CharField(source='user.username')
    email = Serializers.EmailField(source='user.email')

    class Meta:
        model = CustomUser
        fields = ('username', 'email', 'extra_fields_from_CustomUser',)

值得庆幸的是, source允许您轻松访问OneToOneField,以便您不需要任何其他内容。


I guess you're going to use Django's default User model as an OneToOneField with your custom User Model. You don't need another serializer for User model. Since extra fields will be in CustomUser, it's enough to write a serializer for only Custom User.

from django.contrib.auth.models import User
class CustomUser(models.Model):
    user = models.OneToOneField(User)
    # Add your extra fields here.

This is how you do it:

CustomUserSerializer(serializers.ModelSerializer):
    username = Serializers.CharField(source='user.username')
    email = Serializers.EmailField(source='user.email')

    class Meta:
        model = CustomUser
        fields = ('username', 'email', 'extra_fields_from_CustomUser',)

Thankfully, source lets you to reach your OneToOneField easily so that you don't need anything else.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。