M
M
Maxim Shelkov2020-04-24 01:28:42
Django
Maxim Shelkov, 2020-04-24 01:28:42

What is wrong with Django?

Views code
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


Urls code
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')
]


Code Models
from 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']


Code Forms

from django.forms import ModelForm

from .models import Bb

class BbForm(ModelForm):
class Meta:
model = Bb
fields = {'title', 'content', 'price', 'rubric'}


Mistake
django.core.exceptions.ImproperlyConfigured: No URL to redirect to.  Either provide a url or define a get_absolute_url method on the Model.

Be grateful for help

Answer the question

In order to leave comments, you need to log in

4 answer(s)
G
galaxy, 2020-04-24
@galaxy

I suspect that there is no url to view the model. After creation, there is nowhere to redirect

A
Alisa Unylova, 2020-04-24
@alisadvachee

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'),

S
siarheisiarhei, 2020-04-25
@siarheisiarhei

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})

urls.py
path('<slug:slug>/', views.app_detail, name='app_detail'),

D
DedForget, 2020-12-25
@DedForget

My friend, I am also reading this book.
Found a solution on
Stack in urls.py write:

.....

urlpatterns = [
    path('add/', BbCreateView.as_view(success_url="/bboard/"), name='add'),
   .....
]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question