0

我是 django 的新手,我正在尝试制作一个交互式表格,并且能够逐一编辑每一行,这意味着:使用 mysite 的人可以看到所有表格,并且在每一行之后,当他单击它时,会有一个编辑按钮,它让他只能编辑该行,我希望这是有道理的

这是我的代码:views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from .models import Client
from .form import Clientform
from django.urls import reverse_lazy

def editclient(request):
pk = request.POST.get('id')
client=Client.objects.get(pk)
if request.method == 'POST':
        print("seen as POST")
        form = Clientform(request.POST, instance=client)
        if form.is_valid():
            form.save()
        return render(request,'polls/editclient.html',{"form": form})
else:
    return render(request,'polls/editclient.html',{"form": form})

表格.py

from django import forms
from .models import Client

class Clientform(forms.ModelForm):
    class Meta:
        model = Client
        fields =['nom_client','prenom_client','ville_client','email_client']

base.html "表格部分"

 <div>
    <table class="table table-striped table-dark" style="max-width: min-content;">
        <tr>
            <th scope="col" style="width:10px">ID</th>
            <th scope="col"style="width: 20px">Nom</th>
            <th scope="col"style="width: 20px">Prenom</th>
            <th scope="col"style="width: 20px"> Ville</th>
            <th scope="col" style="width: 30px">Email</th>
        </tr>
        {% csrf_token %}
        {% for client in clients %}
        <tr>

            <td scope="row"> {{client.id}}</td>
            <td>{{ client.nom_client }}</td>
            <td>{{ client.prenom_client }}</td>
            <td>{{ client.ville_client }}</td>
            <td>{{ client.email_client }}</td>   
            <td><a href="{% url 'editclient'%}" ><button type="button" name="id" value="{{ client.id }}">edit {{client}}</button></a></td>             
        </tr>
        {% endfor %}
    </table>
</div>

我希望有人告诉我如何解决这个问题,我对 django 编程还是很陌生

4

0 回答 0