(only in Django 2 & 2.0.1)
if I use {{form}} it works but if I use {{form.field}} it disappears all
<form method="POST" class="post-form" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" id="salva" class="btn btn-primary" />
</form>
forms.py
class PreventivoForm(forms.ModelForm):
class Meta:
model = Preventivo
fields = ['cliente','prestazione1', 'ripetizione1', 'prestazione2', 'ripetizione2', 'prestazione3', 'ripetizione3', 'prestazione4', 'ripetizione4', 'prestazione5', 'ripetizione5']
When you use {{form}} then it gets all these value:
<tr><th><label for="id_username">Username:</label></th><td><input type="text" name="username" autofocus maxlength="254" required id="id_username" /></td></tr>
<tr><th><label for="id_password">Password:</label></th><td><input type="password" name="password" required id="id_password" /></td></tr>
But if you use {{form.username}} and {{form.password}} then:
<input type="text" name="username" autofocus maxlength="254" required id="id_username" />
<input type="password" name="password" required id="id_password" />
So you will only see the input field not label for that input field
Related
I am newbie to Django and i need help to solve this problem. It states that cannot resolve file'signup'.
<h3>Create your account!</h3>
<form method="post" action="/signup">
{% csrf_token %}
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Create A Username (use only letters and numbers)" Required>
</div>
You need to have a button to submit.
<input type="submit">
Try that :
<h3>Create your account!</h3>
<form method="post" action="/signup">
{% csrf_token %}
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Create A Username (use only letters and numbers)" Required>
</div>
<input type="submit">
</form>
Your form was not a valid one and was not submitting anything.
Hello I am trying to pass some values from my signup form to a django view but for some reason the values are missing.
My view.py :
#csrf_exempt
def signup(request):
if request.method =='POST':
for key, value in request.POST.lists():
print(" DEBUG %s %s" % (key, value))
print(request.POST['name'],email=request.POST['email'])
return render(request, 'front_app/login.html')
My register.html :
<form class="form" method="post" action="{% url 'signup' %}" name="submitForm">
{% csrf_token %}
<input type="text" class="name" placeholder="Name" id="name" required>
<input type="email" class="email" placeholder="Email" id="email" required>
<div>
<input type="password" class="password" placeholder="Password (8 characters minimum)" id="password" minlength="8" required>
</div>
<input type="submit" class="submit" value="Register" >
</form>
and my urls.py :
urlpatterns = [
# other not related urls
path('signup', views.signup, name='signup'),
]
My problem is that when the views.signup function is running it is missing all three values that I am trying to send : name, email and password.
It is a POST request (and not GET) but the only values in the QueryDict are the "encoding" and "csrfmiddlewaretoken" values.
Any help is appreciated!
As Selcuk comment,
The request.POST data is a dict filled with the name of the input fields of the HTML form.
Try with :
<form class="form" method="post" action="{% url 'signup' %}" name="submitForm">
{% csrf_token %}
<input type="text" class="name" placeholder="Name" id="name" name="name" required>
<input type="email" class="email" placeholder="Email" id="email" name="email" required>
<div>
<input type="password" class="password" placeholder="Password (8 characters minimum)" id="password" minlength="8" name ="password" required>
</div>
<input type="submit" class="submit" value="Register" >
</form>
I have this form in html of my site
<form class="row contact_form" action="." method="post" novalidate="novalidate">
{% csrf_token %}
<div class="col-md-12 form-group p_star">
<input type="text" class="form-control" id="firstname" name="first_name" value=""
placeholder="First Name" required>
</div>
<div class="col-md-12 form-group p_star">
<input type="text" class="form-control" id="lastname" name="last_name" value=""
placeholder="Last Name" required>
</div>
<div class="col-md-12 form-group p_star">
<input type="text" class="form-control" id="mobile" name="mobile" value=""
placeholder="Mobile Number" required>
</div>
<div class="col-md-12 form-group p_star">
<input type="text" class="form-control" id="email" name="email" value=""
placeholder="Email" required>
</div>
<div class="col-md-12 form-group p_star">
<input type="password" class="form-control" id="password" name="password" value=""
placeholder="Password" required>
</div>
<div class="col-md-12 form-group p_star">
<input type="password" class="form-control" id="cpassword" name="cpassword" value=""
placeholder="Confirm Password" required>
</div>
<div class="col-md-12 form-group">
<button type="submit" value="submit" class="btn_3">
SignUp
</button>
</div>
</form>
Now the problem I am facing is:
1)When I try to post data it shows me an error that username is required which i dont want(create_user() missing 1 required positional argument: 'username').
2)There is no mobile number section in auth_user which i want.
I have tried reading the django docs but couldnt understand(OnetoOne thing and custom usermodel thing I couldnt understand both)
Here is my views.py
def signup(request):
if request.method == "POST":
first_name=request.POST['first_name']
last_name=request.POST['last_name']
email=request.POST['email']
mobile=request.POST['mobile']
password=request.POST['password']
cpassword=request.POST['cpassword']
username=request.POST['username']
user=User.objects.create_user(first_name=first_name,last_name=last_name,email=email,password=password,mobile=mobile)
user.save();
return redirect('/')
else:
return render(request,"signup.html")
(The indentations are correct)
Also I want that the users can login using mobile number or email but I dint find any explanation for that.
from django.contrib.auth.models import User
...
class MyUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
# add other custom fields
first_name = models.CharField(...)
...
# create as
user = User.objects.create_user(username=email,...)
my_user = MyUser.objects.create(user=user, first_name=first_name, ...)
# authenticate as
user.authenticate(username=email, password=password)
This might help
Django default auth
Why form is not valid in django!
hi
i dont know why my form is not valid!
i tried this
class SignupForm(forms.Form):
first_name = forms.CharField(required=True, max_length=35)
last_name = forms.CharField(required=True, max_length=35)
username = forms.CharField(required=True, max_length=50)
email = forms.EmailField(required=True)
password = forms.CharField(required=True, min_length=8, max_length=64)
confirm_password = forms.CharField(required=True, min_length=8, max_length=64)
template.html
<form id="signupForm" action="" method="POST" accept-charset="utf-8" class="form" role="form">
{% csrf_token %}
<div class="row">
<div class="col-xs-6 col-md-6">
<input id="first_name" type="text" autocomplete="off" name="firstname" value="" class="form-control input-lg" placeholder="First Name" />
</div>
<div class="col-xs-6 col-md-6">
<input id="last_name" type="text" autocomplete="off" name="lastname" value="" class="form-control input-lg" placeholder="Last Name" />
</div>
</div>
<input id="username" type="text" autocomplete="off" name="username" value="" class="form-control input-lg" placeholder="Choose Username" />
<input id="email" type="text" autocomplete="off" name="email" value="" class="form-control input-lg" placeholder="Your Email" />
<input id="password" type="password" autocomplete="off" name="password" value="" class="form-control input-lg" placeholder="Password" />
<input id="confirm_password" type="password" name="confirm_password" value="" class="form-control input-lg" placeholder="Confirm Password" />
<div class="topmargin active">
<div class="row" style="display: flex;">
<div class="col-xs-5 col-md-5">
<label>I'm </label>
<select name="month" class = "form-control input-lg">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</div>
<br />
<span class="help-block">By clicking Create my account, you agree to our Terms and that you have read our Data Use Policy, including our Cookie Use.</span>
<button class="btn btn-block signup-btn" type="submit">Create my account</button>
</form>
i think it's because i dont pass the form to template, but i dont wanna do that!
it mixes the responsive style
commands are welcome
thank you
The problems are here:
<input id="first_name" type="text" autocomplete="off" name="firstname" value="" class="form-control input-lg" placeholder="First Name" />
<input id="last_name" type="text" autocomplete="off" name="lastname" value="" class="form-control input-lg" placeholder="Last Name" />
In your form you use first_name and last_name with underscore, but on input they are without the underscore.
You need to change the input names to match the form fields:
<input id="first_name" type="text" autocomplete="off" name="first_name" value="" class="form-control input-lg" placeholder="First Name" />
<input id="last_name" type="text" autocomplete="off" name="last_name" value="" class="form-control input-lg" placeholder="Last Name" />
How can I retrieve the data from POST in Flask using a for loop in python. I want to dynamically build pages and this would be very useful.
schema=['username', 'phone', 'postal_code', 'address', 'email']
for i in schema:
if request.form.get(i):
db.execute("UPDATE manpower SET :field=:input WHERE username=:username", field=i, input=request.form.get(i), username=request.form.get("user"))
else:
print(request.form.get(i))
Here is my html, as you can see the text names match what I have in schema but for some reason request.form.get(i) always returns None in my python code.
<form action="/manpower" method="post">
<fieldset>
<div class="form-group">
<input type="submit" name="submit" value="query">
<input type="submit" name="submit" value="addNew">
<input type="submit" name="submit" value="update">
</div>
<div class="form-group">
<select name="user">
<option value=""></option>
<option value="bo">bo</option>
<option value="dog2">dog2</option>
<option value="dunkin">dunkin</option>
<option value="tom">tom</option>
</select>
</div>
<div class ="form-group">
<input autocomplete="off" autofocus class="form-control" name="username" placeholder="username: bo" type="text"/>
<input autocomplete="off" autofocus class="form-control" name="phone" placeholder="phone: None" type="text"/>
<input autocomplete="off" autofocus class="form-control" name="postal_code" placeholder="postal_code: None" type="text"/>
<input autocomplete="off" autofocus class="form-control" name="address" placeholder="address: None" type="text"/>
<input autocomplete="off" autofocus class="form-control" name="email" placeholder="email: None" type="text"/>
</div>
</fieldset>
</form>
I found the answer and it's pretty simple. request.form sends everything that was posted from flask and from that I can figure out how to iterate my loops. Thanks for the help guys.
request.form