我的 Django 表单如何获取当前用户名和电子邮件并将它们提交到另一个数据库 table?
How can my django form grab current username and email and submit them into another database table?
在我的 django 项目中,我有一个表单 VisitorRegForm
,它收集 scanned_id
和 body_temp
并将数据提交到我的数据库 table ClinicalData
,因为当前用户已经登录,我怎样才能让这个表单也能够获取当前登录用户的 username
和 email
(注意:username
和 email
都是创建的来自内置的 django 用户模型) 并提交这四个项目:'scanned_id', 'body_temp', 'username', 'email'
到数据库 table ClinicalData
?
不要介意遗漏的细节和输入,表格有效并且可以提交 'scanned_id', 'body_temp'
。帮助我以最简单的方式提交这四个项目:'scanned_id', 'body_temp', 'username', 'email'
查看以下文件中显示的详细信息:
在哪里应该更容易实现这一目标?在 views.py 或模板文件中?
forms.py
class VisitorRegForm(UserCreationForm):
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2']
class ScannerForm(ModelForm):
class Meta:
model = ClinicalData
fields = ['scanned_id', 'body_temp']
models.py
class ClinicalData(models.Model):
...
scanned_id = models.CharField(verbose_name="Scanned ID", max_length=50, null=True)
body_temp = models.DecimalField(verbose_name="Body Temperature", max_digits=3, decimal_places=1, null=True)
username = models.CharField(verbose_name="Facility Name", max_length=200, null=True)
email = models.EmailField(verbose_name="Visitor Email", max_length=200, null=True)
...
views.py
我假设我可能需要在视图中使用两个变量来帮助我喜欢我已经注释掉的那些,如果它应该从这里开始,请帮助我它应该如何成功:
@login_required(login_url='login')
def scanEnter(request):
#grab_username = function_to_grab_current_logged_in_username_HELP_ME_HERE
#grab_email = function_to_grab_current_logged_in_user_email_HELP_ME_HERE
form = ScannerForm(request.POST)
if request.method == 'POST':
form = ScannerForm(request.POST)
if form.is_valid():
form.save()
return redirect('scanenter')
context = {'form': form}
return render(request, 'scan_enter_fac.html', context)
scan_enter_fac.html
<form action="" method="post">
{% csrf_token %}
<div class="">scanned_id<input type="text" id="multiple" class="form-control" placeholder="or Type ID here; e.g: ACCTS-147" name="scanned_id"></div>
<div class="">body_temp<input type="text" class="form-control" placeholder="e.g: 36.5" name="body_temp"></div>
<button type="submit" class="btn btn-danger btn-block">Submit</button>
</form>
views.py:
@login_required(login_url='login')
def scanEnter(request):
#grab_username = function_to_grab_current_logged_in_username_HELP_ME_HERE
#grab_email = function_to_grab_current_logged_in_user_email_HELP_ME_HERE
form = ScannerForm(request.POST)
if request.method == 'POST':
form = ScannerForm(request.POST)
if form.is_valid():
form = form.save(commit=False)
form.username = request.user.username #new
form.email = request.user.email #new
form.save()
return redirect('scanenter')
context = {'form': form}
return render(request, 'scan_enter_fac.html', context)
scan_enter_fac.html
<form action="" method="post">
{% csrf_token %}
<div class="">{{ form.scanned_id }}</div>
<div class="">{{ form.body_temp }}</div>
<button type="submit" class="btn btn-danger btn-block">Submit</button>
</form>
在我的 django 项目中,我有一个表单 VisitorRegForm
,它收集 scanned_id
和 body_temp
并将数据提交到我的数据库 table ClinicalData
,因为当前用户已经登录,我怎样才能让这个表单也能够获取当前登录用户的 username
和 email
(注意:username
和 email
都是创建的来自内置的 django 用户模型) 并提交这四个项目:'scanned_id', 'body_temp', 'username', 'email'
到数据库 table ClinicalData
?
不要介意遗漏的细节和输入,表格有效并且可以提交 'scanned_id', 'body_temp'
。帮助我以最简单的方式提交这四个项目:'scanned_id', 'body_temp', 'username', 'email'
查看以下文件中显示的详细信息:
在哪里应该更容易实现这一目标?在 views.py 或模板文件中?
forms.py
class VisitorRegForm(UserCreationForm):
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2']
class ScannerForm(ModelForm):
class Meta:
model = ClinicalData
fields = ['scanned_id', 'body_temp']
models.py
class ClinicalData(models.Model):
...
scanned_id = models.CharField(verbose_name="Scanned ID", max_length=50, null=True)
body_temp = models.DecimalField(verbose_name="Body Temperature", max_digits=3, decimal_places=1, null=True)
username = models.CharField(verbose_name="Facility Name", max_length=200, null=True)
email = models.EmailField(verbose_name="Visitor Email", max_length=200, null=True)
...
views.py
我假设我可能需要在视图中使用两个变量来帮助我喜欢我已经注释掉的那些,如果它应该从这里开始,请帮助我它应该如何成功:
@login_required(login_url='login')
def scanEnter(request):
#grab_username = function_to_grab_current_logged_in_username_HELP_ME_HERE
#grab_email = function_to_grab_current_logged_in_user_email_HELP_ME_HERE
form = ScannerForm(request.POST)
if request.method == 'POST':
form = ScannerForm(request.POST)
if form.is_valid():
form.save()
return redirect('scanenter')
context = {'form': form}
return render(request, 'scan_enter_fac.html', context)
scan_enter_fac.html
<form action="" method="post">
{% csrf_token %}
<div class="">scanned_id<input type="text" id="multiple" class="form-control" placeholder="or Type ID here; e.g: ACCTS-147" name="scanned_id"></div>
<div class="">body_temp<input type="text" class="form-control" placeholder="e.g: 36.5" name="body_temp"></div>
<button type="submit" class="btn btn-danger btn-block">Submit</button>
</form>
views.py:
@login_required(login_url='login')
def scanEnter(request):
#grab_username = function_to_grab_current_logged_in_username_HELP_ME_HERE
#grab_email = function_to_grab_current_logged_in_user_email_HELP_ME_HERE
form = ScannerForm(request.POST)
if request.method == 'POST':
form = ScannerForm(request.POST)
if form.is_valid():
form = form.save(commit=False)
form.username = request.user.username #new
form.email = request.user.email #new
form.save()
return redirect('scanenter')
context = {'form': form}
return render(request, 'scan_enter_fac.html', context)
scan_enter_fac.html
<form action="" method="post">
{% csrf_token %}
<div class="">{{ form.scanned_id }}</div>
<div class="">{{ form.body_temp }}</div>
<button type="submit" class="btn btn-danger btn-block">Submit</button>
</form>