|
Has anyone ever used Django smart selects? I followed their example exactly and I cannot get it to work for the life of me. I'm reviving a Django project I started earlier in the year - a character builder for a tabletop vampire game. A character can choose a bloodline based on their clan, so I'm trying to represent that with a chained selects thing. Theoretically, the list of available bloodlines should populate based on the character's clan, but it's doing no such thing. code:
|
# ? May 13, 2014 16:08 |
|
|
# ? Jun 5, 2024 21:38 |
|
I'd like to tell a sad story (with a happy ending). It began when I tried to use the default login view in my project. I suffered mightily over several sessions and resolved many issues, but in the end all was for naught since it just wouldn't process submit. So today I got depressed, ate a lot of unhealthy food, looked through all the debug tools available to me... and then something clicked - I had jQuery attached to submit events, that was located in the root template. But I realized that it only has to do with a different child template and nothing with the login, so I quickly moved it, and everything worked almost immediately. So now I intend to have javascript linked in the template it's used in. I'm a bit confused by the fact that I have to put load staticfiles every time, and also am wondering if this is the right way, because I haven't seen any recommendations re: arranging javascript files and it seems to be the usual practice to have everything in the same file. I think I'm remembering jQuery, in fact.
|
# ? May 16, 2014 16:24 |
|
supermikhail posted:So now I intend to have javascript linked in the template it's used in. I'm a bit confused by the fact that I have to put load staticfiles every time, and also am wondering if this is the right way, because I haven't seen any recommendations re: arranging javascript files and it seems to be the usual practice to have everything in the same file. I think I'm remembering jQuery, in fact. Do you really want every template load to include all of the template tags used to render all of the parent templates w/o your say?
|
# ? May 16, 2014 18:56 |
supermikhail posted:So now I intend to have javascript linked in the template it's used in. I'm a bit confused by the fact that I have to put load staticfiles every time, and also am wondering if this is the right way, because I haven't seen any recommendations re: arranging javascript files and it seems to be the usual practice to have everything in the same file. I think I'm remembering jQuery, in fact. It's not the most efficient way to go about it but I just use django-pipeline to combine & minify all javascript files into one single file, and that gets included in my base.html (which all other templates extend). While DEBUG=True they all get included as separate un-minified files. There's still a somewhat loose correlation between the names of some javascript files and the names of some templates, but it's not very strict and there's some overlap here and there.
|
|
# ? May 16, 2014 19:02 |
|
Looking for wisdom on moving from Sqlite to Postgres. I have a webapp that helps track things at work. It uses a Sqlite database, and is running on Openshift. Openshift has been a pain in the rear end. It works, but I have multiple issues, ie no static files, convoluted directory structure/update process, and wrong system time. These issues might be fixable, but no there's no documentation or support available, so it's a lost cause. I'm moving to Heroku, which doesn't support SQlite. I've got Postgres working on Heroku, and on my personal computer. I need to migrate my database, which has 1000s of records. With Sqlite, it was easy to just upload/download the file - I don't think I can do that with Postgres. Locally, I've been able to use Django's dumpdata and load data to export the data as json. I can't seem to do that on Heroku. If I use 'heroku run python manage.py loaddata', it can't find the file. (the file's local) How should I approach this? Dominoes fucked around with this message at 20:50 on May 16, 2014 |
# ? May 16, 2014 20:47 |
|
MonkeyMaker posted:Do you really want every template load to include all of the template tags used to render all of the parent templates w/o your say? Not that much, but it seemed to me like it was doing it anyway. I am corrected. fletcher posted:It's not the most efficient way to go about it but I just use django-pipeline to combine & minify all javascript files into one single file, and that gets included in my base.html (which all other templates extend). While DEBUG=True they all get included as separate un-minified files. There's still a somewhat loose correlation between the names of some javascript files and the names of some templates, but it's not very strict and there's some overlap here and there. I don't see what the point of this is. Is it a performance boost if upon deployment everything is in a single file?
|
# ? May 16, 2014 21:16 |
|
Dominoes posted:Locally, I've been able to use Django's dumpdata and load data to export the data as json. I can't seem to do that on Heroku. If I use 'heroku run python manage.py loaddata', it can't find the file. (the file's local) How should I approach this? The file's local to what? If it's not in the repo that's getting pushed to Heroku, you need to get it there some other way or directly connect to Heroku Postgres from your local dev machine. The latter would be my preference as there are fewer moving parts. heroku config will give you your Postgres URL. Temporarily sub that in for your development database and let 'er rip. Heroku instances have curl installed if you just want to suck the file down over HTTP. You can run it directly (heroku run curl ...) or by opening a bash session (heroku run bash). Molten Llama fucked around with this message at 21:28 on May 16, 2014 |
# ? May 16, 2014 21:24 |
|
Thanks. I went with your second option, and it worked! Connecting directly seems like a pretty powerful and useful feature.
|
# ? May 16, 2014 22:24 |
supermikhail posted:I don't see what the point of this is. Is it a performance boost if upon deployment everything is in a single file? Minimizing the number of HTTP requests is definitely something you can do to improve performance. There are tradeoffs though, since you don't want to load a ton of code that you aren't going to use. The site I do it on doesn't have a ton of JavaScript so it's not a huge deal. It keeps things simple though, I don't have to think about what JavaScript is loaded for which page, it's just all there all the time on every page.
|
|
# ? May 16, 2014 22:39 |
Is there a standard convention for where you declare all your signal handlers? edit: found this on the signals page https://docs.djangoproject.com/en/dev/topics/signals/ quote:Where should this code live? I'm using 1.5. In the end of my models.py do I just do an import myapp.signals or something? fletcher fucked around with this message at 23:29 on May 16, 2014 |
|
# ? May 16, 2014 22:45 |
|
fletcher posted:Is there a standard convention for where you declare all your signal handlers? I just went through this problem as well. The bottom of models.py is the most common place to keep it, but it's kinda gross:
One alternative I've been trying out is doing explicit calls to register the handlers in the app's __init__.py. It feels a little less gross, but it's still open to some circular import problems if you're not careful.
|
# ? May 17, 2014 04:36 |
Awesome! Thanks for the tips.
|
|
# ? May 17, 2014 09:26 |
|
I'm importing a bunch of XML objects from an API into a database. I don't run the API and I get 1000 entries or nothing. The objects have an ID field that's the same as in the database. There can be no duplicates. The table at the moment has 100,000 rows and the model is very complex. So I have two data sets, items is an ElementTree representing the XML document and then I have the model. [i for i in items if i.get("id") not in Model.objects.all().values_list('id', flat=True)] seems to be extremely slow but produces what I'm looking for, a list of objects I can feed to an import function. Aside from abusing INSERT IGNORE or some other abortion, what would be a better way to combine the two to produce one list of objects that do not have an ID already in the database?
|
# ? May 18, 2014 22:51 |
|
Written that way, it will re-run the query for each element.code:
code:
|
# ? May 19, 2014 00:16 |
|
I knew it was something simple like that. I had the 1000 small queries solution before and I was trying to implement the one big query solution. I'll test the speeds of each and see which one works better.
|
# ? May 19, 2014 01:02 |
|
Luigi Thirty posted:I knew it was something simple like that. I had the 1000 small queries solution before and I was trying to implement the one big query solution. I'll test the speeds of each and see which one works better. I bet you'd get better performance on an inverse query if you used a Q object like code:
|
# ? May 19, 2014 01:21 |
|
I've got the following code that produces some strange html:code:
|
# ? May 20, 2014 16:22 |
|
Luigi Thirty posted:I'm importing a bunch of XML objects from an API into a database code:
You may run into issues with the above if you're using SQLite, because it only supports up to 999 parameters or something IIRC.
|
# ? May 20, 2014 18:59 |
|
What's the preferred method of querying a whole mess of related data from the ORM and displaying it in a template. I am talking about something like an order with about 8 other related tables, traversing multiple joins up to maybe 5 levels deep from a single parent table. I can query the data easily enough as the query params are simple, in this case all client orders. But I am wondering how far to chain things together in the view vs. passing a master obj to the template and then referencing relationships via child _set memberships. For instance I can get everything I need in the view as a number of query set objects and pass them back individually. Or I could try to pass them back to the template in as few objects as possible and then have the ORM hit the related tables as I iterate through the query sets for additional data. Ideas? If this was a standard SQL recordset, I would make a single big query and pass back a large recordset with all the data I needed in a single obj so that I only hit the DB once.
|
# ? May 23, 2014 23:29 |
Ahz posted:If this was a standard SQL recordset, I would make a single big query and pass back a large recordset with all the data I needed in a single obj so that I only hit the DB once. You can do that in Django as well through the use of select_related and prefetch_related. Just use those with your original query for the order and then you don't have to worry about your loops in templates generating additional queries.
|
|
# ? May 23, 2014 23:34 |
|
fletcher posted:You can do that in Django as well through the use of select_related and prefetch_related. Just use those with your original query for the order and then you don't have to worry about your loops in templates generating additional queries. Yeah I've got a good handle on how select_related automatically traces up the line of parents. Prefetch_related is a little trickier for me to understand. If I want to start at the parent and then progress 3 levels down to a child, then back up another way up the 'tree', I could do this with some inner and outer join action. It's a little harder to conceptually understand how the ORM handles deep traversal, especially across more than 1 level of parent->child relationships that select_related cannot do.
|
# ? May 24, 2014 00:15 |
|
I've been troubleshooting getting forms to display cleanly in Django. The way I learned to display forms in the template from the Polls tutorial looks like this:code:
Unable to get it formatted cleanly, I found online that using '{{ form.as_p }}' will show anything with 'label' in the form class as a label tag, which you can then manipulate with CSS. The method I used with help_text showed un-tagged text above the <input> tags in source view. The official docs on forms mention both methods. Thoughts? Also looking for advice on getting the labels/help_text to align left, and the fields to align right. I've tried this: code:
Dominoes fucked around with this message at 10:20 on May 26, 2014 |
# ? May 26, 2014 09:58 |
|
Dominoes posted:I've been troubleshooting getting forms to display cleanly in Django. The way I learned to display forms in the template from the Polls tutorial looks like this: Python code:
|
# ? May 26, 2014 19:06 |
|
Lumpy posted:
Here's an MS-paint mockup of what I'd like:
|
# ? May 26, 2014 19:40 |
|
Sorry for hijacking, but is this applicable to your problem: http://stackoverflow.com/questions/2820586/how-can-i-control-the-width-of-a-label-tag?
|
# ? May 26, 2014 19:54 |
|
supermikhail posted:Sorry for hijacking, but is this applicable to your problem: http://stackoverflow.com/questions/2820586/how-can-i-control-the-width-of-a-label-tag? CSS code:
Django code:
Dominoes fucked around with this message at 20:09 on May 26, 2014 |
# ? May 26, 2014 20:04 |
|
supermikhail posted:Sorry for hijacking, but is this applicable to your problem: http://stackoverflow.com/questions/2820586/how-can-i-control-the-width-of-a-label-tag? Don't be sorry, it's one less thing I need to think about!
|
# ? May 26, 2014 23:46 |
|
Dominoes posted:Dude! That worked! Hey! https://docs.djangoproject.com/en/1.6/ref/forms/fields/#help-text Also, I'm pretty sure {{ field.label }} comes with <label> tags... Oh. No. But you should use field.label_tag instead https://docs.djangoproject.com/en/1.6/topics/forms/#looping-over-the-form-s-fields because it a) comes with the label tags, b) points to the corresponding field, otherwise it's pretty pointless.
|
# ? May 27, 2014 05:02 |
|
Thanks. Any words on how to make a login page redirect to page that triggered it? This Stack Overflow post addresses it, but the solution only works if logging in from a link; I'm using the @login_required method, with no link to reference. I've tried this: Python code:
I feel like this is a very common problem - most major websites I use that have logins don't address this - and it's really annoying! Ie you navigate a few pages deep in a website, are met with a login wall, then are spit out at the top after logging in. Dominoes fucked around with this message at 08:20 on May 28, 2014 |
# ? May 28, 2014 08:03 |
|
One (rather complicated) way I can think of is have the login button be part of a form with a hidden field for the current url (found like this http://stackoverflow.com/questions/2882490/get-the-current-url-within-a-django-template). This goes to your GET login view which passes the hidden field on to the login page, from which you POST it back to the login view, and voila. But maybe I'm overthinking it. Edit: I'm sure you can have log in with a link and pass the current url as an argument, too. supermikhail fucked around with this message at 08:39 on May 28, 2014 |
# ? May 28, 2014 08:33 |
|
Dominoes posted:Thanks. Any words on how to make a login page redirect to page that triggered it? This Stack Overflow post addresses it, but the solution only works if logging in from a link; I'm using the @login_required method, with no link to reference.
|
# ? May 28, 2014 09:15 |
|
I've discovered the following contrivance, which is useful for my situation, on stackoverflow:code:
|
# ? May 28, 2014 09:35 |
|
Dominoes posted:Thanks. Any words on how to make a login page redirect to page that triggered it? This Stack Overflow post addresses it, but the solution only works if logging in from a link; I'm using the @login_required method, with no link to reference. FWIW, I use django-allauth on all my projects requiring registration and login stuff, and when you get redirect to a login page it appends the url to redirect to on login to the url. So, like if you visit www.example.com/dashboard/ it will redirect you to www.example.com/login/?next=/dashboard/ and then, on login redirects back to www.example.com/dashboard/. It's been awhile since I configured any of this on any project, so I can't recall if I had to do anything specific to set that up or not...
|
# ? May 28, 2014 19:05 |
|
@Dominoes Here is what project of mine looks like: In login.html: HTML code:
Python code:
|
# ? May 28, 2014 19:37 |
Should I be avoiding the use of auto_now and auto_now_add?
|
|
# ? May 29, 2014 00:41 |
|
fletcher posted:Should I be avoiding the use of auto_now and auto_now_add? nope. I don't see any warnings or deprecations, do you?
|
# ? May 29, 2014 00:58 |
MonkeyMaker posted:nope. I don't see any warnings or deprecations, do you? Yeah I figured it was probably ok. I was googling around and saw some people had pretty strong opinions about how they were evil and should be avoided, but the posts were quite a few years old. Aaaaand I just realized that I've had auto_now and auto_now_add backwards in all my field definitions this whole time
|
|
# ? May 29, 2014 01:07 |
|
fletcher posted:Yeah I figured it was probably ok. I was googling around and saw some people had pretty strong opinions about how they were evil and should be avoided, but the posts were quite a few years old. They were deprecated at one point but then...they weren't. I think part of the problem is people started doing code:
(BTW, if you don't see what's wrong with that line, read up on when Python initializes values)
|
# ? May 29, 2014 02:45 |
|
supermikhail posted:I've discovered the following contrivance, which is useful for my situation, on stackoverflow:
|
# ? May 29, 2014 09:32 |
|
|
# ? Jun 5, 2024 21:38 |
|
Do you guys have any suggestions for implementing the "user activity or logout" functionality that a lot of banks have? I found a library called session_security but for some weird reason even though it hits the logout(request) line in the library middleware, the user isn't actively logged out and redirected.
|
# ? May 29, 2014 17:49 |