|
If I pass an array of category names to a view how would I get all the posts in those categories? I don't really know how to deal with arrays.
|
# ? Jun 19, 2012 16:19 |
|
|
# ? Jun 6, 2024 03:00 |
|
fuf posted:If I pass an array of category names to a view how would I get all the posts in those categories? I don't really know how to deal with arrays. code:
|
# ? Jun 19, 2012 18:01 |
|
MonkeyMaker posted:
Thanks man. I'll admit I still don't understand kwargs but I think the "pk__in" bit was crucial. I think it turns out that I was passing to the view wasn't actually an array so I ended up using "split" and now it works. I have a crude, ham-fisted way of loading posts from categories without reloading the page using jquery, but I don't really know what I'm doing so it might be totally retarded. When someone clicks a category I make an array of all the 'active' categories (whose posts will be displayed): code:
code:
Then the view looks like this: code:
|
# ? Jun 19, 2012 19:45 |
|
I don't think it's a bad approach, but I have some suggestions. I would be more explicit about the comma separated categories in your JavaScript, and use jQuery's map function rather than building the array manually. code:
Your use of dict(posts=posts) is unusual, although it works perfectly fine. Do you know that you can do this? code:
|
# ? Jun 20, 2012 09:44 |
|
Does anyone have recommendations for generating pre-filled PDF forms out of templates? Apparently it's fairly easy to do if using HTML/CSS templates and then rendering them to PDF but I'd prefer something that does not require knowing HTML/CSS for creating and modifying the form template.
|
# ? Jun 20, 2012 13:32 |
|
On that note, does anyone have a recommendation for a python library that turns a HTML/CSS page into a PDF? I need to set up a weekly digest that gathers some stuff together for the users of my web application and attaches them as PDFs, was hoping there was an option that is as simple as template with css -> pdf so I don't have to spend much time learning how to make it go.
|
# ? Jun 20, 2012 13:52 |
|
raymond posted:I don't think it's a bad approach, but I have some suggestions. Oh cool, that's a good way to do it. My only worry (with your solution and mine) is having integers as the IDs of so many divs. Apparently this is bad CSS practice. I wonder if it could cause problems? quote:Your use of dict(posts=posts) is unusual, although it works perfectly fine. I don't know where I got that from. I think it must have been used in a tutorial and I just stuck with it. Your way is easier. Thanks for the tips.
|
# ? Jun 20, 2012 13:53 |
|
http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html Just make sure you aren't opening with digits and you'll be fine, you must start a HTML ID with a letter of the alphabet, upper or lowercase.
|
# ? Jun 20, 2012 14:04 |
|
Maluco Marinero posted:On that note, does anyone have a recommendation for a python library that turns a HTML/CSS page into a PDF? I need to set up a weekly digest that gathers some stuff together for the users of my web application and attaches them as PDFs, was hoping there was an option that is as simple as template with css -> pdf so I don't have to spend much time learning how to make it go. We used wkhtmltopdf pretty successfully. It uses a headless WebKit to render an HTML page (with whatever associated images and CSS) and then converts it to a PDF.
|
# ? Jun 20, 2012 17:08 |
|
raymond posted:Your use of dict(posts=posts) is unusual, although it works perfectly fine. Do you know that you can do this? I kind of like the dict constructor for string keys. I think it's a bit neater: code:
|
# ? Jun 20, 2012 17:28 |
|
I hope no one minds another tricky question. I have a model called Books and a model called Eras. Each book has a year and each era has a start date and an end date (So for instance the Enlightenment era starts in 1650 and ends in 1800). I want to distribute the books into the eras to which they belong then pass it all to a template. Ideally I want the template to work like this: code:
All I have so far is a way of getting all the books for a certain era: code:
|
# ? Jun 20, 2012 18:07 |
|
Why not also have a ForeignKey from a book to its respective Era?
|
# ? Jun 20, 2012 18:46 |
|
Well because I was hoping the eras could be dynamic, so I could adjust the dates if I need to, or add more specific eras and so on. Also I guess I thought that since each book already has a year, it shouldn't need an era as well. Perhaps I am making things too complicated though. I'll use a foreign key if it's the only way.
|
# ? Jun 20, 2012 20:06 |
|
It's certainly not the only way, but it might be a bit easier right now. In raw SQL, you could do something like:code:
|
# ? Jun 20, 2012 21:04 |
|
MonkeyMaker posted:We used wkhtmltopdf pretty successfully. It uses a headless WebKit to render an HTML page (with whatever associated images and CSS) and then converts it to a PDF.
|
# ? Jun 20, 2012 23:45 |
|
fuf posted:My only worry (with your solution and mine) is having integers as the IDs of so many divs. Suspicious Dish posted:I kind of like the dict constructor for string keys. I think it's a bit neater:
|
# ? Jun 21, 2012 00:43 |
|
fuf posted:I have a model called Books and a model called Eras. Each book has a year and each era has a start date and an end date (So for instance the Enlightenment era starts in 1650 and ends in 1800). code:
|
# ? Jun 21, 2012 00:55 |
|
deimos posted:It doesn't seem like a DB error, seems like the naive implementation of the development http server having timing issue. If your requests take long enough it'll throw that error.
|
# ? Jun 21, 2012 14:40 |
|
Meet IF-ZILLA. I'll revisit this when I'm not braindamaged from a rotten hangover. Its possibly the most insane piece of code I've been asked to write. e: Its a JSON endpoint for an iphone app that can login via user/pass or facebook, creating the account ex nihilo if it doesnt already exist. e2: And if you dont know what sort of diagram that is, I'm going to bash you with my zimmer frame.
|
# ? Jun 25, 2012 04:46 |
|
duck monster posted:e2: And if you dont know what sort of diagram that is, I'm going to bash you with my zimmer frame. Trying to figure out diagram by searching for zimmer frame didn't work out too well. Now I know what a zimmer frame is though, so I'm 50% of the way to enlightenment.
|
# ? Jun 25, 2012 11:31 |
|
Mulozon Empuri posted:Trying to figure out diagram by searching for zimmer frame didn't work out too well. Now I know what a zimmer frame is though, so I'm 50% of the way to enlightenment. Its a Nassi Schneiderman diagram, aka "structogram", and their loving useful if your having trouble holding a particularly messy structured algorithm in your head at once. Its basically a flowchart where its impossible to do a goto. But I have no idea if they are still taught , now that things have gone OO. (Although I'm not sure why it wouldn't be suitable for OO being that OO really is just structured but fancy, anyway.
|
# ? Jun 25, 2012 12:43 |
|
They're not, but I wish they were. Most often it seems to be Data-flow diagrams or copious abuse of MSDs.
|
# ? Jun 25, 2012 16:51 |
|
duck monster posted:Meet IF-ZILLA. As a recent college graduate (1 year out) I can safely say we were never shown a diagram like that. Plenty of other useless flowcharts, but not that one.
|
# ? Jun 26, 2012 15:54 |
|
duck monster posted:and their loving useful if your having trouble holding a "they're" and "you're" I keep seeing you do this and I can't let it go on any longer.
|
# ? Jun 28, 2012 02:09 |
|
Honestly, is this chart more useful than say... anything? duck monster posted:Its a JSON endpoint for an iphone app that can login via user/pass or facebook If you haven't already you may be interested in taking a look at: Djang-facebook and Django-social-auth Even if you can't use them directly in your project they do things like: duck monster posted:creating the account ex nihilo if it doesnt already exist. and you can see how they solve issues that arise from the idiosyncrasies in the Facebook API.
|
# ? Jun 28, 2012 07:41 |
|
Surface posted:Honestly, is this chart more useful than say... anything? I've never seen it before, but I think it looks pretty drat useful for describing flows in functions.
|
# ? Jun 28, 2012 12:04 |
|
Okay so I'm a Django beginner, and I'm having a lot of trouble with a ranking / filter algorithm I'm trying to write. Essentially we're filtering songs/albums/artists first by a bunch of metrics (location, genre, price). These are simple boolean filters. Easy. next we filter the Fan object of all these objects (new fan object for every user who fans the artist/album/song). We filter the fans based on time (fans added this week, etc.) the problem is then ranking each category by #fans, but only fans in our filtered queryset. Right now I'm doing all the ranking in pure python, which is not ideal. I'd like to be able to have the function return 3 ranked query sets. any guidance would be appreciated
|
# ? Jun 28, 2012 22:12 |
|
Provide the models that represent this and it'll be easier to help out. By the way Query sets can follow relationships like code:
|
# ? Jun 28, 2012 23:27 |
|
essentially the profile/artist models are set up like this:code:
so I can sort profiles by # fans by using code:
|
# ? Jun 29, 2012 00:32 |
|
I don't think annotate can take a second queryset, altthough I'd welcome being proved wrong. You can do it with python using sorted definitely, but I'm assuming you're already doing that.Python code:
Maluco Marinero fucked around with this message at 01:03 on Jun 29, 2012 |
# ? Jun 29, 2012 01:00 |
|
Yeah that's something like what I have. I was worried that in production with a large database it'd suffer. I actually didn't use sorted so your suggestion helped.
|
# ? Jun 29, 2012 01:12 |
|
You probably need to look into some form of caching if you're worrried about your heavy stuff. You could even give your profiles an IntegerField which stores the count, which you'd update each save() call or something. Then you can order directly in the first queryset using that stored value. A bit rough but caching really depends on your use case doesn't it.
|
# ? Jun 29, 2012 01:23 |
|
Okay thanks for the heads up.
|
# ? Jun 29, 2012 02:54 |
|
Surface posted:Honestly, is this chart more useful than say... anything? The Nassi-Schneiderman diagram was a technique popular in the 80s and early 90s but seems to have gone out of fashion because it doesnt handle some of the more hairier features in functional languages (I *think*) and OO, back when structured was still king, to design and diagram program flow in a way that forbade spaghetti code of the goto variety and could let you fairly easily map out complex control flows. Its a variant of the flow chart, but its basically *better* because if you are *designing* this way, your guaranteed your resulting chart will work in a structured language. It was mostly used for teaching, and once upon a time books on algorithms would always have one of these diagrams attached to the pseudo or pascal code. It's still handy tool when you have some loving disgusting looking nest of an algorithm you want to design or at least figure out what the gently caress its doing.
|
# ? Jul 3, 2012 16:36 |
|
I have a model with a bound method that returns the name of a related model_c or a blank stringcode:
code:
|
# ? Jul 4, 2012 11:55 |
|
NtotheTC posted:
That's because you're returning the method, not calling it
|
# ? Jul 4, 2012 13:11 |
|
You aren't actually calling the function there, you're mixing your syntaxes. Django's template system will always automatically call a function with no variables, but obviously in Python this will result in returning the function. Add () to your function in modelB Python code:
|
# ? Jul 4, 2012 13:11 |
|
Maluco Marinero posted:Of course this all seems a bit of a waste of effort in this example when {{ instance_of_b.model_a.model_c.name }} is almost the same length, and Django's templating system automatically ignores failed variables if they don't exist, so it'd return '' in a non find anyway. Ah yeah, I've been using model properties a lot recently so my stupid brain didn't pick up on the fact that it was actually a method. Duh. In response to your last point though, the reason I'm using this method is because the default response I was getting if a variable didn't exist was "None" rather than ''. And if I use {{ instance_of_b.model_a.model_c.name }} and it turns out model_b's related field to model_a is null, or model_a to model_c's is null, then it would fail with a NoneType error would it not? I may need to look into this a bit more.
|
# ? Jul 4, 2012 13:54 |
|
Nope, just a silent fail. This is good as long as you know what is happening, because it make's it fairly straightforward to check for existence.Django Documentation posted:If the variable raises an exception when called, the exception will be propagated, unless the exception has an attribute silent_variable_failure whose value is True. If the exception does have a silent_variable_failure attribute whose value is True, the variable will render as an empty string. As the foreign key is going to return an object or raise DoesNotExist, this will fail to nothing. If you want a default to something other than an empty string, you can use the template filter |default or |default_if_none (if suitable for what you're calling returns). Reverse rels/many to many managers are also easy, as in order to call them you use {{ instance.model_a_set.all }}, which returns either an empty queryset which is false in if statements, or a queryset that you can run over like a list. Maluco Marinero fucked around with this message at 14:17 on Jul 4, 2012 |
# ? Jul 4, 2012 14:15 |
|
|
# ? Jun 6, 2024 03:00 |
|
Has anyone suggested a violent purge of ex java programmers from the django development team yet? I swear this framework is getting more complicated and more "enterprisey" as time goes on. Like just a giant review where 60% of stupid poo poo , with close attention payed to which fucker put it there, is stripped out so that django is simple minded , quick to learn and fast to think in without having to constantly be reverting to manuals, again. Gah.... Anyway, does anyone know where the django documentation , without all the confusing poo poo about custom file handlers for big and small files and blah blah blah defining forms blah blah etc is? I'm trying to transfer a file in from an iphone app and I *dont* want to use a form and I'm hosed if I can find the documentation. It *USED* to be there, or maybe its 4am and my brains fritzed it. Been very tempted over a few projects lately where django is just too much loving guff to jump ship to flask... duck monster fucked around with this message at 21:34 on Jul 4, 2012 |
# ? Jul 4, 2012 21:27 |