如問題所述,我在模板中收到兩個錯誤。這是代碼def create(request):full_content = forms.InputForm()if request.method == "POST": full_content = forms.InputForm(request.POST) if full_content.is_valid(): title = full_content.cleaned_data["title"] content = full_content.cleaned_data["content"] if full_content.clean_title():#Works full_content.create(title, content) context= { 'title' : util.get_page_name(title), 'entry' : util.get_entry(title), } return render(request, "encyclopedia/entry.html",context) #From here on its not valid: context = { 'form':full_content } return render(request, "encyclopedia/create.html", context) return render(request, "encyclopedia/create.html", { 'form':full_content})以及 forms.clean_title():def clean_title(self): title_name = self.cleaned_data.get("title") filename = f'entries/{title_name}.md' if default_storage.exists(filename): raise ValidationError("This title is already taken") return title_name當然還有 create.html:<h3>Create new entry</h3><form action="{% url 'create'%}" method="POST"> {{ form.title.errors }} {% csrf_token %} <table> {{form.as_p}} </table> <button type="submit" value="save">Save</button></form>為什么我得到兩顆子彈有什么想法嗎?:此標題已被占用此標題已被占用
1 回答

繁星點點滴滴
TA貢獻1803條經(jīng)驗 獲得超3個贊
沒必要{{ form.title.errors }}。{{form.as_p}}也是顯示錯誤。所以刪除它。
<h3>Create new entry</h3>
<form action="{% url 'create'%}" method="POST">
{% csrf_token %}
<table>
{{form.as_p}}
</table>
<button type="submit" value="save">Save</button>
</form>
添加回答
舉報
0/150
提交
取消