模板渲染期间发生错误 在模板/app/templates/base.html中,第13行出现错误
“设置”对象不可逆
3 <html>
4 <head>
5 <title>Django blog</title>
6 <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400" rel="stylesheet">
7 <link href="{% static 'css/base.css' %}" rel="stylesheet">
8 </head>
9 <body>
10 <div>
11 <header>
12 <div class="nav-left">
13 <h1><a href="{% url 'home' %}">Django blog</a></h1>
14 </div>
15 <div class="nav-right">
16 <a href="{% url 'post_new' %}">+ New Blog Post</a>
17 </div>
18 </header>
19 {% if user.is_authenticated %}
20 <p>Hi {{ user.username }}!</p>
21 <p><a href="{% url 'logout' %}">Log out</a></p>
22 {% else %}
23 <p>You are not logged in.</p>
我正在学习使用2.1.5的《 Django初学者》一书的第7章:用户帐户。它可以在本地主机上工作。它在错误的云端(Heroku)上无法运作。它说“设置”对象是不可逆的。我使用了以下版本。 django =“ == 2.1.5”,gunicorn =“ == 19.9.0”,whitenoise =“ == 3.3.1”,python_version =“ 3.7”
# blog/urls.py
from django.urls import path
from .views import (
BlogListView,
BlogDetailView,
BlogCreateView,
BlogUpdateView,
BlogDeleteView, #new
)
urlpatterns = [
path('post/<int:pk>/delete/', BlogDeleteView.as_view(), name='post_delete'), # new
path('post/<int:pk>/edit/', BlogUpdateView.as_view(), name='post_edit'),
path('post/new/', BlogCreateView.as_view(), name='post_new'),
path('post/<int:pk>/', BlogDetailView.as_view(), name='post_detail'),
path('', BlogListView.as_view(), name='home'),
]
# blog/views.py
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView # new
from django.urls import reverse_lazy #new
from .models import Post
class BlogListView(ListView):
model = Post
template_name = 'home.html'
class BlogDetailView(DetailView):
model = Post
template_name = 'post_detail.html'
class BlogCreateView(CreateView):
model = Post
template_name = 'post_new.html'
fields = '__all__'
class BlogUpdateView(UpdateView): # new
model = Post
template_name = 'post_edit.html'
fields = ['title', 'body']
class BlogDeleteView(DeleteView): # new
model = Post
template_name = 'post_delete.html'
success_url = reverse_lazy('home')