request.POST parameters are missing - python

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>

Related

Csrf_token error on production. but no error on localhost

I just finished my first Django project and I deployed it on Railway. It deployed successfully but my contact form gives the csrf_token error on production but doesn't give an error on localhost.
I have csrf_token tag in my contact form on my html template.
<form action="{% url 'contact' %}" method="post">
{% csrf_token %}
<div class="form-group">
<input class="form-control" type="text" name="message-name" placeholder="Your Name" required>
</div>
<div class="form-group">
<input class="form-control" type="email" name="message-email" placeholder="Your Email" required>
</div>
<div class="form-group">
<textarea name="message" class="form-control" id=" placeholder="Message *" rows="7" required></textarea>
</div>
<div class="form-group ">
<button type="submit" class="form-control btn btn-primary" >Send Message</button>
</div>
</form>
what should i do about this
I assume you are using Django 4.0 or above, where CSRF_TRUSTED_ORIGINS is required to be in the settings.py.
You can specify this env variable as follows.
CSRF_TRUSTED_ORIGINS = ['https://YourSite.com', 'http://YourSite.com']
You can refer here and here.

request.files[] always seems to return error 400 bad request, been stuck for hours now

request.files always seems to return the error 400 bad request. Here is my implementation:
#app.route('/add', methods=['GET','POST'])
def add_stock():
if request.method == 'POST':
file = request.files['image']
<form action="{{page}}" method="POST" enctype="multipart/form-data"> <label for="book_name">Name:</label>
<input id="book_name" name="book_name" value="" type="text"><br><br>
<label for="book_author">Author:</label>
<input id="book_author" name="book_author" value="" type="text"><br><br>
<label for="date_published">Date Published:</label>
<input id="date_published" name="date_published" value="" type="date"><br><br>
<label for="isbn_13">ISBN-13 Number:</label>
<input id="isbn_13" name="isbn_13" value="" type="text"><br><br>
<label for="description">Description:</label>
<textarea id="description" name="description" rows="4" cols="50"></textarea><br><br>
<label for="image">Upload Image:</label>
<input id="image" name="image" type="file"><br><br>
<label for="trade_price">Trade Price:</label>
<input id="trade_price" name="trade_price" type="range" value="50" min="0" max="100" oninput="this.nextElementSibling.value = this.value">
<output>50</output><br>
<label for="retail_price">Retail Price:</label>
<input id="retail_price" name="retail_price" type="range" value="50" min="0" max="100" oninput="this.nextElementSibling.value = this.value">
<output>50</output><br>
<label for="quantity">Quantity:</label>
<input id="quantity" name="quantity" type="range" value="10" min="0" max="20" oninput="this.nextElementSibling.value = this.value">
<output>10</output><br>
<br>
<input value="Submit" type="submit"> </form>
{{page}} is the page I want to send the form data to which is passed through using url_for() when I render the template.
return render_template(add_stock.html, page=url_for('add_stock'). Excuse the mess of the form
I know that the request.files['image'] is the part that is making the bad request, I just cant figure out why its a bad request.
Also I know there is loads more form data but all of the other stuff in the form works perfectly its just when i try to upload an image.
Please help

Cannot resolve file 'signup'

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.

Uploaded Image Displaying in Form Data but not File Data in Flask

So I'm trying to upload an image inside a form and then submit it to a python server using Flask.
Right now my issue is that the file name is displaying in the form data, but when I call request.files I get an ImmutableDict, suggesting that the actual image wasn't uploaded, just the name of its file.
HTML FORM:
<form action="/workshops/new" method="post">
<label for=Title">Title</label>
<input type="text" id="Title" name="Title" minlength="10" size="150" value="{{ workshop_info[1] if workshop_info }}" class="form-control" required>
<label for="URL">URL</label>
<input type="text" id="URL" name="URL" minlength=10 tye="url" size="150" value="{{ workshop_info[3] if workshop_info }}" class="form-control" required>
<label for="Description">Description</label>
<textarea id="Description" name="Description" class="form-control" rows="10" required>{{ workshop_info[2] if workshop_info }}</textarea>
<label for="Image">Image</label>
<input type="file" id="Image" name="Image" accept="image/*">
<button class="btn btn-primary btn-large" type="submit">{{ "Update Workshop" if class_info else "Add Workshop" }}</button>
</form>
FLASK CODE:
#app.route('/workshops/new', methods=["POST", "GET"])
def workshopspage():
if request.method == "POST":
print(request.form)
print(request.files)
return redirect('/')
return render_template("workshop.html")
When the request hits, I get the following image in my terminal:
My guess is that there's something that I'm not specifying correctly in my html form, but beyond the name attribute, I'm not sure what else there is.
You need the enctype="multipart/form-data" to the form tag.
<form action="/workshops/new" method="post" enctype="multipart/form-data">

How to post data via form HTML?

I want to post the data to the server via form. How can I do this?
<div class="panel-body">
<form role="form" action="{% url 'login' %}" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="sender-email" class="control-label">Username:</label>
<div class="input-icon"> <i class="icon-user fa"></i>
<input id="sender-email" type="text" placeholder="Username" class="form-control email">
</div>
</div>
<div class="form-group">
<label for="user-pass" class="control-label">Password:</label>
<div class="input-icon"> <i class="icon-lock fa"></i>
<input type="password" class="form-control" placeholder="Password" id="user-pass">
</div>
</div>
<div class="form-group">
<input class="btn btn-primary btn-block" id="authenticate_user" type="submit" value="Submit">
</div>
</form>
</div>
My views.py
#csrf_exempt
def signin(request):
if request.method == 'POST':
print request.POST.get('sender-email') #prints None
username = request.POST['username'] # throws error, username not available.
password = request.POST['password']
How do I post the data without using jquery? I know of a way of posting through AJAX jquery, but dont wanna do that.
The 'name' attribute of input tags is set as keys of request.GET and request.POST.
Add name attribute to all your input tags.
<input name="username" id="sender-email" type="text" placeholder="Username" class="form-control email">
<input name="password" type="password" class="form-control" placeholder="Password" id="user-pass" >
POST data are populated using the name attribute of your input elements. Change your id attributes or add a name one accordingly. E.g:
<input type="password" class="form-control" placeholder="Password" name="user-pass">
You should also consider using a form to handle your data more easily.

Categories