猿问

如何使用超链接更改 Django 中的实例?

我有 Django 的问题。我想使用超链接更改模型的特定字段,但我不知道该怎么做。我试过这个,但它什么也没做。


视图.py:


def sGiocatore(request, giocatore_id):

    item = get_object_or_404(Giocatore, pk=giocatore_id)

    item.fantasquadra=None #or something else

    return redirect('/sondaggio/fantasquadre')

网址.py:


path('svincola/<int:giocatore_id>/', views.sGiocatore, name='s_player'),

表格.py:


class EditGiocatoreForm(forms.ModelForm):

class Meta:

    model = Giocatore

    fields = ['fantasquadra']

列表.html:


{% for fantasquadra in elenco_fantasquadre %}

<p><b>{{ fantasquadra.nome_fantasquadra }}:</b></p>

<table id="customers">

{%for giocatore in elenco_giocatori%}

{% if giocatore.fantasquadra.nome_fantasquadra == 

fantasquadra.nome_fantasquadra %}

<tr><th>Nome</th><th>Cognome</th><th>N° Maglia</th><th>Squadra</th> 

<th>Ruolo</th><th>Svincola</th></tr>

<tr><td>{{ giocatore.nome_giocatore }}</td>

<td>{{ giocatore.cognome_giocatore }}</td>

<td>{{giocatore.numero_maglia}}</td>

<td>{{giocatore.squadra.nome_squadra}}</td>

<td>{{giocatore.ruolo.ruolo}}</td>

<td><a href="/sondaggio/svincola/{{giocatore.id}}">Svincola</a></td></tr> 

{% endif %}

{% endfor %}

</table>  

{% endfor %}


海绵宝宝撒
浏览 174回答 1
1回答

犯罪嫌疑人X

item.save()修改后添加。
随时随地看视频慕课网APP

相关分类

Python
我要回答