I am trying to define a function in order to create a new page in a wiki application. The user should provide a title and content. If the title already exists in the list, the user should get an error message. If not, the new entry should be saved in the list with its content.
Here is my code so far:
def new_page(request):
if request.method == "POST":
title = request.POST.get("title")
entries = util.list_entries() #this function is already defined and it returns a list of all names of encyclopedia entries.
for entry in entries:
if title.lower() == entry.lower():
return HttpResponse(f"ERROR: {entry} Already Exists")
else:
return render(request,"encyclopedia/new_page.html")
The above code works fine, when I type in an existing title I get the error message.
The problem starts when I add an else condition. Here's an example (for just trying out the code, I don't want the content to be saved yet.)
def new_page(request):
if request.method == "POST":
title = request.POST.get("title")
entries = util.list_entries()
for entry in entries:
if title.lower() == entry.lower():
return HttpResponse(f"ERROR: {entry} Already Exists")
else:
return HttpResponse("Thank you for your contribution!")
else:
return render(request,"encyclopedia/new_page.html")
Now, even if I type in an existing title, I get "Thank you for your contribution!". I couldn't figure out what may be causing this problem. Can you please help?
new_page.html:
{% extends "encyclopedia/layout.html" %}
{% block title %}
Create New Page
{% endblock %}
{% block body %}
<h1>Create Your Entry</h1>
<form action="{% url 'new_page' %}" method="POST">
{% csrf_token %}
<input type="text" name="title" placeholder="Title" style="width: 200px; text-align: center;">
<textarea name="new_entry" id="new_entry" cols="30" rows="10"></textarea>
<input type="submit" name="save_entry" id="save_entry" value="Save">
</form>
{% endblock %}
CodePudding user response:
You should save in a variable whether the page already exists, like the following:
def new_page(request):
if request.method == "POST":
title = request.POST.get("title")
entries = util.list_entries()
pageExists=False
for entry in entries:
if title.lower() == entry.lower():
pageExists=True
if pageExists:
return return HttpResponse(f"ERROR: {entry} Already Exists")
else:
return HttpResponse(f"ERROR: {entry} Already Exists")
else:
return render(request,"encyclopedia/new_page.html")
CodePudding user response:
The return
causes the function to exit in the first iteration of the loop that is not a hit. You have to move the return
statement two levels up like this:
def new_page(request):
if request.method == "POST":
title = request.POST.get("title")
entries = util.list_entries()
for entry in entries:
if title.lower() == entry.lower():
return HttpResponse(f"ERROR: {entry} Already Exists")
# the loop is finished and did not find an existing entry, return success
return HttpResponse("Thank you for your contribution!")
else:
return render(request,"encyclopedia/new_page.html")
CodePudding user response:
The function exits because of the return statement in the else in the for-loop.