Answer the question
In order to leave comments, you need to log in
What is wrong with Django?
from 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
from 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')
]
django.core.exceptions.ImproperlyConfigured: No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model.
Answer the question
In order to leave comments, you need to log in
I suspect that there is no url to view the model. After creation, there is nowhere to redirect
In urls.py, if functions are used in views, you need to knock to functions through the views file itself. And it is not necessary to import each method from views.
For example:
path('/', views.by_rubric, name = 'by_rubric'),
what is there = "you have" - ....
.... this is as an option, working
models.py
def get_absolute_url(self):
return reverse("app_detail", kwargs={'slug': self.slug})
path('<slug:slug>/', views.app_detail, name='app_detail'),
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question