Django DeleteView don't delete the object from data
first of all thanks for your time.
i've just learned CreateView UpdateView and DeleteView models. and them 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 de delete button and confirm delete it isnt deleting from data.
Those 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 Wich (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, Wich
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 %}
python html mysql django sql-delete
add a comment |
first of all thanks for your time.
i've just learned CreateView UpdateView and DeleteView models. and them 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 de delete button and confirm delete it isnt deleting from data.
Those 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 Wich (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, Wich
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 %}
python html mysql django sql-delete
add a comment |
first of all thanks for your time.
i've just learned CreateView UpdateView and DeleteView models. and them 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 de delete button and confirm delete it isnt deleting from data.
Those 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 Wich (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, Wich
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 %}
python html mysql django sql-delete
first of all thanks for your time.
i've just learned CreateView UpdateView and DeleteView models. and them 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 de delete button and confirm delete it isnt deleting from data.
Those 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 Wich (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, Wich
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 %}
python html mysql django sql-delete
python html mysql django sql-delete
edited Nov 19 '18 at 19:48
lucasrf27
asked Nov 19 '18 at 19:16
lucasrf27lucasrf27
94
94
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
sorry 'im editting my question.
– lucasrf27
Nov 19 '18 at 19:44
the real problem is that on the url its gaving me de id like the ones that a i've deleted before havent been deleted is that a problem for the future?
– lucasrf27
Nov 19 '18 at 19:45
I'm pretty sure that if you don't usemethod="post"
, your object is not going anywhere. Well, for another question, it's not likely that you will have the same id as before. What database are you using? I can't recall that django ever use the samepk
at all.
– sipp11
Nov 19 '18 at 19:53
yeah, without the method you can do this but gotta create a confirm_delete.html and i was trying to redirect to the same index page whitout confirm_template, and with your answer that was possible. about the pk, is that right so? my url should show me a number like 14 or 15 after the firsts 2 i've created and have never deleted.
– lucasrf27
Nov 19 '18 at 20:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381223%2fdjango-deleteview-dont-delete-the-object-from-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
sorry 'im editting my question.
– lucasrf27
Nov 19 '18 at 19:44
the real problem is that on the url its gaving me de id like the ones that a i've deleted before havent been deleted is that a problem for the future?
– lucasrf27
Nov 19 '18 at 19:45
I'm pretty sure that if you don't usemethod="post"
, your object is not going anywhere. Well, for another question, it's not likely that you will have the same id as before. What database are you using? I can't recall that django ever use the samepk
at all.
– sipp11
Nov 19 '18 at 19:53
yeah, without the method you can do this but gotta create a confirm_delete.html and i was trying to redirect to the same index page whitout confirm_template, and with your answer that was possible. about the pk, is that right so? my url should show me a number like 14 or 15 after the firsts 2 i've created and have never deleted.
– lucasrf27
Nov 19 '18 at 20:48
add a comment |
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.
sorry 'im editting my question.
– lucasrf27
Nov 19 '18 at 19:44
the real problem is that on the url its gaving me de id like the ones that a i've deleted before havent been deleted is that a problem for the future?
– lucasrf27
Nov 19 '18 at 19:45
I'm pretty sure that if you don't usemethod="post"
, your object is not going anywhere. Well, for another question, it's not likely that you will have the same id as before. What database are you using? I can't recall that django ever use the samepk
at all.
– sipp11
Nov 19 '18 at 19:53
yeah, without the method you can do this but gotta create a confirm_delete.html and i was trying to redirect to the same index page whitout confirm_template, and with your answer that was possible. about the pk, is that right so? my url should show me a number like 14 or 15 after the firsts 2 i've created and have never deleted.
– lucasrf27
Nov 19 '18 at 20:48
add a comment |
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.
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.
edited Nov 19 '18 at 19:47
answered Nov 19 '18 at 19:37
sipp11sipp11
40624
40624
sorry 'im editting my question.
– lucasrf27
Nov 19 '18 at 19:44
the real problem is that on the url its gaving me de id like the ones that a i've deleted before havent been deleted is that a problem for the future?
– lucasrf27
Nov 19 '18 at 19:45
I'm pretty sure that if you don't usemethod="post"
, your object is not going anywhere. Well, for another question, it's not likely that you will have the same id as before. What database are you using? I can't recall that django ever use the samepk
at all.
– sipp11
Nov 19 '18 at 19:53
yeah, without the method you can do this but gotta create a confirm_delete.html and i was trying to redirect to the same index page whitout confirm_template, and with your answer that was possible. about the pk, is that right so? my url should show me a number like 14 or 15 after the firsts 2 i've created and have never deleted.
– lucasrf27
Nov 19 '18 at 20:48
add a comment |
sorry 'im editting my question.
– lucasrf27
Nov 19 '18 at 19:44
the real problem is that on the url its gaving me de id like the ones that a i've deleted before havent been deleted is that a problem for the future?
– lucasrf27
Nov 19 '18 at 19:45
I'm pretty sure that if you don't usemethod="post"
, your object is not going anywhere. Well, for another question, it's not likely that you will have the same id as before. What database are you using? I can't recall that django ever use the samepk
at all.
– sipp11
Nov 19 '18 at 19:53
yeah, without the method you can do this but gotta create a confirm_delete.html and i was trying to redirect to the same index page whitout confirm_template, and with your answer that was possible. about the pk, is that right so? my url should show me a number like 14 or 15 after the firsts 2 i've created and have never deleted.
– lucasrf27
Nov 19 '18 at 20:48
sorry 'im editting my question.
– lucasrf27
Nov 19 '18 at 19:44
sorry 'im editting my question.
– lucasrf27
Nov 19 '18 at 19:44
the real problem is that on the url its gaving me de id like the ones that a i've deleted before havent been deleted is that a problem for the future?
– lucasrf27
Nov 19 '18 at 19:45
the real problem is that on the url its gaving me de id like the ones that a i've deleted before havent been deleted is that a problem for the future?
– lucasrf27
Nov 19 '18 at 19:45
I'm pretty sure that if you don't use
method="post"
, your object is not going anywhere. Well, for another question, it's not likely that you will have the same id as before. What database are you using? I can't recall that django ever use the same pk
at all.– sipp11
Nov 19 '18 at 19:53
I'm pretty sure that if you don't use
method="post"
, your object is not going anywhere. Well, for another question, it's not likely that you will have the same id as before. What database are you using? I can't recall that django ever use the same pk
at all.– sipp11
Nov 19 '18 at 19:53
yeah, without the method you can do this but gotta create a confirm_delete.html and i was trying to redirect to the same index page whitout confirm_template, and with your answer that was possible. about the pk, is that right so? my url should show me a number like 14 or 15 after the firsts 2 i've created and have never deleted.
– lucasrf27
Nov 19 '18 at 20:48
yeah, without the method you can do this but gotta create a confirm_delete.html and i was trying to redirect to the same index page whitout confirm_template, and with your answer that was possible. about the pk, is that right so? my url should show me a number like 14 or 15 after the firsts 2 i've created and have never deleted.
– lucasrf27
Nov 19 '18 at 20:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381223%2fdjango-deleteview-dont-delete-the-object-from-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown