I'm having trouble passing values from my template to view. Basically I created a form for user registration, and want to pass the values from the template to view so that I can construct a User object to add to my database. But somehow my "input" is not working when I add name={{user_form.username}}. Also, I want to pass values 0,1,2 respectively when I select "borrower", "libarrian" and "clerk", what can I do to implement this?
Below are my codes.
<form id="user_form" method="post" action="/sign_up/" class="form-signin" role="form">
{% csrf_token %}
<h2 class="form-signin-heading">Signup </h2>
{% if error%}
<div class="error"> Your registration has been unsuccessfull </div>
{% endif %}
<input type="text" class="form-control" name="username" value="{{user_form.username}}" placeholder="Username" required autofocus>
<input type="password" class="form-control" name="password1" placeholder="Password" value="" required>
<input type="password" class="form-control" name="password2" placeholder="Retype password" value="" required>
<select class="form-control">
<option value="2">Librarian</option>
<option value="0">Borrower</option>
<option value="1">Clerk</option>
</select>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign Up</button>
</form>
In forms.py
class UserForm(forms.Form):
username = forms.CharField(max_length=30)
password1 = forms.CharField(widget=forms.PasswordInput())
password2 = forms.CharField(widget=forms.PasswordInput())
In views.py
def sign_up(request):
registered = False;
error = False;
if request.method == 'POST':
user_form = UserForm(request.POST)
if user_form.is_valid():
registered = True
username = user_form.cleaned_data['username']
password = user_form.cleaned_data['password']
user = User.objects.create_user(username, None, password)
user.save()
else:
error = True;
else:
user_form = UserForm()
return render(request, 'books/sign_up.html',
{'user_form':user_form,
'registered':registered, 'error':error})
Below form should work.
<form id="user_form" method="post" action="/sign_up/" class="form-signin" role="form">
{% csrf_token %}
<h2 class="form-signin-heading">Signup </h2>
<input type="text" class="form-control" name="username" value="{{user_form.username}}" placeholder="Username" required autofocus>
<input type="password" class="form-control" name="password1" placeholder="Password" required>
<input type="password" class="form-control" name="password2" placeholder="Retype password" required>
<select class="form-control">
<option value="1">Librarian</option>
<option value="0">Borrower</option>
<option value="2">Clerk</option>
</select>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign Up</button>
</form>
Changes I have done are -
Change the names of username and password textboxes
Change the type for username textbox
Added value attribute in select control
Related
I am trying to save a contact form, but i am not using the django form but instead the input field.
I am getting the name, email address and message body from the html templates using input field like this <input type="text" id="name"> but the form doesn't get any data from the input field, it seems i am missing something but i cannot find out what is happening.
I have tried passing in the name, email and message into id="name" id="email" id="message" but nothing still happends.
This is my code
index.html
<form method="POST">
{% csrf_token %}
<input type="text" id="name" value="" />
<input type="text" id="email" value="" />
<input type="text" id="address" value="" />
<button type="button">Let's Withdraw</button>
</form>
views.py
if request.method == "POST":
name = request.POST.get("name")
email = request.POST.get("email")
message= request.POST.get("message")
print("Message Sent")
return redirect("core:index")
else:
pass
This is your form
<form method="POST">
{% csrf_token %}
<input type="text" id="name" value="" />
<input type="text" id="email" value="" />
<input type="text" id="address" value="" />
<button type="button">Let's Withdraw</button>
</form>
change to this
<form method="POST">
{% csrf_token %}
<input type="text" id="name" name="name" value="" />
<input type="text" id="email" name="email" value="" />
<input type="text" id="address" name="message" value="" />
<button type="submit">Let's Withdraw</button>
</form>
add the name=" ... " attribute in your input field and the corresponding values e.g name, email, address.
Change your button type to submit as your would be submiting a form
I have 3 form inputs that will be submitted when one master button is clicked to then be passed into a view as the request parameter. I would like to get the values of first_name, last_name and email inside my view using request.get(). When the button is clicked the values inside my form appear as None
HTML:
<div id="form_content">
<form action="" method="post">
<section class="form_inputs">
<label for="first_name">First Name:</label>
<input type="text" id="first_name">
</section>
<section class="form_inputs">
<label for="last_name">Last Name:</label>
<input type="text" id="last_name">
</section>
<section class="form_inputs">
<label for="email">Email:</label>
<input type="text" id="email">
</section>
<input type="submit" value="Submit">
</form>
</div>
views.py
def home(request):
form_response = request.GET.get("form_content")
print(form_response)
context = {"title": "Home"}
return render(request, "myApp/home.html", context)
first you need to add csrf_token in your code for post method and also give a name for each input like this :
<div id="form_content">
<form action="" method="post">
{% csrf_token %}
<section class="form_inputs">
<label for="first_name">First Name:</label>
<input type="text" name="first_name" id="first_name">
</section>
<section class="form_inputs">
<label for="last_name">Last Name:</label>
<input type="text" name="last_name" id="last_name">
</section>
<section class="form_inputs">
<label for="email">Email:</label>
<input type="text" name="email" id="email">
</section>
<input type="submit" value="Submit">
</form>
</div>
and then:
def home(request):
first_name = request.POST['first_name']
last_name = request.POST['last_name']
email = request.POST['email']
print(first_name, last_name, email)
context = {"title": "Home"}
return render(request, "myApp/home.html", context)
In your input tags, you have not passed the name parameter. Pass the name parameter in your input tags, as Django collects the data from the name tags.
For example, in your case, it must be:
<input type="text" id="id_first_name" name="first_name">
<input type="text" id="id_last_name" name="last_name">
<input type="text" id="id_email" name="email">
I'm trying to build a simple application using Leaflet in Flask, but I have a problem with the form. I want to send data from the form to my database but when I use the POST method, Flask doesn't want to read this method. When I used only GET, all values were empty in the database. When i used POST and GET nothing happened, none of the rows were added to the database.
forms.py
class EventForm(FlaskForm):
date_start = DateField(validators=[DataRequired()])
date_end = DateField(validators=[DataRequired()])
type = StringField(validators=[DataRequired()])
name = StringField(validators=[DataRequired()])
len_route = FloatField(validators=[DataRequired()])
mapa.html with Leaflet map and the form
<div id="fields">
<form action="" method="post">
<input type="text" id="route_len" class="form-control mb-2" name="route_len_input" placeholder="Długosc trasy">
<br>
<button id="draw-button" class="btn btn-success">Rysuj trase</button>
<br><br>
<input type="text" id="name" class="form-control mb-2" name="name_input" placeholder="Nazwa">
<br>
<input type="datetime-local" id="date_st" class="form-control mb-2" required name="date_st_input" placeholder="Data startu">
<br>
<input type="datetime-local" id="date_end" class="form-control mb-2" required name="date_end_input" placeholder="Data końcowa">
<br>
<select id="type" class="form-control mb-2" name="type_input">
<option></option>
<option value="Bieganie">Bieganie</option>
<option value="Rower">Rower</option>
<option value="Nordic walking">Nordic Walking</option>
</select>
<br>
<button type="submit" id="end-button" name="sub" class="btn btn-danger">Zakończ rysowanie</button>
<br><br>
</form>
</div>
routes.py
#app.route('/mapaa',methods=["GET","POST"])
def mapa():
if request.method == "POST":
data_pocz = request.form['date_st_input']
data_kon = request.form['date_end_input']
nazwa = request.form['name_input']
typ = request.form['type_input']
dlugosc = request.form['route_len_input']
event_database = Event(date_start=data_pocz, date_end=data_kon, type=typ, name=nazwa, len_route=dlugosc)
db.session.add(event_database)
db.session.commit()
return render_template('mapaa.html', title='Mapa')
You should link the form to your flask method
<form method="POST" action="/mapaa"> [...]
I am trying to make a login-page using django, I am facing troubles in getting POST parameters
login view:
def ProcLogin(request):
if request.method == 'POST':
account_name = request.POST.get('username','')
password = ToMd5(request.POST.get('password',''))
if not account_name or not password: return HttpResponse("invalid input")
template code:
<form method="post" action="{% url 'Main:login' %}" class="login_form">
{% csrf_token %}
<div class="form-group text-right">
<label for="username">User name:</label>
<input id="username" type="text" class="form-control box_shadow">
</div>
<div class="form-group text-right">
<label for="password">Password: </label>
<input id="password" type="password" class="form-control box_shadow">
</div>
<button type="submit" class="login_btn"></button>
</form>
Output when testing it:
invalid input
everything is supposed to be correct except the results aren't. Thank you.
I have created an HTML form trying to do simple registration.
The problem is that after clicking submit button no error appears but when I chech database the data from fields is not there.
signup.html
<form action="\polls\Registration" method="POST">
<div class="form-group mb15">
<input type="text" class="form-control" name="userName" placeholder="Enter Your Username" required>
</div>
<div class="form-group mb15">
<input type="password" class="form-control" name="password" placeholder="Enter Your Password">
</div>
<div class="form-group mb15">
<input type="text" class="form-control" name="fullName" placeholder="Enter Your Full Name">
</div>
<div class="form-group mb20">
<label class="ckbox">
<input type="checkbox" name="checkbox">
<span>Accept terms and conditions</span>
</label>
</div>
<div class="form-group">
<button class="btn btn-success btn-quirk btn-block">Create Account</button>
<br>
Already a member? Sign In Now!
</div>
</form>
forms.py
class RegistrationForm(forms.Form):
userName= forms.CharField(label='Username',max_length=100)
password = forms.CharField(label='Password', max_length=100)
fullName= forms.CharField(label='Full Name', max_length=100)
myview.py
def reg(request):
if request.method == 'POST':
the request:
form = forms.RegistrationForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/polls/signin')
else:
form = forms.RegistrationForm()
return render(request, 'signup.html', {'form': form})
urls.py
urlpatterns = [
url(r'^Registration', myview.reg,name='Registration'),
]
[SOLVED]
What I did is that I created a model to handle form processing.I also used modelForm instead of forms. For anyone having same issue check this