Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

python - How do I solve form not submiting in django

I have created a form without using the form model, but the form wouldn't submit any data and I dont get any error message.

The html file

    
    <form action="{% url 'wiki:add_test' %}" method="post">
        {% csrf_token %}
        <div class="input_form">
            <label class ="label_title" for="entry_title">Title:</label>
            <input class="entry_title" type="text" name="entry_title" placeholder="Title">   
        </div>
        
        <br>
        <div class="input_form">
            <div class="input_label"><label for="entry_content">Content:</label></div>
            <div class="input_field">
                <textarea class="entry_content" name="entry_content" id="" cols="5" rows="2"></textarea>
            </div>
        </div>
        
        <div class="input_form">
            <div class="title"></div>
            <div class="input_button"><input type="button" value="Add Entry"></div>
        </div>
    </form>

The views.py

new_entry = ["Item1", "Item 2"]
def test1(request):
    return render(request, "encyclopedia/display_test.html",{
        "lists": new_entry
    })

def add_test(request):
    if request.method == "POST":
        form1 = request.POST
        print(request.POST['entry_title'])
        list_item = form1['entry_title']
        new_entry.append(list_item)
        return HttpResponseRedirect(reverse("wiki:test1")) 
    return render(request, "encyclopedia/add.html")

The urls.py

from django.urls import path

from . import views

app_name = "wiki"
urlpatterns = [
    path("", views.index, name="index"),
    path("test1", views.test1, name="test1"),
    path("add_test", views.add_test, name="add_test"),
]

I need help to know where I went wrong

question from:https://stackoverflow.com/questions/65848266/how-do-i-solve-form-not-submiting-in-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your form is not submitting because you haven't set the submit buttons type to submit in your html.
change

<div class="input_button"><input type="button" value="Add Entry"></div>

to

<div class="input_button"><input type="submit" value="Add Entry"></div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...