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
Case Sensitivity

I want to set my site up so that users can login and don't have to worry about case sensitivity. So then you can either "Username" or "username". What would be the best way to do this. Currently using SQLite3

/r/flask
https://redd.it/7arbza
Building a Simple Website with Angular 4 and Django (and deploy it to heroku)
http://devarea.com/building-a-simple-website-with-angular-4-and-django-and-deploy-it-to-heroku/

/r/django
https://redd.it/7athb5
Some elegance/'good practice' questions.

Hey,
I've got a couple of questions about flask. I'd be great if you guys could answer one/some!

I've got this database:

field1 field 2 field3
1 2 1
3 1 3
2 1 1
2 1 3
1 2 2

I want to get lists out containing unique combinations of field1 and field2. So my final list would be something like [[row1,row5],[row3,row4],[row2]]. Is there an easy way to do this in SQLalchemy? Because right now I'm doing I'm using a set of tuples, but it's not ideal :/.

Another question: I've got a pretty compitated form. The form is a list of tables, and pretty much all cells can be edited by the user. The amount of tables is flexible and the amount of rows per table is also flexible (the amounts are inputted on a previous form). Only the columns are not flexible. I'm creating the form using Jinja2, names of elements in the form are created like `{{innerloopindex}}_{{outerloopindex}}_columnname`. I'm also using some hidden fields containing the amount of tables and the amount of rows per table. While retrieving the form again, I loop over these names and extract the values again. Is this the way it's normally done? What is a good way of doing this?

Last question: my app became pretty big (1600 lines) and it's still all in one file. I'm making my SQLalchemy classes (9), connecting to the database, and have ~40 app routes all in this file. I want to structure it more to make it less messy and more maintainable but don't really know where to start. For instance, I have one database table/class containing books. I have app routes to add, delete and edit book in the database. So in my app folder, I created another folder called books, and copied the routes connected to it to this new blueprint file in the books folder. I then import the blueprint in my main application. However, the app routes in the blueprint need acces to the database and the database classes. How do I connect those to the blueprint again?
This is my poorly made tree:

/books/books.py
/templates/display_books.html
/templates/add_books.html
/templates/edit_books.py
/application.py


I know they're pretty diverse questions, but I tend to find workarounds if I can't find an aswer quickly enough, so I saved a couple :) !

/r/flask
https://redd.it/7auqi1
Is this something which is possible in python?

I'm completely new to programming and am teaching myself python! (I did start learning html and found it much simpler but python seems better for what i'm needing to do).

I have read that you should have a goal in mind of what you would like to be able to achieve but first I wanted to know if it was possible.

**What i'd like to create**

I am a teacher and every year we have overnight excursions where we have to organize rooms for around 100 students however there are a number of requirements when organise these rooms:

* *Boys and girls MUST be in seperate rooms*
* *Room sizes and amount of rooms will vary. e.g. This year we might have 4x '5 bed' rooms and 7x '4 bed' rooms, however, we may have restrictions on this so all the boys rooms are together etc..*
* *Each student allocates 3 students they would like to share with and we promise to get them at least 1 of their friends.*

I understand this is a lot of variables, but I wanted to know if this is possible and something I can work towards.

Thanks!

/r/Python
https://redd.it/7awlm1
Creating a populated database with an excel sheet.

I converted my Excel sheet into a mysql script for table creation and a large list of insert commands. But how can I create that database and connect it to my Django app? Also is there a magic way to reverse and create models dynamically from the database headers? Thanks for any help I still have a lot to learn with databases.

/r/djangolearning
https://redd.it/7aqwf3
Simple Flask Question

Hi all. I'm new to both Pyhon and Flask. I'm building a simple Flask application where a user will select a store location from a drop-down menu on the page, and on the backend, it'll get the coordinates of that location, and display it on a google map.

When the user chooses a location from the form and clicks Submit, it makes a GET request which hits my route. in my route, I have the usual 'return render_template('store.html'), but I'm not sure where to include the logic to look up the coordinates.

I don't want to add this logic in the route, because it's around 60 lines of code and it would look really messy. I have a separate .py file with my code in it to perform the lookup. The script takes in a store name, then performs a lookup to get the latitute/longitude, and returns the latitude/longitude, which I want to inject into my store.html page via template.

I'm having a really tough time trying to figure out how to set up Flask to run another script when a route gets called, pass arguments into that script, and store whatever it returns in a variable. I've tried just importing my .py file like a regular module, but passing arguments doesn't seem to work. Maybe I'm calling it wrong? Is there a recommended way to do this?

/r/Python
https://redd.it/7b0nax
Need help designing database model

Hi, I'm trying to build a simple website that tracks Teachers and when they are available. I would like to query the database to check to see if they are available to cover a class but the only condition is that they cannot have 4 classes in a row (including the coverage).

