Issue
I've just learned CreateView UpdateView and DeleteView models and then I got all of them working. I can change my object I can delete them, but after deleting an object I try to create another and creates one like the before hasn't been deleted. giving me the following pk of the deleted before
it still creates de object although the pk at the moment should be 3, I think after I click the delete button and confirm delete it isn't deleting from data.
These are my models:
from django.db import models from django.urls import reverse
class Dreams (models.Model):
titulo = models.CharField(max_length=100)
objetivo = models.CharField(max_length=100)
imagem = models.CharField(max_length=100)
def get_absolute_url(self):
return reverse ('webdeve:index', kwargs={'pk': self.pk})
def __str__(self):
return self.titulo + ' - ' + self.objetivo
class Which (models.Model):
lets = models.ForeignKey(Dreams, on_delete=models.CASCADE)
make = models.CharField(max_length=100)
it = models.CharField(max_length=100)
favorite = models.BooleanField(default=False)
def __str__(self):
return self.make
my views.py:
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from .models import Dreams, Which
from django.urls import reverse_lazy
class IndexView (generic.ListView):
template_name = 'index.html'
def get_queryset(self):
return Dreams.objects.all()
class DetailView (generic.DetailView):
model = Dreams
template_name = 'detail.html'
class DreamCreate (CreateView):
model = Dreams
fields = ['titulo', 'objetivo', 'imagem']
template_name = 'dreams_form.html'
class DreamUpdate (UpdateView):
model = Dreams
fields = ['titulo', 'objetivo', 'imagem']
template_name = 'dreams_form.html'
class DreamDelete (DeleteView):
model = Dreams
template_name= ('dreams_confirm_delete.html')
success_url= reverse_lazy('webdeve:index')
my urls.py:
from django.conf.urls import url
from webdeve import views
app_name = 'webdeve'
# Dreams
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
# Dreams/detail
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
# Dreams/detail/add
url(r'^dream/add/$', views.DreamCreate.as_view(), name='dream-add'),
# Dreams/detail/Update
url(r'^dream/(?P<pk>[0-9]+)/$', views.DreamUpdate.as_view(), name='dreams-uptdate'),
# Dreams/detail/detete
url(r'^dream/(?P<pk>[0-9]+)/delete/$', views.DreamDelete.as_view(), name='dreams-delete'),
]
And my index.html with delete button:
<!-- linkando css no html -->
{% extends 'base.html' %}
{% block nav %}
<ul>
{% for Dreams in object_list %}
<a href="{% url 'webdeve:detail' Dreams.id %}"><img src={{ Dreams.imagem }}></a>
<li><a href="{% url 'webdeve:detail' Dreams.id %}"> {{ Dreams.titulo }} - {{ Dreams.objetivo }} </a><li>
<!--delete BUTTON-->
<form action="{% url 'webdeve:dreams-delete' Dreams.id %}">
{% csrf_token %}
<input type="hidden" name="dreams_id" value="{{ Dreams.id }}" method="post" style="display: inline" >
<button type="submit" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-trash"></span>
</button>
</form>
{% endfor %}
</ul>
{% endblock %}
Solution
DeleteView
only deletes your object on POST, not GET. As a result, you need to use method="post"
in your form since GET would render this confirm_delete.html again.
<form method="post" action="">
...
</form>
If you can get into DeleteView on GET, action=""
is all you need to POST.
Answered By - sipp11
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.