在提交按钮“脆皮表单”上使用HTML元素和{{user.userprofile.image.url}}

路易

我希望你一切都好。您知道如何将其添加到按钮中吗?

<img src="{{user.userprofile.image.url}}" width="22px" height="22px" style="border-radius: 90px;background: #ffffff38;">

我已经尝试过,但是在我的页面上始终将其翻译为“评论”。

class CreateCommentForm(forms.ModelForm):
    def __init__(self,*args,**kwargs):
        super(CreateCommentForm, self).__init__(*args,**kwargs)
        self.helper = FormHelper()
        self.helper.form_method="post"
        self.helper.layout = Layout(
            Field("content",css_class="form-control",style="margin-bottom:10px",rows="1"),
        )
        
        self.helper.add_input(Submit('submit','<img src="{{user.userprofile.image.url}}" width="22px" height="22px" style="border-radius: 90px;background: #ffffff38;"> Comment',css_class="btn btn-sm",style="background-color: #0d6ec5;border-color: #0d6ec5;"))


    class Meta:
        model = Comment
        fields = [
            'content'
        ]

在此处输入图片说明

黄鲨

看来您正在尝试解决两个难题:在表单按钮(userprofile.image)中包含动态信息,然后具有带有图像/ HTML内容的提交按钮。

  1. 在表单中包括动态信息:您需要在表单的init方法中添加kwarg / parameter ,以便在视图中创建表单对象时可以将用户传递给它。
  2. 与其使用Crispy的Submit按钮(创建一个input元素),不如使用StrictButton它创建一个button元素的方法,并且可以用任何想要的HTML填充。我会注意到的文档StrictButton位于一个怪异的地方:https : //django-crispy-forms.readthedocs.io/en/latest/layouts.html,与各个按钮上的文档分开。还值得注意的是,您可以简单地Button以类似的方式使用该类,但是StrictButton类具有一些装饰,可以使使用Bootstrap的人更轻松。(查看crispy_forms.bootstrap模块以了解更多信息。)

这是我如何更新您的代码:

# View function:
def my_view(request):
    ...
    comment_form = CreateCommentForm(user=request.user)
    ...


# Forms:
from crispy_forms.bootstrap import StrictButton

class CreateCommentForm(forms.ModelForm):

    # Adding the user kwarg here:
    def __init__(self, user, *args, **kwargs):
        super(CreateCommentForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_method = "post"
        
        # Set up the HTML content for the StrictButton:
        button_content = f'<img src="{user.userprofile.image.url}" width="22px" height="22px" ' \
                         f'style="border-radius: 90px;background: #ffffff38;"> Comment'
                         
        self.helper.layout = Layout(
            Field("content", css_class="form-control", style="margin-bottom:10px", rows="1"),
            
            # Add the button to the layout:
            StrictButton(button_content, type='submit', css_class='btn btn-primary btn-block', id='save'),
        )

    ...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在我的base.html上显示用户图像({{userprofile.image.url}}

尝试在UserProfile对象上使用add_notification()方法,ValueError:无法分配UserProfile-Notification.user必须是User实例

如何在TemplateView中访问User和UserProfile

使用 django-allauth 在用戶註冊時創建 User 和 UserProfile

/ profiles / user-profile / UserProfile上的DidsNotExist不存在匹配查询

正则表达式后向引用对于输入是相等的:“ UserProfile”和“ user-profile”

使用%USERPROFILE%运行外部脚本

无法访问Django中模板中的UserProfile模型字段。尝试过{{user.userprofile}}

Java user.home设置为%userprofile%并且未解析

ValueError: 相关模型 'user.UserProfile' 无法解析 Django 2.1

不兼容的指针类型将User *发送到UserProfile *类型的参数

上载文件时,Django NOT NULL约束的userprofile.user_id失败

Django - 用户配置文件:唯一约束失败:main_userprofile.user_id

Django 上的 MakeMigration 错误 - ImportError:无法导入名称“UserProfile”

获取UserProfile和TimeLine时出现Tweetsharp问题

从UserProfile Table和Billing table字段自动填充视图

如何使用%userprofile%为多个用户创建RDP会话或文件?

在YAML中使用R代码或Windows用户变量(“%userprofile%”)?

如何在使用 SpringSecurity 编辑 UserProfile 后获取 ActiveUser

django rest_auth使用UserProfile扩展用户

为什么不能在%PATH%中使用%USERPROFILE%?

如何使用 rest 框架为 UserProfile 创建 API?

“ $ env:USERPROFILE”产生的结果与在文件资源管理器中使用“%USERPROFILE%”的结果不同

为什么background-image:url()替换url上的斜杠?

使用 URL + GET 参数填写表单按钮而不提交表单

使用 Image url 中的 ViewPager- 崩溃

使用JSoup从URL获取og:image

我的 UserProfile 模型表单拒绝使用用户 ID 更新和填写 updateview 类中的表单:

尝试使用Promises和.then()从Firebase获取Image-Url