django - pass parameter to form from template in Django 2
问题描述
in Django I make a form which get an email address and save it in database and this my form.py:
class NewsletterUserSignUpForm(forms.ModelForm):
class Meta:
model = NewsletterUsers
fields = ['email']
def clean_email(self):
email = self.cleaned_data.get('email')
return email
and this is my views.py :
def newsletter_signup(request):
form = NewsletterUserSignUpForm(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
if NewsletterUsers.objects.filter(email=instance.email).exists():
messages.warning(request, 'Your Email Already Exist In Our DataBase.',
'alert alert-warning alert-dismissible')
else:
instance.save()
messages.success(request, 'Your Has Been Submitted To Our DataBase.',
'alert alert-success alert-dismissible')
context = {
'form': form,
}
return render(request, 'newsletter/subscribe.html', context)
the problem is here that this form has it own input which the input must put inside it but I want to design my own template and get input in my template then pass it to this form and my question is how do I can pass inputs in my .html template file to my form?
this is my html file and don't know to put what in href for input tag :
<form method="post" class="login100-form validate-form">
{% csrf_token %}
<span class="login100-form-title p-b-43">
Subscribe
</span>
<div>
<inputtype="email" name="Email">
<span class="label">Email</span>
</div>
<button type="submit" href="">
Subscribe
</button>
</div>
and what should I put in my href and how pass input to form from here? In addition, I'm sorry for writing mistakes in my question.
解决方案
据我了解,您想创建自己的自定义输入框,并且当该框被填充时,您希望表单输入框也被填充。
使用 display:none 隐藏表单输入框。创建自己的自定义输入框,填写自定义输入框时使用javascript填充表单输入框。前任 :
<script>
form_input_box = document.getElementById('id_of_form_input_box')
custom_input_box = documen.getElementById('id_of_custom_input_box')
$("id_of_custom_input_box").change(function(){
form_input_box.value = custom_input_box.value
});
</script>
推荐阅读
- python - 随机和弦创建器的概率集不会打印相同的音符两次
- robotframework - 如何使用机器人框架单击表单中的元素
- python - 如何转置特定的csv表?
- django - 创建和接收模型对象
- java - Android 代码在模拟器中打开时崩溃
- c - 尝试调用单独的函数时未定义的行为
- reactjs - 如何解决:“无法在未安装的组件上找到节点。” 在输入文本时
- python - tweepy 的搜索过滤器是否允许在字符串中搜索子字符串?
- python - 将列表中的元素数更改为数字 N
- hyperledger-fabric - 使用 VS Code Extension 容器不适用于 Hyperledger Fabric