Django 管理站点不反映表单中的数据

Django admin site doesn't reflect the data from the form

我希望用户在我的数据库中输入数据,这样管理员就可以得到一个请求休假的用户列表。

以下是我的脚本。

models.py

from django.db import models
from django.contrib.auth.models import User

class Leave(models.Model):

    name = models.CharField(max_length = 50)
    employee_id = models.CharField(max_length = 20)
    department = models.CharField(max_length = 15)
    designation = models.CharField(max_length = 15)
    type_of_leave = models.CharField(max_length = 15)
    from_date = models.DateField()
    to_date = models.DateField()
    reason = models.CharField(max_length= 180)

我不确定如何将这里的表单保存到数据库中。 views.py

from .forms import LeaveRequestForm
from django.shortcuts import render, redirect
from django.views.generic import TemplateView

class LeaveRequestView(TemplateView):
    template_name = 'lrequests/request_form.html'

    def get(self, request):
        form = LeaveRequestForm()
        return render(request, self.template_name, {'form':form})

    def post(self, request):
        form = LeaveRequestForm(request.POST or None)
        context = {
            'form':form
        }

        if form.is_valid():
            form.save()
            name = form.cleaned_data['name']
            employee_id = form.cleaned_data['employee_id']
            dept = form.cleaned_data['department']
            desg = form.cleaned_data['designation']
            rpt_mgr = form.cleaned_data['reporting_manager']
            tof = form.cleaned_data['department']
            from_date = form.cleaned_data['from_date']
            to_date = form.cleaned_data['to_date']
            reason = form.cleaned_data['reason']
            context.update({'name':name, 'employee_id':employee_id, 'dept':dept, 'desg':desg, 'rpt_mgr':rpt_mgr, 'tof':tof, 'from_date':from_date, 'to_date':to_date, 'reason':reason})
    return render(request, self.template_name, context)

以下是我想要在我的表单中填写的字段。

forms.py

from django import forms
from .models import Leave

CHOICES = (('1','Earned Leave'),('2','Casual Leave'),('3','Sick Leave'),('4','Paid Leave'))

class LeaveRequestForm(forms.ModelForm): 

    name = forms.CharField(max_length = 50)
    employee_id = forms.CharField(max_length = 20)
    department = forms.CharField(label = 'Department ', required = True)
    designation = forms.CharField(label = 'Designation ', required = True)
    reporting_manager = forms.CharField(max_length = 20, required = True)
    type_of_leave = forms.ChoiceField(choices=CHOICES, required = True)
    from_date = forms.DateField(required = True)
    to_date = forms.DateField(required = True)
    reason = forms.CharField( required = True, max_length= 180)

    class Meta:
        model = Leave
        fields = ('name',)

我什至在管理员中也注册了模型

admin.py

from django.contrib import admin

from . import models

admin.site.register(models.Leave)

确保表单是模型表单并在管理员中注册。如果它仍然没有出现,那么可能有一些需要填写的必填字段当前没有填写,这会阻止表单保存到数据库

您定义了一个表单,但没有告诉 Django 在该模型的管理中使用它。您需要定义一个 ModelAdmin class 并设置它的 form attribute:

class LeaveAdmin(admin.ModelAdmin):
    form = LeaveRequestForm

admin.site.register(models.Leave, LeaveAdmin)

admin.py 不应该是这样的:

from django.contrib import admin
from . import models

@admin.register(models.Leave)
class LeaveAdmin(admin.ModelAdmin):
    list_display = [ 'name', 'department', etc...]

将您的 views.py 函数设为 -

if request.method == "POST":
        form = LeaveRequestForm(request.POST)
        if form.is_valid():
            leave = form.save(commit = False)
            leave.user = request.user
            form.save()