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
Is there a way to make shell context in Django like in Flask?

What I mean is whenever I use python manage.py shell I don't want to keep typing the imports I need from my models, such as from models import User. In Flask, there is a way so that whenever you call python manage.py shell User is already a variable in the shell that you can reference without importing. Can you do a similar thing in Django?

/r/django
https://redd.it/6woap1
Has anyone had problems with Django settings files not interpreting environment variables correctly?

-----------------------
EDIT: Solved! Thanks /u/dirtybutter and /u/mipadi!
-----------------------


I feel like I've come across a sort of weird problem here. On my local machine, I work in virtualenvs when I am building things. Then I primarily push projects up to Heroku.

On most of my projects that are public-facing I set them up using env variables for sensitive stuff like DEBUG, SECRET_KEY, etc. Well a couple days ago I had an issue where I couldn't get debug to turn off on one of my projects on Heroku even though I had this line in my settings file:

DEBUG = bool(os.environ.get('DJANGO_DEBUG', True))

On the Heroku side, just like I always do, I set an ENV variable with the cli like so:

heroku config:set DJANGO_DEBUG=" " #Empty string evals to false

So for whatever reason that wasn't working to turn off debug on Heroku, and it seemed like Heroku wasn't interpreting it correctly because when I would print the variable using the python shell, with that same code above (i.e. bool(...)) when running bash on the Heroku dyno it would still come back as true no matter what I set it as. Finally I adjusted the settings file in my project so now it says this:

if os.environ.get('DJANGO_DEBUG') == 'True':
dbg = True
else:
dbg = False

DEBUG = dbg #bool(os.environ.get('DJANGO_DEBUG', True))

That fixed the problem on Heroku. HOWEVER, now when I run the local python server on my local machine during development, all of a sudden I can't load my static files?!?! If I set an env variable locally on my machine called DJANGO_DEBUG and set it to 'True' (note it's a STRING not a boolean) the application still has debug off. If I change the settings file back to what it originally was, DEBUG = bool(os.environ.get('DJANGO_DEBUG', True)), then my static files load again and debug acts normally, but then debug won't be interpreted right on Heroku!

So basically, my question is: WTF!?!?!?! I don't know what changed...if it's a python versioning thing, a heroku thing, or a django version thing, or something totally different. I have at least 10 other projects running on Heroku AND locally right now and I have used this syntax for years so I don't know what the hell changed or why my settings file doesn't work right on my local machine when I use the "long form" if statement, but doesn't work right on Heroku when I use the short form.

Any ideas? Thanks!!

/r/django
https://redd.it/6wl4bq
Building this website using Flask. Feeback needed!

Hi,