Thing is, I am having trouble designing the model class to make it easier to check the given condition.

The way I'm thinking of making the model (in pseudo code)

class Teacher(models.Model):
#some information about the teacher

class Class(models.Model):
# I was thinking to have the Class model be a foreignkey to the Teacher model

class Weekday(models.Model):
# have the Weekday model be a foreignkey to the Class model

class Period(models.Model):
# I was thinking to make the Period model be a foreignkey to the Weekday model


I know it's really not much to go on but I'm having such a hard time thinking of how to design the database to make it easier to query in views.py so I apologize in advance.


EDIT: In hopes of being clearer, here is what I would have in Excel.
I would have a table with the teacher's name. Then for each row would have Period 1 through Period 8 and for each column, would be the Monday through Friday.


/r/django
https://redd.it/7b0l4d
Noob-friendly info on integrating pre-existing database with new django project? (and how to best design such a db)

As background, I'm a noob who has done the first-level tutorials. For instance, I've built a message-log system, where I define the models and the user determines *all* of the database content, and django does all the heavy lifting when it comes to the database operations.

My next project (the point of me doing all this in the first place) is different: I will be creating a database, and want to use django to display the data (read only).
Embarrassingly, I'm a little lost about how to go about this, I've always just started django projects from scratch with zero data in the db, and then added it via models/forms that the user interacts with.

Are there any simple examples/tutorials online anyone here can recommend for pulling a pre-made database, into a project. I have control over the database structure, which I will be rebuilding nightly (I'm pulling info, using an API, from a public database), so any advice/resources out there on how I should be structuring the database to mesh best with Django would also be really helpful. Up 'til now I let django handle all these details and I literally never thought about them.

I found a related topic here:
https://docs.djangoproject.com/en/1.11/howto/legacy-databases/



/r/django
https://redd.it/7azgy3
Flask-rest-JSONAPI help? Please?

Every request I make requires page[size]=0 or else I get a keyerror 'PAGE_SIZE' error.

Can I disable this requirement? It's killing me.

/u/akira-dev

/r/flask
https://redd.it/7aywv4
Removing duplicates from a multidimensional array?

Hi all,

I hope the terminoligy is correct, I apologise if not.

I have a list called `hitList` like this:

['ONE', 'A']
['TWO', 'B']
['TWO', 'B']
['THREE', 'C']
['THREE', 'C']
['THREE', 'C']
['ONE', 'A']
['ONE', 'A']
['ONE', 'A']
['ONE', 'A']
['ONE', 'A']
['FOUR', 'D']
['FOUR', 'D']
['FOUR', 'D']
['FOUR', 'D']
['FOUR', 'D']
['FOUR', 'D']
['FOUR', 'D']
['FOUR', 'D']
['FOUR', 'D']
['FOUR', 'D']
['FOUR', 'D']

I want to remove all the duplicates in `hitList` so it's as such:

['ONE', 'A']
['TWO', 'B']
['THREE', 'C']
['FOUR', 'D']

I've looked into various suggestions but they all seem to be for lists that are a single dimension (again I am sorry if I got that description wrong).

Does anyone have a link to a doc or a suggestion that could help me learn how to do this?

Thanks so much.

/r/Python
https://redd.it/7b3w4z
What kind of projects do you use Django for?

Hola!

In which of the following contexts do you use the Django web framework?

1. Bootstrapped business websites (e.g. eCommerce, SaaS, etc)

2. Personal pages (e.g. blog, portfolio, etc)

3. Developing 3rd party websites (freelancer / company)

4. Embedded development (e.g. deployed on a Rpi for IoT, etc)

5. Something else?

Appreciate your answers much!

/r/django
https://redd.it/7b36b8
How to learn a new API or Package?

We all know that python has a lot of libraries, packages, and frameworks available online. This is one of the main reason why python is such a powerful language.

But for a beginner, learning several packages can be overwhelming.

For an expert python programmer; lets say that you dont have any knowledge of django, how would you go about learning this framework?

What are your common strategies?

/r/Python
https://redd.it/7b4l7u
Non-parametric stats with Statsmodels?

Hey all -- I'm interested in doing a simple group means test with statsmodels, and I was wondering if anyone knows if the functionality is there or not.

Basically, I'm testing whether a subset (n=30) of a group (N=300) has a higher than expected mean. So, I want to build a distribution of means for random groups of size 30, then see where my test group's mean lands.

Is this the correct way to go about it, and is this built into statsmodels or another package?

(I have already been able to code this myself, just interested in knowing whether there is an "official" way out there.)

/r/pystats
https://redd.it/7b4u3r
Multiple user profiles

I’ve been scratching my head on this one all night/morning. What’s the best approach to use for multiple user profiles that have multiple fields that differ in each ?

/r/django
https://redd.it/7axh8z