Django crispy forms无法正常工作

2024-05-29 03:30:11 发布

您现在位置:Python中文网/ 问答频道 /正文

crispy表单不起作用,我没有任何错误,但表单仍然正常。我想在这方面得到帮助。我不明白我从来没有遇到过这个问题谢谢

我在已安装的应用程序中有'crispy_forms',并且安装正确

我尝试了很多东西,但没有得到任何结果

newhome.html

{% load crispy_forms_tags %}
{% load static %}

{% block content %}

<link rel="stylesheet" type="text/css" href="{% static 'pools/newmain.css' %}">

<!-- Load an icon library to show a hamburger menu (bars) on small screens -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<div class="topnav" id="myTopnav">
  <a href="#home" class="active">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
  <a href="javascript:void(0);" class="icon" onclick="myFunction()">
    <i class="fa fa-bars"></i>
  </a>
</div>



<div class="content-section">
  <form method="POST" action="">
    {% csrf_token %}
    {{ form|crispy }}
    <button class="btn btn-outline-info" type="submit">Send</button>
  </form>

</div>

{% endblock %}

newmain.css

body {
    background-color: coral;
}

/* Add a black background color to the top navigation */
.topnav {
  background-color: #333;
  overflow: hidden;
}

/* Style the links inside the navigation bar */
.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

/* Change the color of links on hover */
.topnav a:hover {
  background-color: #ddd;
  color: black;
}

/* Add an active class to highlight the current page */
.topnav a.active {
  background-color: #4CAF50;
  color: white;
}

/* Hide the link that should open and close the topnav on small screens */
.topnav .icon {
  display: none;
}

views.py

def newhome(request):
    form = NewTask

    if request.method == "POST":
        form = NewTask(request.POST)
        if form.is_valid():
            form.save()
            task = form.cleaned_data["title"]
            print(task)

        else:
            form = NewTask()

        return render(request, "pools/newhome.html", {"form": form})
    return render(request, "pools/newhome.html", {"form": form})


Tags: thetextdivformrequesthtmllinkcss
2条回答

1.在doc中,它说:

crispy-forms does not include static files. You will need to include the proper corresponding media files yourself depending on what CSS framework (Template pack) you are using. This might involve one or more CSS and JS files. Read CSS framework’s docs for help on how to set it up.

你把所有的html文件代码都粘贴在这里了吗?我没有看到cryspy表单文档提到的任何CSS框架,例如bootstrap4

注意,您需要将以下内容添加到html

有关cdn2,请参见引导程序网站


<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

别忘了删除浏览历史记录

  1. 修复css部分后,表单可能无法正常运行。尝试替换为:
def newhome(request):
    if request.method == "POST":
        form = NewTask(request.POST)
        if form.is_valid():
            task = form.cleaned_data["title"] # this is good practice because later you might want to add things like user = request.user before saving the form.
            form.save()
            print(task)
        else:
            print(form.errors) 
    else:
        form = NewTask()

    return render(request, "pools/newhome.html", {"form": form})

I have 'crispy_forms' in my INSTALLED_APPS and it correctly installed

您可能已将“crispy_表单”放入已安装的_应用程序中,但您是否在settings.py中指定了模板包?e、 g.CRISPY_TEMPLATE_PACK = 'uni_form'

相关问题 更多 >

    热门问题