【问题标题】:problem in class-based views in django SyntaxError: invalid syntaxdjango SyntaxError 中基于类的视图中的问题:语法无效
【发布时间】:2019-05-09 00:17:08
【问题描述】:

我正在关注 django 2.1 教程,基于此链接 https://docs.djangoproject.com/en/2.1/topics/class-based-views/ 这是我的书籍/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")),
]

下面是我的书/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

我得到的错误是

文件“E:\DJango\mysite\book\urls.py”,第 8 行 路径('list/', BookListView.as_view(template_name="media/templates/book/book_list.html")),

^ SyntaxError: 无效语法

【问题讨论】:

  • 无关,但您不应该将模板存储在您的媒体目录中;这是两个不同的东西。
  • @DanielRoseman 非常感谢你,我只是在学习一个教程,在那个教程中做了这个......那么我应该如何对它们进行排序?

标签: django django-class-based-views


【解决方案1】:
urlpatterns = [
    path('', views.index, name = 'index'), // missed a ,
    path('list/', BookListView.as_view(template_name="media/templates/book/book_list.html")),
]

【讨论】:

    【解决方案2】:

    存储 html 文件的标准方法是在 Django 应用程序下创建一个名为“templates”的文件夹。您还可以在模板文件夹中添加另一个与您的应用同名的文件夹。

    这样你就可以像这样在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="your_app_name/book_list.html")),
    ]
    

    如果你只创建了“templates”文件夹,你可以这样做:

    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")),
        ]
    

    您还可以在基于类的视图中调用您正在使用的模板:

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

    或者只有“模板”文件夹:

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

    【讨论】:

      猜你喜欢
      • 2016-08-20
      • 2021-09-27
      • 2011-09-19
      • 2011-09-21
      • 2017-12-29
      • 2012-10-23
      • 1970-01-01
      • 2018-05-14
      • 1970-01-01
      相关资源
      最近更新 更多