request.FILES empty uploading image on Django - python

Here is my HTML sending the request, printing request.FILES return <MultiValueDict: {}>.
Also i would ask how can i save my charged picture (is ok to only form.save()?)
<form action="send_news" method="POST" encrypt="multipart/form-data">
{% csrf_token %} {{ form.as_p }}
<p>
<label for="news_title">Your name: </label>
<input id="news_title" type="text" name="news_title" value="newtitle">
</p>
<p>
<label for="news_small_description">Your name: </label>
<input name="news_small_description" id="news_small_description" value="news_small_description">
</p>
<p>
<label for="news_description">Your name: </label>
<input name="news_description" id="news_description" value="news_description">
</p>
<p>
<label for="news_image">Your name: </label>
<input id="news_image" type="file" class="" name="news_image">
</p>
<input type="submit" value="Submit" />
</form>
printing request.FILES return <MultiValueDict: {}>.
Also i would ask how can i save my charged picture (is ok to only form.save()?)

The problem is you are using encrypt but you need to use enctype
e.g.
<form action="send_news" method="POST" enctype="multipart/form-data">
See What does enctype='multipart/form-data' mean? for more info if needed.

Related

Reset Password Using Django

I am trying to reset password using Django but I am getting the following error:
Method Not Allowed (POST): /reset/done/
Method Not Allowed: /reset/done/
Below are my forms:
Form Which Sent Link To My Email
<form action="{% url 'reset_pass' %}" method="post">
{% csrf_token %}
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Email" required autofocus>
</div>
<input type="submit" value="Send Email" class="btn btn-primary btn-block">
</form>
Form Which I Get On Clicking That Link
<form action="{% url 'password_reset_complete' %}" method="post">
{% csrf_token %}
<div class="form-group">
<input type="password" class="form-control" name="new_pass" placeholder="New Password" required autofocus>
</div>
<div class="form-group">
<input type="password" class="form-control" name="confirm_pass" placeholder="Confirm Password" required>
</div>
<input type="submit" value="Update Password" class="btn btn-primary btn-block">
</form>
URL
path('reset/pass/', views.reset_pass, name='reset_pass'),
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(template_name="feebeeapp/reset_form.html"), name='password_reset_form'),
path('reset/done/', auth_views.PasswordResetCompleteView.as_view(template_name='feebeeapp/login.html'), name='password_reset_complete')
Not sure, what I am doing wrong. Can someone please guide me?
Your form is submitting to the wrong view. You are supposed to make a POST request to a PasswordResetConfirmView view.
If your PasswordResetConfirmView is thus registered as:
path('reset/', auth_views.PasswordResetView.as_view(), name='password_reset')
then in your form you work with:
<form action="{% url 'password_reset' %}" method="post">
…
</form>
Normally you already use this view to render the form. So you submit a POST request to the same view.
This view will send an email with a reset link, and then will redirect to the password_reset_complete view.

can't get the POST parameters in django

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.

How can submit form with array fields and process on view?

I am trying to submit form with array fields and process on Django view. Also, I want to compare the submitted form data with correct answer table data.
question.html:
<form method="post" class="form-inline">
{% csrf_token %}
{% for question in questions %}
<h3>{{ question.title }}</h3>
<div class="radio">
<label class="radio-inline">
<input type="radio" name="ans{{ question.id }}" value="{{ question.option.0 }}">
{{ question.option.0 }}
</label>
</div>
<div class="radio">
<label class="radio-inline">
<input type="radio" name="ans{{ question.id }}" value="{{ question.option.1 }}">
{{ question.option.1 }}
</label>
</div>
<div class="radio">
<label class="radio-inline">
<input type="radio" name="ans{{ question.id }}" value="{{ question.option.2 }}">
{{ question.option.2 }}
</label>
</div>
{% endfor %}
<button type="submit" class="btn btn-success" name="ans_submit">Submit</button>
</form>
view.py:
if request.method == 'POST':
if(request.POST.get('ans_submit')):
temp_context["submitted_answers"] = request.POST.get("ans", "") # Receive as an array?
Expecting to get expert advice.
Thanks
Actually what is posted to you is ans0, ans1, ..., ansN.
So what you want to do is something like this:
answer_fields = [field for field in request.POST if field.startswith('ans')]
for field in answer_fields:
# Do something with `field`...
print(request.GET[field])
Additionally, you might want to check that the latter part is numeric, like so:
[field for field in request.POST
if field.startswith('ans') and field[3:].isnumeric()]

recover the data from models to views in form html in django

please i tried with this How can I build multiple submit buttons django form?
but it doesnt work for me and im new to django programming
my aim is : when the user enter the identifiant when he clicks on the button recherche i want that all information come on the input tags here is my code
views.py:
def modifierEmploye(request):
if request.method=='POST':
emp2=EmployeForm(request.POST)
if emp2.is_valid():
if 'recherche' in request.POST:
return HttpResponse("recherche")
elif 'enregistrer' in request.POST:
return HttpResponse("enregistrer")
else:
emp2=EmployeForm()
return render(request,"GPI/modifierEmploye.html",locals())
html:
<form action="" method="POST">
{% csrf_token %}
<p> <input id="id" maxlength="200" name="id" type="text" placeholder="identifiant" /></p><br>
<p> <input id="id_nom" maxlength="200" name="nom" type="text" placeholder="Nom" /></p><br>
<p><input id="id_prenom" maxlength="200" name="prenom" type="text" placeholder="Prenom"/></p><br>
<p> <input id="id_departement" maxlength="200" name="departement" type="text" placeholder="Département" /></p><br>
<p> <input id="id_poste" maxlength="200" name="poste" type="text" placeholder="Poste" /></p><br>
<p> <input id="id_telephone" maxlength="200" name="telephone" type="text" placeholder="Téléphone" /></p><br>
<p><input id="id_email" maxlength="200" name="email" type="email" placeholder="Email" /></p>
<p> <input id="id" maxlength="200" name="id" type="text" placeholder="Nom" /></p><br>
<br>
<input type="submit" value="Rechercher" name="recherche" />
<input type="submit" value="Enregistrer" name="enregistrer" />
</form>
this program doesn't work, please qomeone to help me
change your template with this, it will work :
<form action="" method="POST">
{% csrf_token %}
{{emp2.as_p}}
<input type="submit" value="Rechercher" name="recherche" />
<input type="submit" value="Enregistrer" name="enregistrer" />
</form>

In Flask, how do you update an input field in a template?

Being am new to Flask I read the manuals of Flask an Jinja and I know how to read a filed from a template, but not how to update the template.
My template has the following structure which I modified from the doco
<form action="{{ url_for('add_entry') }}" method=post class=add-entry>
<dl>
<dt>Documet:
<dd><input name=text size = 80 value = {{ AO_sDocument }} > </input >
<dd><input type=submit value=Analyse>
</dl>
</form>
and my Python has the following line
render_template('show_entries.html', AO_sDocument=AO_sDocument )
Yest this line does not seem to update the field.
Thanks!
As Jon Clements points out, your HTML has some markup errors. In order to guarantee all browsers can parse the HTML correctly, your template markup should look more like the following:
<form action="{{ url_for('add_entry') }}" method="post" class="add-entry">
<dl>
<dt>Document:</dt>
<dd><input name="text" size="80" value="{{ AO_sDocument }}" /></dd>
<dd><input type="submit" value="Analyse" /></dd>
</dl>
</form>
Alternately, you may wish to drop the use of <dl> and <dt> in favor of a <label>, which are ever so slightly more semantically correct:
<form action="{{ url_for('add_entry') }}" method="post" class="add-entry">
<label for="text">Document:</label>
<input id="text" name="text" size="80" value="{{ AO_sDocument }}" />
<input type="submit" value="Analyse" />
</form>

Categories

Resources