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
PSA: Check your ALLOWED_HOSTS

I've seen a huge uptick in malicious entities trying to gain access across my django sites over the past 48 hours or so. Maybe it's just at my servers, but I suspect these attacks are widespread. This is a good time to verify that your production settings are not:

ALLOWED_HOSTS=['*']

(Edit: Also, while I'm at it, you should ALWAYS have **DEBUG=False** in production, and never commit your **SECRET_KEY** to git/svn)

If you're new to django, [Security in Django](https://docs.djangoproject.com/en/2.1/topics/security/) is the relevant docs page for this topic.

/r/django
https://redd.it/awqdtf
Help with query: how to filter objects based on boolean attribute on model with ForeignKey to that object?

Have a User Profile model with OneToOneField to User and given profiles:

`profiles = Profile.objects.filter(has_car=True, user__age__gt=30)`

Now want to only show profiles where user has verified EmailAddres in Allauth. I think a given User can have multiple EmailAddresses that are verified.

class EmailAddress(models.Model):

user = models.ForeignKey(allauth_app_settings.USER_MODEL)
email = models.EmailField(unique=app_settings.UNIQUE_EMAIL)
verified = models.BooleanField(default=False)
primary = models.BooleanField(default=False)

So how can I filter profiles to only show users where user has at least one EmailAddress with verified=True? Obviously one way would be to just iterate over profiles:

profile_set = []
for profile in profiles:
if profile.user.emailaddress_set.first().verified:
profile_set.add(profile)

This just is inefficient, especially for thousands of profiles. Any suggestions? Thanks all.

/r/django
https://redd.it/aws4rd
Raw SQL usage in Django

Hi everyone.
As we know, django orm each release has a good performance improvement and nice feature until now.

I just need your opinion, when you decide to use raw SQL?
Personally, I never use raw SQL due to orm is more maintainable to me.

/r/django
https://redd.it/awusjs
A Python utility for analyzing a given solution to the Einstein field equations.

I wanted to share with the community an open source python package I had been developing since November. It is called Spacetime Engine.

[github.com/spacetimeengineer/spacetimeengine](https://github.com/spacetimeengineer/spacetimeengine)

My name is Michael. I am a physicist who works as an engineer. My story is that in October 2018 I was preparing to submit my first physics publication, which reviewed a particular cosmological model that I had been working with for some time. I was at the point where I had completed all of my research and was fully prepared. The only problem was that at the time I felt concerned that I may have made some mistakes which went unnoticed, so I decided to write a computer algebra software to check the veracity of my solutions so that I could feel confident in my results. After using this new utility, I realized I had made some fairly significant errors in my approach, and so the software apparently did it’s job. At first I was quite frustrated because I had put a significant amount of time into this solution, but I realized I had a new ability to correct my own errors and expand upon what I already knew. I was now able to study

/r/Python
https://redd.it/awtry9
Any tutorial or guide for Social Authentication using django rest-auth and vuejs?

I'm trying to implement social app authentication using vuejs as front end but since it's the first time I'm doing it and am also not so familiar with vuejs, can anyone guide me through the process? Thank you all in advance. I've managed to implement rest-auth properly but now that I'm using the front end, I'm kind of lost and don't know

​

I have not been able to find a lot of helpful content to guide me. This is the first time I'm dealing with APIs in general. Any help would be appreciated. Thanks.

/r/django
https://redd.it/awwp7q
How to make scripts usable by non CLI users

I think this is a common problem for developers who like to create great CLI applications. How do you make it usable by your grandpa? Or by anyone whom the functionality of the script is useful but won't ever pop up a cli and type those commands.

Let's say I have a script that would do a simple task. Something that I could probably run as:

```
$ ./script --file /home/my/file.txt
```

Only ways I could think of how non CLI users can use this are the following:

* make a GUI - everyone knows a GUI and knows how to use a file chooser.
* wrap your python script in an executable (bat file maybe) and provide user prompts for the filename (they'd need to type in really long names though!).
* enforce restrictions with your script - i.e. rename file to this and that before using, must be executed from this directory etc.

Any other ideas besides those?

I'm usually working with web so I have little knowledge of making native GUI applications with Python. I'm also seeking recommendation on how I should go about this:

* build my own GUI - this looks nice https://pypi.org/project/PySimpleGUI/
* try out this awesome project https://github.com/chriskiehl/Gooey, although bummer that it only supports `argparse`


I'm

/r/Python
https://redd.it/awu33r
What is the standard(ish) way to make a library configurable?

We have an internal library that we want to make open-source, so we are trying to normalize its structure and workflows. This library has several configuration options that are currently split between hard-coded values and some environment variables, and we want to change it to have all the fields read from an hierarchical path:

* Get it from an environment variables.
* If it's not there - get it from a config file in the project directory.
* If it's not there - get it from a config file in the home directory.
* If it's not there - use the library's defaut.

Is there any standard way to do it? Tools like Flake8 read their configuration [from various files](http://flake8.pycqa.org/en/2.6.0/config.html#per-project), in various formats, but we want to have a single way to configure our project.

/r/Python
https://redd.it/awunwn
Django and lists..

Hello!

I have been stuck for a while now with this problem. I would need to create a list of lists for each of my users.User's then could select a some of their lists and input numbers there. I will later use these numbers to show a graph of the selected list.

PS I am pretty new to django so detailed answers would be good. Dont know all the little things yet.. Thanks already!

/r/django
https://redd.it/ax0lfs
Facebook Pixel Conversion Tracking + Flask Template Rendering

So I set my Facebook pixel to track when a page contains for example 'pay' in the URL, but the tracking doesn't seem to be working. I think its because the "pay" route doesn't actually render a template you see, it just redirects the user to an initial page that has its own /intro_page URL (which does render a template, where I could put the pixel code)

I am curious as to what the best approach here is.

I dont wanna double track conversions, so is there any way I can check if it's the first time they've visited?

/r/flask
https://redd.it/ax0ec6
Handling url_for parameters with flask.

Hey guys. I'm trying to handle some url_for parameters with flask.

I have a page that has a big table of parameters, and then a link that gets made at the end of each row.

For example:

<a href="{ url_for('test', test_type=test_select[4], service_type=test_select[3], test_level='80') }">Test</a>

However, when I take the link, the parameters are in the website URL.

I'm not finding great information online. Do I need to add @app.route options to handle these?

Thanks for the help! :)

EDIT: A little more specifics. Normally I have something like this:

@app.route('/test')
def test:
return(render_template(test.html, parameters and stuff))

I want my test route to also be able to handle a different return if it gets some parameters from a url_for link. I hope that makes sense. I will be around to try to clarify as much as I can!

/r/flask
https://redd.it/awuu1q
A 4YO's first python program!

Today, I taught my 4 year old cousin to write her first python program. Within a few minutes of explaining to her, she learnt to open the terminal, start python and write code (shown in the image). While explaining to her as to why we need to put parentheses and quotations, I told her that computers are dumb and that they don't understand us unless we follow some rules (syntax). She wrote down "cows have eight legs" to see how the computer responds to it. To her surprise, when the computer printed out the same, she yelled "Computers are really dumb, it says a cow has 8 legs!". It was bedtime by then and she said goodnight and would learn more programming tomorrow.

&#x200B;

Edit: image

https://i.redd.it/8srpe2iw22k21.png

/r/Python
https://redd.it/ax4gl3
Free Django Book Released

Hello r/django!

I wanted to share that my beta version of my full stack web development book is now live.

You can check it out on my website: [http://www.codexplore.io/books/crash-course/](http://www.codexplore.io/books/crash-course/)

The Github is here: [https://github.com/codexplore-io/crash-course-django](https://github.com/codexplore-io/crash-course-django)

This is just the "Beta version", so I am open to suggestions if anybody has them. I am using this book as an experiment in "open source publishing", so we shall see how it goes.

I am working on building a programming education website that allows anybody to learn how to program. There are many like it, but I truly believe that people learn much differently, so as many different explanations out there will make it easier for those to learn. Feel free to subscribe to learn more about what we are doing in the next couple of months as we are actively expanding across content mediums. We are going to be putting together a podcast as well, so if you have anyone in mind you would love to hear interviewed in the realm of STEM in general (or specifically Django), PM me or send us a contact on our website at [http://www.codexplore.io/contact](http://www.codexplore.io/contact).

&#x200B;

Please follow us on Twitter if you find our information useful: [https://twitter.com/codexplore\_io](https://twitter.com/codexplore_io)

&#x200B;

Any feedback on the book, website or company.

/r/django
https://redd.it/ax3hd5
OTP/Two factor authentication on Django?

I'm trying to add two factor authentication to my project, but the libriaries i found for Django have very little docs and i don't understand how to implement it into my own project. Is there an example, a tutorial or a widely used library i can follow to make my own implementation?

/r/django
https://redd.it/axd9nc