0

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

7
  • I know this is primitive but I have to start somewhere Commented Apr 27, 2021 at 3:11
  • if not articles will 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"? Commented Apr 27, 2021 at 3:11
  • Nice, I see what you are saying ok let me revise this and see if I can get it to work thank you so much @TimRoberts Commented Apr 27, 2021 at 3:13
  • 3
    If you want to return all three, then you have to create a string with the combined contents, and then pass that to HttpResponse. Commented Apr 27, 2021 at 3:13
  • I am trying to list all articles so mike, sammy and ahmed on the page but only returning mike even though i am using a for loop. but after your comment I think my code is bad I need to set the true condition action first before even hoping on else, I am going to to rewrite and will update here @TimRoberts Commented Apr 27, 2021 at 3:19

1 Answer 1

1

This view will always return the first element of the list because you return the first element in the first iteration.

Views don't generally return an HttpResponse instance, but more frequently use the render method.

This is probably what you wanted to do with your view and how it should look like.

def add(request):
  articles = ["mike", "sammy", "ahmed"]
  context = {
    "articles": articles
  }
  return render(request, "<APP_NAME>/<TEMPLATE_NAME>.html", context)

And then you can handle that in your HTML template. This way you view is a lot cleaner.

If you don't want to write the logic in the template then you can do something like this:

def add(request):
    articles = ["mike", "sammy", "ahmed"]

    context = {
      "articles": articles if articles else ["No Articles to display here"]
    }
    return render(request, "<APP_NAME>/<TEMPLATE_NAME>.html", context)

This way you don't have to write any logic in the HTML template (other than the iteration).

Your template should look like this:

<body>
  {% for article in articles %}
    {{ article }}
    <br>
  {% endfor %}
</body>

This will display each article in its own row.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for your detailed answer, I really appreciate it. its been awesome journey coming here... your answer is right on!! @Flip

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.