Django get info on the same page without showing the pk in the url

1

I hope you can help me, I am trying to see information from the database but without moving the url.

When I access the list I get, for example, a list of books and when I click on the details of the book, I enter it in detail, however when I enter that page, it has the id of the database of the model.

Is there a way to be able to access without the URL moving?

I hope you can help me or give me some examples

urls:

url(r'^detalle/(?P<pk>[0-9]+)/$', views.detalle_id, name="detalle_id"),
url(r'^detalle/(?P<nombre>.*)/$', views.detalle_nombre),

views:

def detalle_id(request, pk):
    detalle = Pregunta.objects.get(pk=pk)
    Pregunta.objects.filter(id=pk).update(comentario='1')

    return render(request, 'detalle.html', {'detalle': detalle})


def detalle_nombre(request, nombre):
    detalle = Pregunta.objects.get(nombre=nombre)
    return render(request, 'detalle.html', {'detalle': detalle})

def lista(request):
    listadb = Pregunta.objects.all()
    return render(request, 'lista.html', {'listadb':listadb})

I hope you can help me or give me some examples, Thanks!

    
asked by Rodrigo Calderon 17.09.2016 в 00:41
source

1 answer

1

I can think of two options:

  • Send id using the verb POST . And instead of a link, use a button, and obviously, process the form

    <form method='POST' action='{% url "detalle_id" '>
        {% csrf_token %}
        <input type='hidden' value='{{ detalle.id }}' name='id'>
        <input type="submit" name='Botón de enviar' value="Ver detalle">
    </form>
    
  • Use jQuery to bring the information, for example

    $.ajax({
      type: "GET",
      url: {% url 'detalle_id' %},
      dataType: "html",
      success: // lo que hagas con tus datos
    });
    
  • answered by 17.09.2016 в 14:50