真実私はdjangoの新人です。私は入力タイプの値をキャプチャする方法を知りたいと思います.djangoのビューでは、request.POST [私のフィールドの方向を記入したいが、データベースに何もデータが入っていない場合は空にしてください。これは、これまで私が持っているコードです:htmlのPOSTフォームからDjangoのモデルを取得する
views.py
def formularioLleno(request):
form = InformationForm()
if request.method == 'POST':
form = InformationForm(request.POST or None)
if form.is_valid():
form.ubicacion = request.POST['direccion']
form.save()
#return redirect('formas.index')
return HttpResponseRedirect(reverse('index'))
else:
form = InformationForm()
data = {
'form': form,
}
return render_to_response('forma_form.html', data, context_instance=RequestContext(request))
forms.py
from django import forms
from .models import forma
class InformationForm(forms.ModelForm):
class Meta:
model = forma
fields = ('nombre', 'telefono')
models.py
class forma(models.Model):
nombre = models.CharField(verbose_name='nombre', max_length=50, unique=False)
telefono = models.CharField(verbose_name='telefono', max_length=10, unique=False)
ubicacion = models.CharField(verbose_name='ubicacion', max_length=15, unique=False)
forma_form.html
<div id="formularios">
{% block form_content %}
<form action="" method="POST">
{% csrf_token %}
{{ form.as_p }}
<div id="ubicacion"></div>
<input type="hidden" id="direccion" name="direccion" value="hello">
<button type="submit" onclick="alerta()">Contactar</button>
</form>
{% endblock form_content %}
コードにインデントエラーを修正してください。 – ozgur
ありがとうございます –
あなたのフォームクラス 'InformationForm(...)'には、2つのフィールドしか表示されないことが具体的に記載されています –