Home > Enterprise >  Why do I need to specify HTML file in render()
Why do I need to specify HTML file in render()

Time:09-21

Why do I need to give html file name in render() - I have already set url in my project file in urls.py in django

urls.py

url('view-books',views.viewBooks)

views.py

def viewBooks(request):
    books=models.Book.objects.all()
    res=render(request,'BRMapp/view_book.html',{'books':books})

Why can I not give in render view-books?

CodePudding user response:

i think you have typo

def viewBooks(request):
    books=models.Book.objects.all()
    context = {"books":book}
    return render(request,'BRMapp/view_book.html',context)

your question why you need html file name in render because render is a function it takes 3 arguments 1st is request second is "path of the html file" 3rd is the context

further explaination

Do you have basic idea how django work first of first you are not giving url in render you are giving path to render which template should be render . django follow mvc pattern you read on it but to simplify it urls just have the routing task they are just there to filter routes not to do any thing in url you can give 3 arguments two are compulsary first the path by which it recognize that the time has come to act the second the function name which direct him where to go then its function responsibilty to process the data

CodePudding user response:

Unfortunately, you didn't return anything in your view. So you need to add return to your function:

def viewBooks(request):
    books=models.Book.objects.all()
    return render(request,'BRMapp/view_book.html', {'books':books})

You might want to take a look at this tutorial.

CodePudding user response:

https://yourwebsite/view-book is not the same as BRMapp/view_book.html, Django needs to know that one corresponds to the other.

The routing in Django works like this:

  1. The user sends a request to Django with a url.
  2. Django looks through your urls in urls.py for a path that matches what was requested.
  3. When it finds a path, like view-books, that path has a view. The view is just a function (viewBooks()), and Django executes it.
  4. The view function is expected to return the content that the user will see. You could, if you wanted, write the whole page by hand as a string in the return line of viewBooks(), but that's inconvenient, so instead you tell Django to make the page for you, starting from a template. To do so, you call render().
  5. What render() does is take the template and replace all parts that need to be replaced for the user that will see it. But to know what the initial content will look like, it needs to read it from somewhere, and that's the HTML file BRMapp/view_book.html.

The HTML file doesn't need to have the same name as the view, you could have called it foobar.html and it would have worked the same. But regardless of its name, you need to tell Django that you want to use a file (render() tells Django that), and you need to tell Django where that file is. You'll have many different files in different places with different names, and it can happen that you have the same name for templates in different directories, so Django will not attempt to guess which one you want: you'll have to put its path inside render() so that Django knows where to start building the page.

If you gave the URL to render() instead of the path to the file, Django would get to point 5 and then back again to 1 to figure out what that URL means, and so on and so forth forever.

  • Related