Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
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
[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
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
[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
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
# 10
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