0

I'm following django 2.1 tutorial, based on this link https://docs.djangoproject.com/en/2.1/topics/class-based-views/ Here is my Code for books/urls.py

from django.urls import path, re_path
from . import views
from book.views import BookListView

app_name = 'book'
urlpatterns = [
    path('', views.index, name = 'index')
    path('list/', BookListView.as_view(template_name="media/templates/book/book_list.html")),
]

Below is my book/views.py

from django.shortcuts import render

from .models import Author, Book, BookInstance, Genre

from django.views.generic import ListView

def index(request):
    num_books = Book.objects.all().count()
    num_instances = BookInstance.objects.all().count()
    num_instances_available = BookInstance.objects.filter(status__exact = 'a').count()
    num_author = Author.objects.count()

    context = {
        'num_books' : num_books,
        'num_instances' : num_instances,
        'num_instances_available' : num_instances_available,
        'num_author' : num_author,
    }
    return render(request, 'book/index.html', context) 

class BookListView(ListView):
        model = Book

Error I'm getting is

File "E:\DJango\mysite\book\urls.py", line 8 path('list/', BookListView.as_view(template_name="media/templates/book/book_list.html")),

^ SyntaxError: invalid syntax

2
  • 2
    Unrelated, but you shouldn't be storing templates in your media directory; those are two separate things. Commented Dec 7, 2018 at 8:56
  • @DanielRoseman thank you so much, I'm just fallowing a tutorial, in that tutorial did this... Then how should I sort them? Commented Dec 7, 2018 at 9:40

2 Answers 2

2
urlpatterns = [
    path('', views.index, name = 'index'), // missed a ,
    path('list/', BookListView.as_view(template_name="media/templates/book/book_list.html")),
]
Sign up to request clarification or add additional context in comments.

Comments

0

The standard way to store html files is to create a folder named "templates" under your Django app. You may also add another folder inside the templates folder with the same name as you app.

That way you can just call this file in your urls.py like this:

from django.urls import path, re_path
from . import views
from book.views import BookListView

app_name = 'book'
urlpatterns = [
    path('', views.index, name = 'index')
    path('list/', BookListView.as_view(template_name="your_app_name/book_list.html")),
]

If you only created the "templates" folder, you can do this:

from django.urls import path, re_path
    from . import views
    from book.views import BookListView

    app_name = 'book'
    urlpatterns = [
        path('', views.index, name = 'index')
        path('list/', BookListView.as_view(template_name="your_app_name/book_list.html")),
    ]

You may also call which template you are using inside your class based view:

class BookListView(ListView):
        model = Book
        template_name = 'your_app_name/book_list.html'

Or with only the "templates" folder:

class BookListView(ListView):
            model = Book
            template_name = 'book_list.html'

Comments

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.