【问题标题】:How do I use Django generic updateviews to update my model using individual form field values?如何使用 Django 通用更新视图来使用单个表单字段值更新我的模型?
【发布时间】:2022-12-12 10:48:25
【问题描述】:

模型.py:

from django.db import models

class Location(models.Model):
    name = models.CharField(max_length=20)
    is_source = models.BooleanField(default=False)
    is_destination = models.BooleanField(default=False)

    def __str__(self):
        return self.name

视图.py

from django.shortcuts import render
from django.urls import reverse_lazy
from django.views import generic
from .models import Location

class LocationsListView(generic.ListView):
    model = Location
    template_name = 'locations/list.html'
    context_object_name = 'locations'


class LocationUpdateView(generic.edit.UpdateView):
    model = Location
    fields = ['name', 'is_source', 'is_destination']
    context_object_name = 'location'
    template_name = 'locations/update.html'
    success_url = reverse_lazy('locations:list')

class LocationDeleteView (generic.edit.DeleteView):
    model = Location
    template_name = 'locations/confirm_delete.html'
    context_object_name = 'location'
    success_url = reverse_lazy('locations:list')

位置/update.html

{% extends 'base.html' %}

{% block title %}Location Update{% endblock %}

{% block content %}

<section>
    <div class="container">
        <h1>Location Update</h1>
        <div class="form-container">
            <form method="post">
                {% csrf_token %}
                {% if form.errors %}
                <div class="p-3 mb-3 border border-danger border-3 rounded">{{ form.errors }}</div>
                {% endif %}
                <div class="mb-3">
                    <label for="" class="form-label">Name</label>
                    <input type="text" class="form-control" value="{{ form.name.value }}">
                </div>
                <div class="mb-3">
                    <input type="checkbox" class="form-check-input" {% if form.is_source.value %} checked {% endif %}>
                    <label for="">Source</label>
                </div>
                <div class="mb-3">
                    <input type="checkbox" class="form-check-input" {% if form.is_destination.value %} checked {% endif %}> 
                    <label for="">Destination</label>
                </div>
                <input type="submit" class="btn btn-success mb-3" value="Save">
            </form>
        </div>
    </div>
</section>

{% endblock %}

位置.urls.py

from django.urls import path
from . import views

app_name = 'locations'


urlpatterns = [
    path('', views.LocationsListView.as_view(), name='list'),
    path('update/<int:pk>/', views.LocationUpdateView.as_view(), name='update'),
    path('delete/<int:pk>/', views.LocationDeleteView.as_view(), name='delete'), 
]

当我尝试更新我的模型时,使用 {{form.name.value}} 单独呈现表单字段时,我收到一条错误消息,指出我的姓名字段是必需的,但它已被填充。例如,如果我使用 {{form.as_p}} 呈现整个表单,我不会收到错误消息。我究竟做错了什么?

我尝试使用 {{form.as_p}} 并且有效。但我需要单独的字段渲染,以便我可以设计我的表单。

【问题讨论】:

    标签: django forms field required updateview


    【解决方案1】:

    您需要为每个字段 &lt;input&gt; 标签提供 name 属性。如果手动渲染,应该使用该字段的html_name属性

    <input name="{{ form.name.html_name }}" type="text" class="form-control" value="{{ form.name.value }}">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多