如何在 TextInput 小部件上设置默认值?

时间:2021-05-11 02:17:25

标签: python django

我创建了一个 forms.py 文件,找到下面的代码。

我想从 view.py 的函数 add_cr 获取当前登录用户,然后将此值发送到 pm 小部件。

请告知如何实现,谢谢!

class TaskForm(ModelForm):
class Meta:
    model = Task
    fields = ('user',
              'cr_date', 
              'project_name', 
              'cr_description', 
              'cr_project_code', 
              'attendance_point',
              'result_point',
              'pm',
              'remark',
              )

    labels = {
            'user': "Select Engineer",
            'cr_date': "CR date", 
            'project_name': "", 
            'cr_description': "", 
            'cr_project_code': "", 
            'attendance_point': "出勤",
            'result_point': "作業結果",
            'pm': "PM",
            'remark': "",
    }

    MONTHS = {
        1:('1月'), 2:('2月'), 3:('3月'), 4:('4月'),
        5:('5月'), 6:('6月'), 7:('7月'), 8:('8月'),
        9:('9月'), 10:('10月'), 11:('11月'), 12:('12月')
        }

    widgets = {
        'user': forms.Select(attrs={'class':'form-select'}),
        # 'cr_date': forms.SelectDateWidget(months=MONTHS, attrs={'style': 'font-size: 15px'}),
        'cr_date': forms.NumberInput(attrs={'type': 'date'}),
        'project_name': forms.TextInput(attrs={'class':'form-control', 'placeholder': 'Enter Project Name'}), 
        'cr_description': forms.TextInput(attrs={'class':'form-control', 'placeholder': 'Enter CR content'}), 
        'cr_project_code': forms.TextInput(attrs={'class':'form-control', 'placeholder': 'Enter Project Code'}), 
        'attendance_point': forms.Select(attrs={'class':'form-select'}),
        'result_point': forms.Select(attrs={'class':'form-select'}),
        'pm': forms.TextInput(attrs={'class':'form-select'},),
        'remark': forms.TextInput(attrs={'class':'form-control', 'placeholder': '[Optional] : Enter a reamrk for CR ' }),

    }

更新。

views.py

@login_required(login_url='login')

定义 add_cr(请求): 提交 = 错误

# print(current_user)
if request.method == "POST":
    form = TaskForm(request.POST)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect('/add_cr?submitted=True', current_user)

else:
    form = TaskForm
    if 'submitted' in request.GET:
        submitted = True

return render(request, 'add_cr.html', {'form':form(user=request.user), 'submitted': submitted})

0 个答案:

没有答案
相关问题