V
V
Val1n0r2019-08-24 12:29:07
Django
Val1n0r, 2019-08-24 12:29:07

How to handle clicking on a django link?

I started learning django, a question arose
I am writing a simple text-based web game
I have a "Fights" page against other players (The system is extremely simple. We click on the "attack" button, we calculate who won and display by the formula, without any visualization)
There is a hero model , with its characteristics (which returns the ID that I use to generate the link url )
urls.py

urlpatterns = [
    path('', main_page,name='main_page'),
    path('swap-hero/', swap_hero, name='swap-hero'),
    path('store/', store, name='buyhero'),
    path('store/<int:id>',buyhero,name='buyhero'),
    path('profile/',profileUrl,name='profileUrl'),
    path('fight/',fightPage,name='fightPage'),
    path('buff/',buffHero,name='buffHero'),
    path('castle/',UserCastle,name='UserCastle'),
    path('fight/<int:id>',fightPage,name='makefight'),
]

On the fight.html page, I display a list of opponents in a loop, each "Attack" link looks like: The question is, how to handle clicking on the link? I didn’t find anything concrete even on theory (Unless I realized that regular views cannot process POST and it’s worth digging towards Class-based-Views)
<a href="fight.get_absolute_url">Напасть</a>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2019-08-24
@sergey-gornostaev

I didn't find anything concrete even on the theory

It's in the Django manual.
Your understanding is poor. First, a normal view can process a POST request. Secondly, CBV is about code organization.

A
Alexey Guest007, 2019-08-25
@Guest007

on the fight.html page, add id attributes to the links (unique, by itself) and put an on("click") handler using, for example, jQuery at the end of the page. And let this onclick execute an AJAX request of the type you need. So you don’t have to reload the page and queries can be built as you need.
And, by the way, it's not very correct... But you probably already read this in the documentation... And, yes, the "regular" view does what it needs - everything you want :-) Accept POST requests - so definitely can

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question