简体   繁体   中英

Django crispy forms is not working correctly

crispy forms doesn't work, I don't have any errors but the forms is still normal. Would like to have help on that. I don't understand I never had this probleme, thank you

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

I tried many things but didn't get any result

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})

1.In the doc , it says:

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.

Did you pasted all the html file code here? I do not see any CSS framework that the cryspy-forms doc mentioned such as bootstrap4 .

Note that, you need to add the followings to your html

see bootstrap website for 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>

Don't forget to delete your browswer history!

  1. after you fixed the css part, your form might not function properly. try replace with:
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

You may have put the 'crispy_forms' in INSTALLED_APPS but did you specify your template pack in your settings.py? eg CRISPY_TEMPLATE_PACK = 'uni_form'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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