Answer the question
In order to leave comments, you need to log in
How to fix update/delete data error via generics (django)?
It is not possible to delete or update data through the default junga generics. Please give me a specific answer on how to solve this. Thank you in advance.
Files:
models.py
from django.db import models as md
class City(md.Model):
name = md.CharField(max_length=150, unique=True, verbose_name='Город')
def __str__(self):
return f'{self.name}'
class Meta:
verbose_name = 'Город'
verbose_name_plural = 'Города'
ordering = ['name']
from cities.models import City
from cities.forms import NewCity
from django.views.generic import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls.base import reverse_lazy
class IndexView(ListView):
model = City
context_object_name = 'cities'
template_name = 'cities/index.html'
class City(DetailView):
queryset = City.objects.all()
context_object_name = 'city'
template_name = 'cities/city.html'
class CreateCity(CreateView):
model = City
form_class = NewCity
template_name = 'cities/create.html'
success_url = reverse_lazy('cities:index')
class UpdateCity(UpdateView):
model = City
form_class = NewCity
template_name = 'cities/update.html'
success_url = reverse_lazy('cities:index')
class DeleteCity(DeleteView):
model = City
template_name = 'cities/delete.html'
success_url = reverse_lazy('cities:index')
from django.urls import path
from cities import views
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('city/<int:pk>', views.City.as_view(), name='city'),
path('create/', views.CreateCity.as_view(), name='create'),
path('update/<int:pk>', views.UpdateCity.as_view(), name='update'),
path('delete/<int:pk>', views.DeleteCity.as_view(), name='delete'),
]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question