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
How to structure a custom built admin?

Hey guys, my application is getting much more complex and the inbuilt django admin isn't cutting it anymore.

So I've decided to build a custom admin for our most used flows.

How do I go about structuring this custom admin? Do I make a new app called `custom_admin` and house all the functionality in there, or do I just add a new `custom_admin.py` file to all my existing apps? (since I already have a lot of stuff in `admin.py`), or simply write the views in `views.py`?

One example I found is that `saleor` (a django project) have their custom admin dashboard as a separate app https://github.com/mirumee/saleor/tree/master/saleor/dashboard

How do you guys build a custom admin?



/r/django
https://redd.it/8e9bvd
Submitting multiple forms at once?

New to flask, hello :)

I have a page with two forms that are passed in as parameters.

return render_template(
'survey.html',
clientDetailsForm=ClientDetailsForm(),
houseDetailsForm=HouseDetailsForm())

I like having them as seperate forms because they represent seperate concepts.

On my survey.html I have a save button which I would like to save the values of both forms.

Additionally I have a "Send Report" button which ideally would first call the save view, then the sendReport view.

How can this be achieved?

Thanks!

/r/flask
https://redd.it/8e9fj1
Android development using Python?

Does anybody know the best way to develop for Android in Python? I've heard about a lot of packages /etc but no clear answer to "what is the best / standard / working".

/r/Python
https://redd.it/8e9wae
Are there any good continuous testing tools ?

Hello !

The C# team at my company is using a proprietary tool called [NCrunch](https://www.ncrunch.net/) which amongst other things allow us to have tests continiously running the appropriate unit tests when a part of the codebase is modified.

After working some weeks on the C# codebase, I found myself missing this feature when coding in Python.

Do you know a good tool for continuous testing for a Python codebase ?

Thanks in advance for your feedbacks :)

[EDIT 1]: We already have set up continuous integration on our [Jenkins](https://jenkins.io/) server.

[EDIT 2]: I do not try to find a replacement for NCrunch, but instead I am looking for its equivalent for Python codebases :)

[EDIT 3]: Teamates told me about [Wallaby.js](https://wallabyjs.com/) which is an continuous testing tool for JavaScript. If this kind of stuff exists for JS, I am confident that it can be done for Python too !

/r/Python
https://redd.it/8ea9lx
sqlalchemy queries

I am learning about flask and sqlalchemy and I have a question about the ways to make queries. I have seen three types of querys: cls.query, db.session.query and model.query. I want to know what are, if there are, the differences between these three methods? Or do the do the exact same thing?

/r/flask
https://redd.it/8eb8go
manage.py migrate: [migration_name] just needs the first few characters

EDIT: looking at the comments I think I should add some context. Say you did a migration and want to undo the database changes (so it doesn't screw up future migrations), you can run `manage.py migrate [app_label] [migration_name]` where migration_name is the filename of the previous migration you wish to revert to. What's not clear in the docs is that you can actually use just the first few characters of the filename, so long as there are no clashes.

---

I feel silly for not trying this before, but when doing `manage.py migrate app_label migration_name` you can actually just use the first few characters of a migration for `migration_name` e.g. if your migration file is 0018_auto_20170802_1822.py, you can just use `manage.py migrate app_label *0018*`.

This works because the function to get the migration uses `startswith()`: https://github.com/django/django/blob/master/django/db/migrations/loader.py#L134

Hope this helps if you're trying to undo a previous migration!

/r/django
https://redd.it/8eal1n
[AF] Request.referrer gives BuildError when redirecting to the same endpoint.

Hi, so I have a website that enables voting, much like on reddit, and I've tried to make it so that whenever the user votes he is instantly redirected back to wherever he initially viewed a post. So say I upvoted a post when viewing it, I expect to be redirected back to the post, not the front page.

Here is the code I used, taken from a snippet:

def redirect_url():
return request.args.get('next') or \
request.referrer or \
url_for('index')

And here is where it is implemented:

@app.route('/vote/<post_id>', methods=['POST'])
def vote(post_id):
post = Post.query.filter_by(id=post_id).first()
if post != None:
if post.upvotes == None:
post.make_vote_int()

if "upvote" in request.form:
post.upvotes = post.upvotes + 1
post.get_score()
db.session.commit()
if "downvote" in request.form:
post.downvotes = post.downvotes + 1
post.get_score()
db.session.commit()

return redirect(url_for(redirect_url()))

Using this I receive this error:

werkzeug.routing.BuildError: Could not build url for endpoint 'http://localhost:5000/

when voting wherever.

What am I doing wrong and how can I fix it?

Here is the github link, however I haven't yet pushed this code yet.

https://github.com/ModoUnreal/dopenet

/r/flask
https://redd.it/8efkzl
My first calculator with python and Tkinter...

/r/Python
https://redd.it/8ecfkv
I shipped my first real Django project, an easy way to make your own job board

Hey there y'all! I recently built [Seeker](https://seeker.company), an easy way for people to make their own paid job boards. I built this using Django in the back-end with Django Rest Framework and Vue.js on the front-end. Postgres is the database (hosted in Google Cloud) and it's deployed to Heroku.

/r/django
https://redd.it/8egd1h
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/8ekob5
Integrating S3 (static + media vs media)

This isn't specific to Django or S3 but conceptually it should be the same. I'm using Flask \+ Digital Ocean Spaces \(API is compatible with S3\) but I figured that this subreddit would have a larger reach, especially since I've found way more S3\+Django tutorials than Spaces\+Flask/S3\+Flask. I'm just looking for a conceptual understanding on how I should integrate S3.

My application is running on DO behind Nginx. Nginx is configured to serve my static files. I wanted to move my user\-uploaded media files onto S3.

I've noticed that most tutorials explain how to serve your static assets \(JS/CSS\) and media files from S3 using something like django\-storages \(flask\-s3 is the flask counterpart\) \+ boto3.

1. Would it be unidiomatic to configure S3 to serve only media files? I just wanted to make sure because I've just deployed my site a week ago and this is all new to me. This makes sense to me but I didn't want to go against the grain for no reason.
2. The only advantage for serving static assets \(JS/CSS\) from S3 is to take advantage of a CDN \(e.g. Cloudfront\) so that your reverse proxy \(e.g. Nginx\) can focus only on load balancing, right? I always had the idea that S3 was primarily for media files to save space on your VM. I never thought people used it for static assets too.

EDIT:

**TLDR: Static assets: Nginx or S3?**

/r/django
https://redd.it/8emhrq
Google PageSpeed is saying my server response time is too long (340ms). How can I improve it ?

I'm using PythonAnywhere for hosting.

I've installed django-debug-toolbar but don't really know how to use it. It's reporting 76 queries in 42ms for SQL, which doesn't seem to be causing my issue.

This is my PageSpeed link : https://developers.google.com/speed/pagespeed/insights/?url=https%3A%2F%2Fwww.tavolia.fr%2F

/r/djangolearning
https://redd.it/8emq9z