【问题标题】:Django the best way to create an edit modal form?Django 创建编辑模式表单的最佳方式?
【发布时间】:2020-10-30 21:34:09
【问题描述】:

在我的一个 django 应用程序中,我设置了以下架构:

#models.py
    class Income(models.Model):
        price = models.DecimalField()
        quantity = models.DecimalField()
        date=models.DateField()



# forms.py
    class IncomeForm(forms.ModelForm):
        class Meta:
            model = Income
            fields = "__all__"

#views.py

def income_(request):
    elements = Income.objects.all()
    if request.method == 'POST':
         form = IncomeForm(request.POST)
         if form.is_valid():
             new_input = form.save()
    else :
       form = IncomeForm()
       elements = Income.objects.all()

    context= {
       'form': form,
       'elements':elements,
            }
    return render(request, "income/income.html", context)

在我的 income.html 文件中,我设置了以下内容

{% load crispy_forms_tags %}
 <form id="" method="post">                    
   <div class="form-group col-2 0 mb-0" >
       {{form.quantity|as_crispy_field}}
   </div>
   <div class="form-group col-2 0 mb-0" >
       {{form.price|as_crispy_field}}
   </div>
   <div class="form-group col-2 0 mb-0" >
      {{form.date|as_crispy_field}}
   </div>
</div>

之后,我创建了一个表格,列出了所有填写的数据。 现在我想为每一行创建一个按钮,打开一个模式表单,让我可以修改每个 id 数据集的特定数据。

我尝试使用 ajax 调用来执行它,但是我很难执行表单和数据类型(因为以这种方式我没有可能使用清晰的表单或表单模型django 框架)。

所以我的问题是:有一种简单的方法可以实现我的目标吗?

【问题讨论】:

    标签: javascript django django-models django-forms django-views


    【解决方案1】:

    根据我从您的问题中了解到的情况,您可以尝试在 view.py 中创建一个 UpdateView 并将带有对象 ID 的 html 按钮重定向到该视图。

    更新的答案- 因为您要求以更简单的方式实现编辑页面...

    - models.py
        class Income(models.Model):
            price = models.DecimalField(decimal_places=2, max_digits=10000)
            quantity = models.DecimalField(decimal_places=2, max_digits=10000)
            date = models.DateField()
    
    - urls.py
        urlpatterns = [
            path('income/', views.IncomeListView.as_view(), name='income'),
            path('income_edit/<int:pk>', views.IncomeEdit.as_view(), name='income-edit'),
        ]
    
    - views.py
        class IncomeListView(ListView):
            model = Income
            template_name = 'income.html'
    
    
        class IncomeEdit(UpdateView):
            model = Income
            form_class = IncomeForm
            template_name = "income_form.html"
    
    - forms.py
        class IncomeForm(forms.ModelForm):
            class Meta:
                model = Income
                fields = '__all__'
    
    - income.html
        <h1>Income List</h1>
        <table>
            <tr>
                <th>ID</th>
                <th>price</th>
                <th>quantity</th>
                <th>date</th>
            </tr>
        {% if income_list %}
          {% for income in income_list %}
            <tr>
                <td>{{income.id}}</td>
                <td>{{income.price}}</td>
                <td>{{income.quantity}}</td>
                <td>{{income.date}}</td>
                <td><a href="{% url 'income-edit' income.id %}">Edit</a> </td>
            </tr>
          {% endfor %}
        {% endif %}
        </table>
    
    - income_form.html
        <form action="" method="post">
            {% csrf_token %}
            {{ form.as_p }}
            <input type="submit" value="Submit">
            {{ form.media }}
        </form>
    

    请查看所使用的类以获得更多信息和理解。希望对您有所帮助 =)

    【讨论】:

    • 你能举个例子吗?
    • 使用您的 form.py 和 view.py 更好地理解您的代码对我很有帮助。
    • 是的,我已经编辑了我的答案,希望大家能给我一个回答:)
    • 我已经更新了答案。请查看此示例中使用的类以便更好地理解并将其修改为您的偏好。
    • 很高兴听到这个消息 =)
    猜你喜欢
    • 2017-10-16
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 2011-11-27
    相关资源
    最近更新 更多