I am a newbie so please be kind, working on a blog app within my django project the idea is to check if there are articles , if there is list each article. if there is not articles show no articles found
below is my view function, however when I test this it only list the first item in my list which mike, other names are not showing on the page or on source like they do not exist ??? any help with this would be great. thanks in advance
def add(request):
articles=["mike","sammy","ahmed"]
if not articles :
return HttpResponse("<body><h1>No Articles to display here</h1></body>")
else:
for article in articles:
return HttpResponse(f"{article}")
after discussing with @TimRoberts I made some changes
def add(request):
articles=["mike","sammy","ahmed"]
count = len(articles)
if count > 0:
for article in articles :
return HttpResponse(f"<li>{article}</li>")
else:
return HttpResponse("No articles found here. ")
but this is only returning the first item in the articles list. Why is it not doing a for each and iterating listing all in the response ?
iterate not listing all items in the list which python count shows as 3 its only listing mike
if not articleswill always be false. You just gave them a value. And your loop will return first time through. What are you trying to do here? How do you think you are "checking if there are articles"?HttpResponse.