1

I have created a forms.py file, find below code.

I would like to get the current login user from function add_cr of view.py then send this value to the pm widget.

Please advise how to achieve that, thanks!

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 ' }),

    }

Update.

views.py

@login_required(login_url='login')

def add_cr(request): submitted = False

# 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})
LeeLiao
  • 11
  • 2
  • To clarify, you want to put a placeholder in `pm` with the username of the logged in user? – PacketLoss May 11 '21 at 02:18
  • @PacketLoss, thank you for your reply. I want to set the username of the logged in user automatically and set the TextInput widget to disable. – LeeLiao May 11 '21 at 02:21
  • I am not idea how to implement that. Could you show me how to set it up? I have updated my views file in my post. – LeeLiao May 11 '21 at 03:22

0 Answers0