Код Viеwsfrom django.http import HttpResponse
from django.shortcuts import render
from .models import Rubric
from django.views.generic.edit import CreateView
from django.urls import reverse_lazy
from .models import Bb
from .forms import BbForm
def index(request):
bbs = Bb.objects.all()
rubrics = Rubric.objects.all()
context = {'bbs' : bbs,
'rubrics' : rubrics
}
return render(request, 'bboard/index.html', 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/index.html', context)
class BbCreateView(CreateView):
template_name = 'bboard/create.html'
form_class = BbForm
succes_url = reverse_lazy('index')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["rubris"] = Rubric.objects.all()
return context
Код Urlsfrom django . urls import path
from .views import index, by_rubric, BbCreateView
urlpatterns = [
path('add/', BbCreateView.as_view(), name = 'add'),
path('<int:rubric_id>/', by_rubric, name = 'by_rubric'),
path('', index, name = 'index')
]
Код Modеlsfrom django.db import models
class Bb(models.Model):
title = models.CharField(max_length = 50, verbose_name = 'Товар')
content = models.TextField(null = True, blank = True, verbose_name = 'Описание')
price = models.FloatField( null=True, blank = True, verbose_name = 'Цена')
published = models.DateTimeField(auto_now_add=True, db_index=True, verbose_name = 'Опyбликованно')
rubric = models.ForeignKey('Rubric', null = True, on_delete = models.PROTECT, verbose_name = 'Рубрика')
class Meta:
verbose_name_plural = 'Объявления'
verbose_name = 'Объявление'
ordering = ('-published', '-published')
class Rubric(models.Model):
name = models.CharField(max_length = 40, db_index = True, verbose_name = 'Название')
def __str__(self):
return self.name
class Meta:
verbose_name_plural = 'Рубрики'
verbose_name = 'Рубрика'
ordering = ['name']
Код Forms
from django.forms import ModelForm
from .models import Bb
class BbForm(ModelForm):
class Meta:
model = Bb
fields = {'title', 'content', 'price', 'rubric'}
Ошибка
django.core.exceptions.ImproperlyConfigured: No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model.
Бyдy благодарен помощи