How to open a new window with passed through data id, by clicking on a row, in {% url '...' %}?

How to open a new window with passed through data id, by clicking on a row, in {% url '...' %}?

ynynloxxynynloxx Posts: 14Questions: 6Answers: 0

Hey people,

my current problem is, that I don't know how to pass the id of the data of the row I have clicked on to the {% url '...' %}.

var table = $('#table_id').DataTable({ ... });
...
$('#table_id tbody').on( 'click', 'tr', function () {
        var ID = table.row(this).id();
        var href = '{% url 'lagerverwaltung:article_outsourcing' %}';
        window.open(href, target='_self');
} );
...

But the argument '{% url 'lagerverwaltung:article_outsourcing' %}' is missing the article_id because my view.py looks like this:

def article_outsourcing(request, article_id):
    try:
        article = Article.objects.get(pk = article_id)
    except Article.DoesNotExist:
        raise Http404("There is no article with id " + article)
    return render(request, 'lagerverwaltung/article_outsourcing.html', {'article': article})

and urls.py like this:

urlpatterns = [
...
       path('outsourcing/article_outsourcing/<int:article_id>', views.article_outsourcing, name='article_outsourcing'),
...
]

When I write it like this

$('#table_id tbody').on( 'click', 'tr', function () {
        var ID = table.row(this).id();
        window.open('{% url 'lagerverwaltung:article_outsourcing' ID %}', target='_self');
} );

The ID won't be recognised.

I hope someone can help me.

Kind regards,
Yin-yin

Answers

  • colincolin Posts: 15,146Questions: 1Answers: 2,586

    Hi @ynynloxx ,

    Do the rows have an explicit ID, like this example here? Or is the ID just a field in the data, which your previous post seems to suggest. If it is just in the data, try:

    $('#table_id tbody').on( 'click', 'tr', function () {
            var ID = table.row(this).data().id
            window.open('{% url 'lagerverwaltung:article_outsourcing' ID %}', target='_self');
    } );
    

    Cheers,

    Colin

  • ynynloxxynynloxx Posts: 14Questions: 6Answers: 0

    Hi @colin,

    actually it is not the exact problem. I get exact ID of the data of the clicked on row. But currently there is a NoReverseMatch Error

    where I don't know how it came from.

    Hope you can help

    Yin-yin

  • kthorngrenkthorngren Posts: 20,313Questions: 26Answers: 4,771

    Thats not a Datatables error. Maybe this SO thread will help:
    https://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it

    Kevin

This discussion has been closed.