I created [https://apileap.com](https://apileap.com) using Flask as the main framework and have to say that it has been an wonderful experience so far. There is a lot of nice extensions and I love being able to keep everything as simple as possible.

I do have a few questions though.

What deployment options do you recommend?
I currently use **nginx**, **gunicorn** and **gevent** on **EC2**. Do you think there are better options? How do you think I should scale if I ever need to? Anybody tried [Elastic Beanstalk](https://aws.amazon.com/elasticbeanstalk/) with Flask?

What do you think about the website? What can I improve? Do you think it might be useful?

The website offers only one API right now but the idea is to offer several easy to use APIs for small companies in the near future. Do you think it might be interesting for your company? What API would you like to see added? I was thinking about currency conversion API, IP geolocation API, phone number validation API, sms API, ...

For the curious, here are the dependencies I use:

- Flask==0.12.2
- Flask-SQLAlchemy==2.2
- Flask-Security==3.0.0
- Flask-DebugToolbar==0.10.1
- Flask-Migrate==2.0.4
- Flask-Admin==1.5.0
- SQLAlchemy==1.1.11
- gunicorn==19.7.1
- psutil==5.2.2
- gevent==1.2.2
- requests==2.18.3
- stripe==1.62.0
- websocket-client==0.44.0
- Fabric3==1.13.1.post1

And for the front end:

- jquery==3.2.1
- bootstrap==4.0.0-beta
- vue=2.4.2

Have a good day!

Tim

/r/flask
https://redd.it/6wpynb
What's everyone working on this week?

Tell /r/python what you're working on this week! You can be bragging, grousing, sharing your passion, or explaining your pain. Talk about your current project or your pet project; whatever you want to share.


/r/Python
https://redd.it/6wrgil
The Google Brain team will be back for its second AMA on September 13

Happy to announce the Google Brain team will be back in /r/MachineLearning for a second AMA on September 13. Hear how the team’s research and focuses have evolved in the past year, including updates on work in healthcare, creativity, human-AI interaction, and more.

A thread will be created before the official AMA time for those who won't be able to attend on that day.


/r/MachineLearning
https://redd.it/6wle45
I have created a collection of resources I used to learn python and Django! I'll post it here for everyone looking for a place to start!
https://mindweb.network/board/how-to-create-a-modern-website-with-django

/r/djangolearning
https://redd.it/6x0nob
Django Install Script for Ubuntu 16.04

Hey there, since I found this subreddit after already sharing this in the Django-subreddit, I wanna share my link to the original post here. :)

[Django Install on Ubuntu 16.04](https://www.reddit.com/r/django/comments/6wu4gu/django_install_i_am_just_a_newbie_and_i_did_this/?ref=share&ref_source=link)

Be free to comment here, or just use the real topic in /r/django as posted above.

/r/djangolearning
https://redd.it/6wxdxl
How dos createview trigger saving the model?

Digging through parts of the django 1.6 code (dont ask...) i was not able to find out how the basic CreateView issues saving the model, as is stated in its documentation (https://docs.djangoproject.com/en/1.11/ref/class-based-views/generic-editing/#django.views.generic.edit.CreateView)

Possibly (and hopefully) related things ive found out:

* CreateView's super classes dont handle saving in any way
* BaseModelForm seems to be the only part where the model instance of a form is pulled and actually saved
* BaseInlineFormSet has a save method, that actually calls save on a form, but i was not able to find out how that is related to actual forms, since most forms just inherit from Form, and not BaseModelForm...

Can someone please explain to me how all this works together?

/r/djangolearning
https://redd.it/6x363z
Tags for post

I was thinking if we can create tags for all the awesome resources that are shared on this sub?

For example we could have a tag for e books, online free courses, sample projects. Would be more than happy to contribute.

/r/Python
https://redd.it/6x5nol
What are the biggest drawbacks/cons of Python? And why choosing Python over JavaScript?

I want to be a Pentester (and It is my focus) and I like making the Front end side of websites and videogames. With JavaScript I can be a Frontend Developer and making games even for mobile thanks to React Native,Cordova and Ionic and inject Javascript on websites and many other things.

Actually I am interested on knowing more on Python and use It as a main language, but everyone say Python is only a "glue" language,It isn't easily scalable and maintainable than languages like Java and it isn't very suitable for concurrency and other things because it is slow.

On the other hand, I heard people very enthusiastic about Python and this language can do everything,but it is true?

Is Python more than a replacement of Bash?

Thank you!

/r/Python
https://redd.it/6x6oji
silly question about sessions

I'm using sessions in a current django project and recently got a 'Object of type 'date' is not JSON serializable' error.

Reading through the django docs on sessions I'm not clear why sessions use a serializer?

(I'm using the db to store the session)
From my understanding isn't just the session ID going to the browser in a cookie and the session data is stored in the db? The data in the session can be accessed via the ORM like a normal model. However when I look at the database table there is just one field, session_data (apart from expiry date and session id). Looking at the docs seems to be signed (but not encrypted? - not sure of the difference). So I'm not clear where the serializer is being used?

Additionally I was using django wizard before which stores intermediate data (such as the field causing the date issue above) in the session. I've now switched that part of the app to use seperate views for flexibility (signup wasn't just a linear path), but now have the 'not JSON serializable' error, how does django wizard get round this?

/r/djangolearning
https://redd.it/6x70xe