简体   繁体   English

Django Crispy 表单不显示引导程序/css 或按钮

[英]Django Crispy forms not showing bootstrap/css or button

I got crispy forms working with my model, though the form looks plain and bootstrap not showing up, also there seems to be no button.我的模型使用了脆脆的表格,虽然表格看起来很简单,引导程序没有出现,但似乎也没有按钮。 Even when adding button and clicking it(it refreshes) no data has been saved to the database.即使添加按钮并单击它(刷新),也没有数据保存到数据库中。 I have tried many ways.我尝试了很多方法。 What seems to be wrong?似乎有什么问题? Any help would be highly appreciated.任何帮助将不胜感激。

forms.py表格.py

   class PlotForm(forms.ModelForm):
    helper = FormHelper()
    helper.form_tag = False
    helper.form_method = 'POST'

    class Meta:
        model = Plot
        fields = '__all__'

views:意见:

def plot_form(request):
    return render(request, 'plot_form.html', {'form': PlotForm()})

the html: html:

{% load crispy_forms_tags %}
    <form action="" method="POST">
   {% crispy form %}
        <input type="submit" class="btn btn-default" value="save">

First the {% csrf_token %} is missing.首先{% csrf_token %}丢失。 Second If I remember it correct you need to use {{ form|crispy }} to load the form.第二如果我没记错的话,你需要使用{{ form|crispy }}来加载表单。

And third I would recommend to use Widget Tweaks第三,我建议使用Widget Tweaks

<form method='POST' action="/" enctype='multipart/form-data'>
 {% load widget_tweaks %}
 {% csrf_token %}
 {{ form.first_name |add_class:"customCSS1 customCSS2" }}
 {{ form.second_name |add_class:"customCSS3 customCSS4" }}
</form>
{{ form.media.js }}

with this plugin you can style the form as you wish.使用此插件,您可以根据需要设置表单样式。 All Css classes work.所有 CSS 类都有效。 Crispy is nice but you have get into the documentation and there are always some workarounds you need to do when you want to style the form. Crispy 很好,但是您已经进入了文档,并且当您想要设置表单样式时,总会有一些解决方法。 With Widget Tweaks you can simply apply any CSS class.使用 Widget Tweaks,您可以简单地应用任何 CSS 类。 When you really know your way around with crispy you can do a lot but to get to that point.... I switched at some Point and now everything works like a charm当你真正了解脆皮的方式时,你可以做很多事情,但要做到这一点......我在某个点切换,现在一切都像魅力一样

Hope that helps if not leave a comment :)如果不发表评论,希望能有所帮助:)

Edit编辑

I just saw something in your views.py.我刚刚在你的 views.py 中看到了一些东西。 You are not referencing the form correct as far as I can tell.据我所知,您没有引用正确的表格。

from appName.forms import PlotForm

def plot_form(request):
  form = PlotForm(request.POST or None, request.FILES or None) #request files is only required when you want to upload a file
  if form.is_valid():
    instance = form.save(commit = False)
    ...
    instance.save()
    #messages.success(request, 'form was saved') #optional
  context = {
  'form':form,
  }
  return render(request, 'AppName/plot_form.html', context)

Maybe that will do the trick.也许这会奏效。 You did not have a form validation and Im not sure if the "()" at {'form': PlotForm()} would break the code.您没有表单验证,我不确定{'form': PlotForm()}处的“()”是否会破坏代码。

I also faced the same issue.我也面临同样的问题。 Solved it by testing it on firefox instead of google chrome.通过在 Firefox 而不是谷歌浏览器上测试它来解决它。 Apparently, Chrome does not load CSS style for a few local web apps.显然,Chrome 不会为一些本地网络应用加载 CSS 样式。 Got it to work after following this hack.遵循此 hack 后,它就可以工作了。 https://css-tricks.com/new-in-chrome-css-overview/ https://css-tricks.com/new-in-chrome-css-overview/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM