Sending a file via POST using Django using Javascript instead of HTML Form (X-post from /r/webdev)
I have a PDF file saved in a variable 'report.'
I am using Django and want to pass it to a function in my views.py file using POST --
@xframe_options_exempt
@csrf_exempt
def process_report(request):
if request.method == 'POST'
report_file = request.FILES['docfile']
....
return response
How can I send a ajax POST request with this file? The filetype is a PDF.
I was using an HTML form to do this before, but I wanted to style the form upload using javascript. I tried styling the button using javascript (changing colors and text depending on file type uploaded), but the file was no longer being able to be passed through the POST request. Now I just have the file saved in a javascript variable, and am trying to just pass it via POST without using form. I understand it's impossible to prepopulate a form with a file.
My old code:
<form id="boring" action="{% url "process_report" %}" method="post"
enctype="multipart/form-data">
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.docfile.label_tag }} {{ form.docfile.help_text }
{{ form.docfile.errors }}
{{ form.docfile }}
</form>
What I have been trying to do with ajax
var formdata = new FormData(report);
$.ajax({
url: 'process_report',
type: 'POST',
processData: false,
contentType: false,
dataType : 'application/pdf',
data: {
'content': 'formdata',
'csrfmiddlewaretoken': '{{ csrf_token }}',
}
/r/djangolearning
https://redd.it/7fcdfi
I have a PDF file saved in a variable 'report.'
I am using Django and want to pass it to a function in my views.py file using POST --
@xframe_options_exempt
@csrf_exempt
def process_report(request):
if request.method == 'POST'
report_file = request.FILES['docfile']
....
return response
How can I send a ajax POST request with this file? The filetype is a PDF.
I was using an HTML form to do this before, but I wanted to style the form upload using javascript. I tried styling the button using javascript (changing colors and text depending on file type uploaded), but the file was no longer being able to be passed through the POST request. Now I just have the file saved in a javascript variable, and am trying to just pass it via POST without using form. I understand it's impossible to prepopulate a form with a file.
My old code:
<form id="boring" action="{% url "process_report" %}" method="post"
enctype="multipart/form-data">
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.docfile.label_tag }} {{ form.docfile.help_text }
{{ form.docfile.errors }}
{{ form.docfile }}
</form>
What I have been trying to do with ajax
var formdata = new FormData(report);
$.ajax({
url: 'process_report',
type: 'POST',
processData: false,
contentType: false,
dataType : 'application/pdf',
data: {
'content': 'formdata',
'csrfmiddlewaretoken': '{{ csrf_token }}',
}
/r/djangolearning
https://redd.it/7fcdfi
reddit
Sending a file via POST using Django using... • r/djangolearning
I have a PDF file saved in a variable 'report.' I am using Django and want to pass it to a function in my views.py file using POST -- ...
Getting started with Python for R developers
http://n-s-f.github.io/2017/03/25/r-to-python.html
/r/Python
https://redd.it/7fc4q1
http://n-s-f.github.io/2017/03/25/r-to-python.html
/r/Python
https://redd.it/7fc4q1
n-s-f.github.io
Getting started with Python for R developers
If you work in R and are curious about using Python, but haven’t quite known howto get started, this blog post is for you. I’ve come across quite a few R use...
Python Top 10 Articles - Nov 2017
https://medium.com/@Mybridge/python-top-10-articles-for-the-past-month-v-nov-2017-f46fcaa36e4b
/r/Python
https://redd.it/7fdv49
https://medium.com/@Mybridge/python-top-10-articles-for-the-past-month-v-nov-2017-f46fcaa36e4b
/r/Python
https://redd.it/7fdv49
Medium
Python Top 10 Articles for the Past Month (v.Nov 2017)
For the past month, we ranked nearly 1,000 Python articles to pick the Top 10 stories that can help advance your career (1% chance).
[AF][Flask-SQLAlchemy] How do I execute this query?
Hello, everyone!
I have the following relationship ([shitty diagram](https://i.imgur.com/QXgidVX.png)) and would like to fetch row(s) from the `Payments` table. Now obviously, what gets returned is `(id, employer_id, employee_id, payment_date)`. What is the correct way to fetch `(id, employer_name, employee_name, payment_date)`?
What I currently have:
# views.py
@app.route("/dashboard")
@login_required
def dashboard():
payments = Payment.query.all()
# TODO: Find a 'cleaner' way to fetch the relevant info for payments.
for payment in payments:
payment.employee_name = Employee.query.filter_by(id=payment.employee_id).first().name
payment.employer_name = Employer.query.filter_by(id=payment.employer_id).first().name
return render_template("dashboard.html", payments=payments)
---
# dashboard.html
...
<tbody>
{% for payment in payments %}
<tr>
<td>{{ payment.id }}</td>
<td>{{ payment.employer_name }}</td>
<td>{{ payment.employee_name }}</td>
<td>{{ payment.payment_date }}</td>
</tr>
{% endfor %}
</tbody>
Admittedly, I don't have much knowledge about databases aside from the basic ORM commands. I don't even know the correct terms to search for! Anyway, I'd like to know:
1. What is the correct way of doing what I did with the above code?
2. What would it look like in plain SQL?
3. What topics should I be reading up on?
Thanks a lot, r/flask!
/r/flask
https://redd.it/7aqvcl
Hello, everyone!
I have the following relationship ([shitty diagram](https://i.imgur.com/QXgidVX.png)) and would like to fetch row(s) from the `Payments` table. Now obviously, what gets returned is `(id, employer_id, employee_id, payment_date)`. What is the correct way to fetch `(id, employer_name, employee_name, payment_date)`?
What I currently have:
# views.py
@app.route("/dashboard")
@login_required
def dashboard():
payments = Payment.query.all()
# TODO: Find a 'cleaner' way to fetch the relevant info for payments.
for payment in payments:
payment.employee_name = Employee.query.filter_by(id=payment.employee_id).first().name
payment.employer_name = Employer.query.filter_by(id=payment.employer_id).first().name
return render_template("dashboard.html", payments=payments)
---
# dashboard.html
...
<tbody>
{% for payment in payments %}
<tr>
<td>{{ payment.id }}</td>
<td>{{ payment.employer_name }}</td>
<td>{{ payment.employee_name }}</td>
<td>{{ payment.payment_date }}</td>
</tr>
{% endfor %}
</tbody>
Admittedly, I don't have much knowledge about databases aside from the basic ORM commands. I don't even know the correct terms to search for! Anyway, I'd like to know:
1. What is the correct way of doing what I did with the above code?
2. What would it look like in plain SQL?
3. What topics should I be reading up on?
Thanks a lot, r/flask!
/r/flask
https://redd.it/7aqvcl
Making a test with django
I'm trying to make a political test with Django.
Luckily, the official tutorial deals with a pretty similar concept- making a "polls" app.
I have been trying to use the Django API for a bit now but I haven't been successful. Unlike the polls app, all my questions in my test will have the same five choices:
* Strongly Agree
* Agree
* Neutral/Don't know
* Disagree
* Strongly Agree
How would I make it so that when I create a new Question, it is automatically assigned those 5 options as a choice, rather than individually assigning the choices per question? This is important because the test will need to store and track how many times "Agree" was chosen, so it has to be the same variable, if that makes sense.
Thanks
/r/djangolearning
https://redd.it/7f023w
I'm trying to make a political test with Django.
Luckily, the official tutorial deals with a pretty similar concept- making a "polls" app.
I have been trying to use the Django API for a bit now but I haven't been successful. Unlike the polls app, all my questions in my test will have the same five choices:
* Strongly Agree
* Agree
* Neutral/Don't know
* Disagree
* Strongly Agree
How would I make it so that when I create a new Question, it is automatically assigned those 5 options as a choice, rather than individually assigning the choices per question? This is important because the test will need to store and track how many times "Agree" was chosen, so it has to be the same variable, if that makes sense.
Thanks
/r/djangolearning
https://redd.it/7f023w
reddit
Making a test with django • r/djangolearning
I'm trying to make a political test with Django. Luckily, the official tutorial deals with a pretty similar concept- making a "polls" app. I...
[P] I made a movie recommendation system in C as my first ML project in University
https://imgur.com/a/dk3IY
/r/MachineLearning
https://redd.it/7fa5hk
https://imgur.com/a/dk3IY
/r/MachineLearning
https://redd.it/7fa5hk
Imgur
Movie Recommendation System using C
Imgur: The most awesome images on the Internet.
Online Text Multiplayer RPG engine?
What would you use if you wanted to making something like Kingdom of Loathing , a browser-based rpg with light multiplayer interactions ?
/r/Python
https://redd.it/7fdkux
What would you use if you wanted to making something like Kingdom of Loathing , a browser-based rpg with light multiplayer interactions ?
/r/Python
https://redd.it/7fdkux
reddit
Online Text Multiplayer RPG engine? • r/Python
What would you use if you wanted to making something like Kingdom of Loathing , a browser-based rpg with light multiplayer interactions ?
Asyncio Semaphores and Bounded Semaphores Tutorial
https://www.youtube.com/watch?v=uvM-JYnz1Mw&feature=youtu.be
/r/Python
https://redd.it/7fel9h
https://www.youtube.com/watch?v=uvM-JYnz1Mw&feature=youtu.be
/r/Python
https://redd.it/7fel9h
YouTube
Python Asyncio Semaphores and Bounded Semaphores Tutorial
● SUBSCRIBE to see more of my Videos & hit that LIKE button to support the channel! Hi Everyone, in this tutorial we will be covering both semaphores and bou...
How to Configure Role Based Access Control in Django
https://hashedin.com/2017/05/30/configure-role-based-access-control-in-django/?utm=sm&source=fb
/r/django
https://redd.it/7few92
https://hashedin.com/2017/05/30/configure-role-based-access-control-in-django/?utm=sm&source=fb
/r/django
https://redd.it/7few92
HashedIn
How to Configure Role Based Access Control in Django
Improve your application’s security by adding a role based access control, using Django user authentication, authorization and built-in models.
Built this simple To-Do and Calendar app using Python-Flask.
https://github.com/sujaybr/Tasker
/r/flask
https://redd.it/7ff7le
https://github.com/sujaybr/Tasker
/r/flask
https://redd.it/7ff7le
GitHub
sujaybr/Tasker
Tasker - A simple python-Flask Application to create your own local to-do list, Diary with tags and a simple Calendar.