简体   繁体   中英

KeyError in Django after changing action in form

When I submit the form I get a Key error, before it was getting the key error but it was not saving the entry. I changed the action in the form, an now i get Key error. I read some answers here about key error but I could not find the problem. Could somebody help me, please?

in HTML

<h1>Create New Page:</h1>
        <form action="{% url 'create' %}" method="post">
            {% csrf_token %}
            {{ form.as_p}}
            
            <button id='saveButton' >Save!</button>
        </form>

in Vews:

class NewEntryForm(forms.Form):
    title = forms.CharField(label="Title")
    Content = forms.CharField(widget=forms.Textarea)
def create(request):
    if request.method == "POST":
        form = NewEntryForm(request.POST)
        if form.is_valid():
            title = form.cleaned_data["title"]
            content = form.cleaned_data["content"]
            util.save_entry(title,content)
        else:
            return render(request, "encyclopedia/create.html",{"form": form
        })
    else:
        return render(request, "encyclopedia/create.html",{"form": NewEntryForm()

        })

Your form must contain a submit button

<input type="submit" value="Save!" />

Or the id "saveButton" must trigger a submitForm in JQuery or JavaScript but in that case it is better to have an id on the form to be able to point on it in JavaScript

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