我是django的新手。我正在尝试访问HTML表单数据。我正在为此写视图。
<form>
<input type="text" class="form-control"id="inline"placeholder="Username">
<div class="input-group">
<div class="input-group">
<div class="input-group-text"><i class="pass"></i></div>
</div>
<input type="email"class="form
control"id="example"placeholder="Password">
</div>
<button type="submit" class="button-primary mt-3">Submit</button>
答案 0 :(得分:0)
对于POST表单,您可以执行以下操作:
创建一个 forms.py
from django import forms
class ContactForm(forms.Form):
first_name = forms.CharField(max_length=30, required=True)
last_name = forms.CharField(max_length=30, required=True)
然后进行查看(请求):
views.py
from django.shortcuts import render
from .forms import ContactForm
def formView(request):
form = ContactForm(request.POST)
if form.is_valid():
#Do Something If Form Is Valid
return render(request, "form.html", {'form': form})
在您的HTML文件中,因此在我的示例form.html中,您可以通过以下方式获取它:
form.html
<form id="contact_form_post" method="post">
{% csrf_token %}
{{ form.first_name }}
{{ form.first_name }}
<div class="form-actions">
<button type="submit">Submit</button>
</div>
</form>
答案 1 :(得分:0)
#views.py
class YourView(LoginRequiredMixin, CreateView):
model = model
template_name = 'your.html'
form_class = YourForm
#forms.py
class YourModelForm(forms.ModelForm):
class Meta:
fields = ('yourfields')
#models.py
class YourModel(models.Model):
yourfields= models.CharField/IntegerField/ForeignKey/ManyToMany
#your.html
<form method="POST">
<input type="text" class="form-control"id="inline"placeholder="Username">
<div class="input-group">
<div class="input-group">
<div class="input-group-text"><i class="pass"></i></div>
</div>
<input type="email"class="form
control"id="example"placeholder="Password">
</div>
<button type="submit" class="button-primary mt-3">Submit</button>
</form>