在更新窗体之前,如何显示窗体中行的db值?

2024-05-08 11:58:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在通过URL传递DB-by-pk后显示雇员数据。你知道吗

我可以更新表单,但我不希望表单字段为空,因为我只想更新,这意味着我不会接触所有的字段

你知道吗表单.py你知道吗

class AddEmployeeForm(forms.Form):

    genderset = [(0,'--Select an Option--'),('Male','Male'), ('Female', 'Female')]
    marital_status_set = [(0,'--Select an Option--'),('Married','Married'), ('Single', 'Single')]
    employment_type_set =  [(0,'--Select an Option--'),('Contract','Contract'), ('Full-Time', 'Full-Time'),('Intern', 'Intern')]
    employment_status_set =  [(0,'--Select an Option--'),('Active','Active'), ('Inactive', 'Inactive')]


    first_name = forms.CharField(label = "First Name ", max_length = 200)
    last_name = forms.CharField(label = "Last Name ", max_length = 200)
    employee_id = forms.IntegerField()
    email = forms.EmailField(label = "Email ", max_length = 200)
    address = forms.CharField(label = "Address", max_length = 200)
    role = forms.CharField( max_length = 200)
    date_of_birth = forms.DateField()
    join_date = forms.DateField()
    end_date = forms.DateField()
    location = forms.CharField( max_length = 200)
    hod  = forms.ModelChoiceField(queryset=Department.objects.only('lead'))
    phone_number = forms.CharField( max_length = 200)
    employment_type = forms.ChoiceField( choices = employment_type_set)
    employment_status = forms.ChoiceField( choices = employment_status_set  )
    marital_status = forms.ChoiceField( choices = marital_status_set )
    gender = forms.ChoiceField( choices = genderset )

    department = forms.ModelChoiceField(queryset=Department.objects.only('dept_name'))
    credentials = forms.FileField()
    passport = forms.FileField()
    date_added = forms.DateTimeField( initial = datetime.now, widget=forms.HiddenInput())


你知道吗视图.py你知道吗

@login_required(login_url='/accounts/login')
def edit(request, pk):
    employee = Employee.objects.filter(pk=pk)
    form = AddEmployeeForm()
    context = {
        'employee': employee,
        'form':form
    }


    return render(request, 'employees/edit.html', context)

你知道吗编辑.html你知道吗


{% extends 'base.html' %}
{% load crispy_forms_tags %}


{% block content %}

    <div class="page-wrapper">
        <div class="content container-fluid">
            <div class="row">
                <div class="col-xs-4">
                    <h4 class="page-title">Edit Employee Details</h4>
                </div>
                <div class="col-xs-4 text-center">
                    {% include "partials/_alerts.html" %}
                </div>
            </div>
            <form class="m-b-30" action="{% url 'add' %}" method="post" enctype="multipart/form-data">
                    {% csrf_token %}
                    <div class="row"></div>
                    {% for field in form %}

                        <div class="col-sm-6">
                            <div class="form-group">

                                {{ field.errors }}
                                {{ field|as_crispy_field }}
                                {% if field.help_text %}
                                <p class="help">{{ field.help_text|safe }}</p>
                                {% endif %}
                            </div>
                        </div>
                        {% endfor %}
                    </div>

                    <div class="m-t-20 text-center">
                        <button class="btn btn-primary">Save Changes</button>
                    </div>
            </form>
        </div>
    </div>

{% endblock content %}

它意味着显示使用PK值从数据库中筛选出来的雇员的值


Tags: divformanfieldstatusformsselectlength
2条回答

views.py中,可以将字典传递给AddEmployeeForm构造函数以显示值:

@login_required(login_url='/accounts/login')
def edit(request, pk):
    employee = Employee.objects.filter(pk=pk)
    field_values = { 'first_name': employee.first_name } #...other fields
    form = AddEmployeeForm(field_values)
    context = {
        'employee': employee,
        'form':form
}
return render(request, 'employees/edit.html', context)

A Form instance is either bound to a set of data, or unbound.

If it’s bound to a set of data, it’s capable of validating that data and rendering the form as HTML with the data displayed in the HTML.

If it’s unbound, it cannot do validation (because there’s no data to validate!), but it can still render the blank form as HTML.

参考https://docs.djangoproject.com/en/2.2/ref/forms/api/#ref-forms-api-bound-unbound

您可以创建表单实例并使用以下格式:

form = In_Form(initial={'username': name})

在模板中,您可以使用以下方法填充表单:

{{employee.name}}

相关问题 更多 >