1 year ago
#380524
Damian Keretić
Django DeleteView strange behavior
So I have a problem with a Django class-based Delete View. Actually, the delete view works just fine as it deletes the chosen entry, but the problem occurs when I want to redirect to the DetailView URL of the page where all entries are.
class DeleteStavkeView(DeleteView):
model = Stavke
template_name = 'app/radni_nalozi/stavke_delete.html'
def get_success_url(self):
rn_id = self.object.Rn_id
cijena_stavke_del = self.object.Cijena
### UZIMANJE TRENUTNE CIJENE NA TOM RADNOM NALOG ###
trenutna_cijena = Radni_nalozi.objects.values_list('Cijena').filter(id=rn_id)
trenutna_cijena = trenutna_cijena[0][0]
### UMANJUJEMO TRENUTNU CIJENU ZA CIJENU STAVKE KOJU BRIŠEMO
nova_cijena_rn = trenutna_cijena - cijena_stavke_del
### POSTAVLJAMO NOVU CIJENU
rn_cijena = Radni_nalozi.objects.get(id=rn_id)
rn_cijena.Cijena = nova_cijena_rn
rn_cijena.save()
### REDIRECTAMO VIEW
return reverse_lazy('detalji_rn', kwargs={'pk': rn_id})
As you can see in the code above, I am using reverse lazy with a pk of the page before. But after I confirm to delete it seems like Django is trying to get to the same delete URL once again and I am always getting the same error that URL is not found.
It seems like it's trying to get to the URL of the entry that was just deleted so obviously there is none found. I can't understand what am I doing wrong? Please help.
urlpatterns = [
...
path('radni_nalozi/details_rn/<pk>', views.RadniDetailView.as_view(), name="detalji_rn"),
path('radni_nalozi/update/<pk>', views.RadniUpdateView.as_view(), name="update_rn"),
path('radni_nalozi/delete/<pk>', views.RadniDeleteView.as_view(), name="delete_rn"),
path('radni_nalozi/add_stavke/<pk>', views.AddStavkeView.as_view(), name="add_stavke_rn"),
path('radni_nalozi/delete_stavke/<pk>', views.DeleteStavkeView.as_view(), name="delete_item"),
EDIT [7.4.22.]
To make it clear. I have a list view of all the work orders. When I pick one of the work orders I go to the detail view of that work order. Every work order contains various items attached to him by a foreign key (items that need to be done on that work order). The problem occurs when I delete one of the items on that work order. I want to go back to the detail page of that work order not the list view of all the work orders. So what I want to do is to get a pk of that work order and go back to the refreshed detail page of that work order after I delete one of the items attached to him.
Here you can see my full views.py
class Radni_nalozi_view(ListView):
model = Radni_nalozi
template_name = 'app/radni_nalozi/radni.html'
def get_queryset(self):
return Radni_nalozi.objects.filter(Aktivan=1)
def get_context_data(self, **kwargs):
context = super(ListView, self).get_context_data(**kwargs)
context['title'] = 'RADNI NALOZI'
context['link'] = 'dodaj_rn'
return context
############################# DETAIL VIEW ###########################################
class RadniDetailView(DetailView):
# specify the model to use
model = Radni_nalozi
template_name = 'app/radni_nalozi/detalji_rn.html'
def get_context_data(self, **kwargs):
context = super(RadniDetailView, self).get_context_data()
radni_obj = self.object # this contain the object that the view is operating upon
context['object'] = radni_obj # don't forget this also
# Get all items/Stavke related to the work order/Radni_nalozi
context['items'] = Stavke.objects.filter(Rn=radni_obj)
context['entries'] = Unosi.objects.filter(Rn=radni_obj)
context['phases'] = Unosi_faza.objects.filter(Rn=radni_obj)
return context
class AddStavkeView(BSModalCreateView):
model = Stavke
template_name = 'app/radni_nalozi/dodaj_rn.html'
form_class = StavkeForm
def get_context_data(self, **kwargs):
context = super(BSModalCreateView, self).get_context_data(**kwargs)
context['title'] = 'DODAVANJE STAVKI NA RADNI NALOG'
context['gumb'] = 'DODAJ'
### POVLAČENJE TRENUTNE CIJENE PREMA DODANIM STAVKAMA ###
rn_cijena = Stavke.objects.values_list('Cijena').filter(Rn_id=self.kwargs['pk'])
global cijena
cijena = 0
for i in range(len(rn_cijena)):
cijena = cijena + (rn_cijena[i][0])
return context
# DODAJ NOVE STAVKE I STAVI DA JE RN_ID JEDNAK PRIMARNOM KLJUČU
def form_valid(self, form):
form.instance.Rn_id = self.kwargs['pk']
### POVLAČENJE UPISANIH KOMADA I ODABRANOG ARTIKLA SA FORME
kom = form.cleaned_data['Komada']
get_artikl = form.cleaned_data['Artikl']
### POVLAČENJE TRENUTNIH CIJENA ARTIKALA I DODAVANJE NA STAVKE (pridodavanje aktualnih cijena radnom nalogu)
get_artikl = str(get_artikl).split(", ")
print(get_artikl)
artikl_cijena = Artikli.objects.values_list('Cijena').filter(Artikl=get_artikl[1])
artikl_cijena = artikl_cijena[0][0]
print(artikl_cijena)
### RAČUNANJE CIJENA PREMA BROJU KOMADA I AKTUALNIM CIJENAMA ARTIKALA I SPREMANJE U BAZU
stavka_cijena = artikl_cijena * kom
form.instance.Cijena = stavka_cijena
nova_cijena_rn = stavka_cijena + cijena
rn_cijena = Radni_nalozi.objects.get(id=self.kwargs['pk'])
rn_cijena.Cijena = nova_cijena_rn
rn_cijena.save()
return super(AddStavkeView, self).form_valid(form)
def get_success_url(self):
### NAKON USPJEŠNOG SUBMITANJA REDIRECTAJ NA NEKI DRUGI URL
rn_id=self.kwargs['pk']
return reverse_lazy('detalji_rn', kwargs={'pk': rn_id})
class DeleteStavkeView(DeleteView):
model = Stavke
template_name = 'app/radni_nalozi/stavke_delete.html'
def get_success_url(self):
### UZIMANJE ID-a radnog naloga i CIJENE STAVKE KOJU BRIŠEMO SA TOG RN-a
rn_id = self.object.Rn_id
cijena_stavke_del = self.object.Cijena
### UZIMANJE TRENUTNE CIJENE NA TOM RADNOM NALOG ###
trenutna_cijena = Radni_nalozi.objects.values_list('Cijena').filter(id=rn_id)
trenutna_cijena = trenutna_cijena[0][0]
### UMANJUJEMO TRENUTNU CIJENU ZA CIJENU STAVKE KOJU BRIŠEMO
nova_cijena_rn = trenutna_cijena - cijena_stavke_del
### POSTAVLJAMO NOVU CIJENU
rn_cijena = Radni_nalozi.objects.get(id=rn_id)
rn_cijena.Cijena = nova_cijena_rn
rn_cijena.save()
### REDIRECTAMO VIEW
return reverse_lazy('detalji_rn', kwargs={'pk': rn_id})
python
django
reverse
django-urls
django-class-based-views
0 Answers
Your Answer