Возникла ошибка при изучении книги "Практика создания веб-сайтов на Python" от Владимира Дронова
Ошибку показывает в views.py ... def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['rubrics'] = Rubric.objects.all() return context
views.py
from django.shortcuts import render
from django.views.generic.edit import CreateView
from .forms import BbForm
from .models import Bb
from .models import Rubric
class BbCreateView(CreateView):
template_name = 'bboard/create.html'
from_class = BbForm
success_url = '/main/'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['rubrics'] = Rubric.objects.all()
return context
def by_rubric(request,rubric_id):
bbs = Bb.objects.filter(rubric = rubric_id)
rubrics = Rubric.objects.all()
current_rubric = Rubric.objects.get(pk = rubric_id)
context={'bbs':bbs,'rubrics':rubrics,'current_rubric':current_rubric}
return render(request,'bboard/by_rubric.html',context)
def index(request):
bbs = Bb.objects.all()
rubrics = Rubric.objects.all()
context = {'bbs':bbs,'rubrics':rubrics}
return render(request, 'bboard/index.html', context)
urls.py
from django.urls import path
from .views import index,by_rubric
from .views import BbCreateView
urlpatterns = [
path('add/',BbCreateView.as_view(),name = 'add'),
path('<int:rubric_id>/', by_rubric, name = 'by_rubric'),
path('',index, name = 'index')
]
forms.py
from django.forms import ModelForm
from .models import Bb
class BbForm(ModelForm):
class Meta:
model = Bb
fields = ('title','content','price','rubric')
create.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content = "text/html; charset=utf-8">
<title>Add Ads - Ads</title>
</head>
<body>
<h1>Add Ads</h1>
<div>
<a href="{% url 'index' %}">Main</a>
<a href="{% url 'add' %}">Add</a>
{% for rubric in rubrics %}
<a href="{% url 'by_rubric' rubric.pk %}">{{ rubric.name }}</a>
{% endfor %}
</div>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Add">
</form>
</body>
</html>