Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Link a template with the url of a class-based-view?

I'm trying to create a link <a> that redirects me to an url of a view DeleteView. I want that link to be in the template of my app. By clicking on that link you will get redirected to the url of the DeleteView.

Here's the DeleteView:

class imagenDelete(DeleteView):
model = imagen

def get_id(self, request):
    getall = imagen.objects.all()

Here's the urls.py:

urlpatterns = [

path('', views.galeria, name='galeria'),
path('delete/<int:pk>', views.imagenDelete.as_view(), name='deletegaleria'),

]

Here's the link im trying to create in the template of my App:

<a style="font-size: 3rem;" href="{% url 'deletegaleria' pk %}">Click to delete</a>

The problem is that in the url i must pass the id of the image that you want to delete. If i'd write it like this {% url 'deletegaleria' 14 %} it works, te link is shown in the template and it redirects you to the url to delete the image with id = 14.

If i'd do it like this:

{% for on in getall %}
<a style="font-size: 3rem;" href="{% url 'deletegaleria' o.id %}">Click to delete</a>
{% endfor %}

This doesn't give me any error but doesn't show the link in the template.

And if i do this way {% url 'deletegaleria' pk %} which is basically no iterating i get the next error:

Reverse for 'deletegaleria' with arguments '('',)' not found. 1 pattern(s) tried: ['galeria/delete/(?P[0-9]+)$']

like image 636
Space guy Avatar asked Nov 19 '25 07:11

Space guy


1 Answers

So the problem was that i was working with a Function-Based-View to render my template and Class-Based-View to Delete some objects.

What i did to make it work was only work with class-based-views.

For anyone wondering what the next code does: I created two views. One for showing images. Another one shows a form that allows users to upload their own images. I'm also saving the User that upload the image when the method is POST. Later i create the DeleteView which will delete images only if the logged in user is the same that uploaded the image.

views.py:

class galeriaListView(ListView):
    model = imagen
    template_name = "galeria/galeria.html"
    imagenes = imagen.objects.all()
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['imagenes'] = self.imagenes
        return context

class formularioimagenListView(ListView):
    model = imagen
    formulario_subir_imagen = formulario_img()
    template_name = "galeria/formulario_subir_imagen.html"
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['formulario_subir_imagen'] = self.formulario_subir_imagen
        return context
    def post(self, request, *args, **kwargs):
        formulario_subir_imagen = formulario_img(request.POST, request.FILES)
        if formulario_subir_imagen.is_valid():
            profile = formulario_subir_imagen.save(commit=False)
            profile.autor = request.user
            profile.save()
            return redirect("/galeria/")

class imagenDelete(DeleteView):
    model = imagen
    success_url = "/galeria/"
    template_name = "galeria/delete_imagen.html"
    def dispatch(self, request, *args, **kwargs):
        self.obj = self.get_object()
        if self.obj.autor != self.request.user:
            return HttpResponseForbidden("Cannot delete other's posts")
        return super(imagenDelete, self).dispatch(request, *args, **kwargs)
    

urls.py:

    path('', views.galeriaListView.as_view(), name='galeria'),
    path('<pk>/delete/', views.imagenDelete.as_view(), name='deleteimagen'),
    path('subir_imagen', views.formularioimagenListView.as_view(), name='subirimagen'),
    path('borrar_imagen', views.borrar_imagenListView.as_view(), name='opcionesimagenesaborrar'),

And the <a> in the template would look like this.

template.html:

<a style="font-size: 3rem;" href="{% url 'subirimagen' %}">Click para subir imagen</a>
<a style="font-size: 3rem;" href="{% url 'opcionesimagenesaborrar' %}">Click para borrar tus imagenes</a> 
like image 191
Space guy Avatar answered Nov 22 '25 01:11

Space guy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!