|
nbv4 posted:Actually you're right. I was confused. The problem I was having was if the user isn't logged in, get_profile returns nothing. In that case, I'm pretty sure the only solution is to use a context processor, in order to make a profile object regardless if the user is logged in or not. Yes, that's a perfect use of a context processor because that's something you'd have to repeat in all your templates or views and which can't be easily done in the templates.
|
# ? Oct 16, 2008 16:24 |
|
|
# ? May 16, 2024 04:10 |
|
I'm still kind of a nublet at python/django, but I wrote up a little post on how to easily add reCAPTCHA support to django 1.0, since pretty much all of the examples I found online were for the old version of the comments framework. Hopefully someone will find it useful. If I'm doing/explaining something wrong or inefficient, please let me know
|
# ? Oct 17, 2008 17:06 |
|
I know this is a dumb question and there's probably a simple answer to it. How can I update a field when a view if accessed? Like the view is hit and it adds +1 to a value. I figured out how to do this easily with PHP/CodeIgniter, but Django has me stumped again.
|
# ? Oct 17, 2008 21:50 |
|
All the data models in my site are very heavily user centric. All data has a "owner", and only the owner ever looks at and edits that data. For instance a user will create a character or something, but that one user is the only person who will ever see that character. How can I define this concept in the data model? When the user adds a new character, he will choose from a dropdown box various items for that character to have (such as a hat or a cane). I only want the item the user "owns" to be in that dropdown box. This is how just about 100% of my data models are going to end up working. I basically want code:
|
# ? Oct 18, 2008 07:53 |
|
nbv4 posted:I basically want Use a custom manager method. You'd create something like a PerUserManager, add a .for_user() method, and call it like this: code:
|
# ? Oct 18, 2008 08:32 |
|
hitze posted:I know this is a dumb question and there's probably a simple answer to it. How can I update a field when a view if accessed? Like the view is hit and it adds +1 to a value. I figured out how to do this easily with PHP/CodeIgniter, but Django has me stumped again. What do you mean by "a field"? A field in the database? I'm assuming you have some sort of, say, page-hit-count table with 2 columns (view name, integer count), in which case you'd do something like this: code:
|
# ? Oct 18, 2008 16:25 |
|
I'm trying to port over a webapp I wrote in PHP to Django. In my PHP site, I stored the user's password as a sha246 hash from the userid, the user's password, and then a random string of letters, all concatenated together. So if your userid number was 25 and your password was "poopypants", it would store the sha256 hash of "25popypantsfdshjfdshjsdf". Is there anyway to have django authenticate users this way? I'd really rather not have my entire site create a new password when I switch over...
|
# ? Oct 27, 2008 02:50 |
|
nbv4 posted:I'm trying to port over a webapp I wrote in PHP to Django. In my PHP site, I stored the user's password as a sha246 hash from the userid, the user's password, and then a random string of letters, all concatenated together. So if your userid number was 25 and your password was "poopypants", it would store the sha256 hash of "25popypantsfdshjfdshjsdf". Write your own authentication backend. In it, define an authenticate() method that does whatever custom authentication (reading from your database, hashing the given password, comparing them...) and returns a Django User (if login successful) and None if the login didn't work, and also a get_user() method. http://www.djangoproject.com/documentation/authentication/#other-authentication-sources
|
# ? Oct 27, 2008 16:05 |
|
I'm using django to keep a simple order database (in addition to many other things). I've got a model called 'hoody' which contains fields 'paid_for' and 'collected', and can be either Y or N. These show up in the admin list, but I'd like a way of editing these easily directly from the main list. Is this possible?
|
# ? Oct 29, 2008 14:28 |
|
evilmonkeh posted:I'm using django to keep a simple order database (in addition to many other things). In the admin? No, not without altering the admin's templates, which is semi-easy for some cases, but I think that's mostly the forms and not the listing pages. Not sure. Depending on what you're doing with the site, and if you need to do not-entirely-common things like what you describe, you'll probably want to ignore the admin and just write things from scratch (which is a lot easier than it sounds, with generic views).
|
# ? Oct 29, 2008 18:47 |
|
I'm writing a survey application. For items that have multiple checkbox entries (i.e. pick as many choices as you want), I was thinking of using a CommaSeparatedIntegerField in my model, and a MultipleChoiceCheckbox in the form. Is the the "right" way to do this?
|
# ? Oct 29, 2008 20:11 |
|
Larry Horse posted:I'm writing a survey application. For items that have multiple checkbox entries (i.e. pick as many choices as you want), I was thinking of using a CommaSeparatedIntegerField in my model, and a MultipleChoiceCheckbox in the form. Is the the "right" way to do this? Guess it depends on what exactly you need to do with those values on the query side of things, but it could be an OK choice, nothing obviously wrong with it that I can see. Also, hooray! the book is officially out. Got my hard copies last night. Here's a crappy iPhone photo:
|
# ? Oct 30, 2008 21:31 |
|
Any samples from the book available? How does it compare to Practical Django Projects?
|
# ? Oct 30, 2008 22:19 |
|
bitprophet posted:Guess it depends on what exactly you need to do with those values on the query side of things, but it could be an OK choice, nothing obviously wrong with it that I can see. Congrats! Most likely I'll just be exporting stuff to XLS sheets for people in my organization to play with for now, so I don't think the comma separated integers will be an issue. Thanks!
|
# ? Oct 30, 2008 22:22 |
|
bitprophet posted:Guess it depends on what exactly you need to do with those values on the query side of things, but it could be an OK choice, nothing obviously wrong with it that I can see. Congrats! How is pbx doing? I remember hearing he was in a bad car accident over the summer.
|
# ? Oct 30, 2008 23:19 |
|
Congrats man! When I get some cash, I'll pick this up!
|
# ? Oct 31, 2008 00:11 |
|
Thanks guys Paul is doing a lot better, king_kilr, as far as I can tell he's fully physically recovered and I think he's mostly up to speed in terms of his work and personal life. He wasn't able to really get back into the book until after we'd sent off the last edits, but he's curating the website. Which isn't much of a website right now, but we're putting up an aborted appendix (it's a list of links; we felt it would work better as part of the website), and will try to package up our sample applications for download, soonish.
|
# ? Oct 31, 2008 02:07 |
|
Just looked through the index for your book, and I have to say the examples sound really cool, I can't wait to check it out.
|
# ? Oct 31, 2008 03:10 |
|
bitprophet posted:Guess it depends on what exactly you need to do with those values on the query side of things, but it could be an OK choice, nothing obviously wrong with it that I can see. Hooray! Tell me it's updated for 1.0 Also, slightly off topic - I've started fleshing out an outline for a book I'm pondering, except I've never written something that expansive - I'd be interested in hearing how you approached it and how it went for you. m0nk3yz fucked around with this message at 14:09 on Oct 31, 2008 |
# ? Oct 31, 2008 14:07 |
|
m0nk3yz posted:Hooray! Tell me it's updated for 1.0 Take a closer look at the photo specifically the upper right quadrant. Think circles. quote:Also, slightly off topic - I've started fleshing out an outline for a book I'm pondering, except I've never written something that expansive - I'd be interested in hearing how you approached it and how it went for you. It's a significant effort, especially if you're going to do it solo (while I ended up doing more than half the work for our book, it was still very much a team effort, and I believe that made a big difference). We're talking no social life and no free time (well, no non-guilty free time) for a year or two or even three, depending on the process and the timeliness of the material. At least if you're balancing the book with a full-time job. I was also fortunate in that I didn't have to sell the book to a publisher -- Wes has written books before (Core Python Programming) and so he had a strong relationship with Pearson already. So we didn't just have a foot in the door, we were already sleeping on the couch, so to speak. And even so, we still spent a bit of money on having a legally inclined individual look over the contract with us. If you don't have a contact at an interested publisher, I hear that getting one can be a big chore in and of itself. I could probably hook you up with Wes, I'm sure he has stories about CPP. In terms of writing the book itself, my main regret is not spending a constant amount of time on it; it was more like slacking off earlier on and then really getting into it towards the end. I wish I'd evened it out more. This comes back to the idea of time -- even if you could jot down your ideas in N hours, like all other creative works you really need to re-examine it multiple times, and then there's a nontrivial time allotment for the back-and-forth with publisher staff during the copyediting and layout phases. Uh...that's a bit of a ramble, sorry! let me know if you have specific questions, that might help. We might also want to take this off-thread, it's kind of off-topic
|
# ? Oct 31, 2008 20:25 |
|
Janin posted:Use a custom manager method. You'd create something like a PerUserManager, add a .for_user() method, and call it like this: What about a whole new manager? Like "characters.mine.all()", which list all of my characters? That seems like the most logical way of doing it. The only problem is I can't quite figure out how to tell the manager who "I" am... bitprophet posted:
hmm, I'm in the market for a django book. Does your book spend a lot of time explaining beginner concepts, or does it focus more on how to get things done in django?
|
# ? Nov 2, 2008 09:07 |
|
You can override the manager's __init__ method and give it an extra argument and then save that argument as an instance variable and then call the superclass __init__. That way you can do something like characters.mine(request.user).all()
|
# ? Nov 2, 2008 13:36 |
|
nbv4 posted:hmm, I'm in the market for a django book. Does your book spend a lot of time explaining beginner concepts, or does it focus more on how to get things done in django? We definitely go over the basics; there's even a smallish chapter towards the beginning that just makes sure the reader is up to speed on general Web development concepts, without mentioning anything specifically technical. I wrote that particular chapter because I found a lot of newbies, even if they knew "Web development" coming from PHP or ASP (or any Web language which is generally implemented via simple scripts), had never actually been shown the Big Picture, and so had some initial conceptual problems when using a framework that basically models HTTP. The first half of the book basically goes Python primer (a decently sized one ) => whirlwind tour chapter => that small concepts chapter I mentioned => 3 pretty big chapters going over all the main components of Django. So by the time you hit the "how to get things done" sections (example apps + advanced materials) you've got a good foundation to build on.
|
# ? Nov 2, 2008 14:54 |
|
No pdf/e-book version, eh? :\
|
# ? Nov 2, 2008 15:20 |
|
bitprophet posted:Take a closer look at the photo specifically the upper right quadrant. Think circles. I am a failure.
|
# ? Nov 2, 2008 16:54 |
|
I've started setting up a site with Django a few days ago and I'm stuck with a completely newbie problem. I'm trying to set up a blogish type thing and in the table for the entries I want it to store the author. Here's what I figured it would be like:code:
|
# ? Nov 2, 2008 17:41 |
|
bmoyles posted:No pdf/e-book version, eh? :\ Not through Amazon AFAIK, but it's available on InformIT's Safari service here. Honestly I'm not sure if you can get it as a single PDF or what -- I've only used Safari via subscription -- but poke around and see what you can get
|
# ? Nov 2, 2008 19:24 |
|
Congrats. I had started on a book on Boa-Constructor a while back, but it started becoming clear that the system had no future because the guy who maintains/coded it works at a ponderously glacial speed and is alergic to collaborating with people who want to help him. Then I started on a book on Python Webware, but gave that up too because it was getting clear it was being majorly eclipsed by Django I suck at future prediction. I'm sure this english lit degree will become handy ONE day.
|
# ? Nov 2, 2008 21:52 |
|
bitprophet posted:Not through Amazon AFAIK, but it's available on InformIT's Safari service here. Honestly I'm not sure if you can get it as a single PDF or what -- I've only used Safari via subscription -- but poke around and see what you can get Oh sweet, I'm a safari subscriber through O'Reilly. Perfect. I'm trying to keep up my record of not having bought a physical computer book in 3 years
|
# ? Nov 2, 2008 22:54 |
|
raezr posted:I've started setting up a site with Django a few days ago and I'm stuck with a completely newbie problem. I'm trying to set up a blogish type thing and in the table for the entries I want it to store the author. Here's what I figured it would be like: It is simple. In your view where the entry's form is submitted, save the object but don't commit it. Then add in the user. Then save it for real. Example: code:
|
# ? Nov 3, 2008 01:20 |
|
pokeyman posted:It is simple. In your view where the entry's form is submitted, save the object but don't commit it. Then add in the user. Then save it for real. EDIT: durr I found an example showing exactly what I want to do on their documentation http://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-methods I was just looking in all the wrong places tankadillo fucked around with this message at 05:45 on Nov 3, 2008 |
# ? Nov 3, 2008 05:34 |
|
raezr posted:EDIT: durr I found an example showing exactly what I want to do on their documentation http://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-methods I was just looking in all the wrong places Cool, that's good to know. The documentation seems totally haphazard to me. Sometimes I find exactly what I need immediately; other times not a single Google search or random following of links can get me close. I'm not sure whether this is a fault of the documentation or of me though It's just weird how I can find whatever I need in the Python documentation in a heartbeat, but not so much with Django.
|
# ? Nov 3, 2008 13:46 |
|
Bonus posted:You can override the manager's __init__ method and give it an extra argument and then save that argument as an instance variable and then call the superclass __init__. That way you can do something like characters.mine(request.user).all() Is there any way to hard code the "request.user" part into the model manager? I can't figure out how to import the request object into models.py. Does request only exist in views.py or something?
|
# ? Nov 3, 2008 14:41 |
|
nbv4 posted:Is there any way to hard code the "request.user" part into the model manager? I can't figure out how to import the request object into models.py. Does request only exist in views.py or something? Look in the Django docs (or just Google) to see what this whole "MVC" thing is the entire point is that you keep the model, views and templates as separate as possible, which makes your apps much easier to maintain and modify. Yes, it makes things slightly more difficult when you have a need to cross layers, but it's well worth it. For example, if you required an HTTP request object in one of your model classes, you'd be pretty screwed if you needed to use that model in a non Web context, such as via a script. In your case, no, there's no way to "hard code" the request user into the model manager -- you'd have to explicitly set up a manager subclass with an extra init parameter, as Bonus suggested, which would let you pass the request in when it's needed. This basically "pokes a hole" into the model layer that you can pass the request through when necessary, but doesn't actually tie the two layers together. I seem to have missed it up-thread -- what's the actual problem being solved here? A custom manager is sometimes useful but when you're talking about needing info related to specific objects, it's almost always better to add regular QuerySet-returning methods to the model class. This is trickier with auth.User because it's not "your" model, but should still be possible (especially if you set up your own model that has a relationship to User; there are lots of blog posts out there about 'extending the User model' if you want to check them out).
|
# ? Nov 3, 2008 15:13 |
|
bitprophet posted:Look in the Django docs (or just Google) to see what this whole "MVC" thing is the entire point is that you keep the model, views and templates as separate as possible, which makes your apps much easier to maintain and modify. Think of it as like a webmail app. Your emails only exist to you. There will never be a time when some user will need to display emails of another user. It seems to me it would be best to define this "fact", if you will, is in the model layer. But I don't know I'm kinda new to this whole MVC thing...
|
# ? Nov 3, 2008 21:44 |
|
Just started blogging(thanks to a bunch of Django people for yelling at me), here's my first post: http://lazypython.blogspot.com/2008/11/lazy-user-foreign-keysthis-is-double.html Let me know what you guys think.
|
# ? Nov 4, 2008 01:19 |
|
Hey guys I'm going to be asking a whole lot of dumb questions the next few weeks because I'm making an online Dungeons and Dragons game in django for a school project. I think we are doing well so far. We are learning Python as we go along and we haven't hit any major road blocks yet. My question right now is how do I split up the models.py class? Right now it's 200 lines of code and growing. I think I can split it up using the __init.py__ class but I'm not entirely sure. How do I go about changing this?
|
# ? Nov 6, 2008 21:15 |
|
MEAT TREAT posted:Hey guys I'm going to be asking a whole lot of dumb questions the next few weeks because I'm making an online Dungeons and Dragons game in django for a school project. Google around for this, there've been a lot of blog entries and such about it. I thought the official docs had a note about it by now, but can't find it. Offhand you can use __init__.py as you sort of mentioned (it's not a class; it's basically an organizational tool you can use to control what a given Python module "exposes" when it's imported. In this case you'd make models.py (a module in a single file) into models/*.py (a module defined in 1+ file within a folder) and have your __init__.py import the constituent parts so it becomes a flat namespace. In other words, if you normally had mymodule/a.py: code:
code:
But what you want is basically to flatten the contents of these into one big flat namespace (since that's what is expected of Django model files for the most part; you need to be able to refer to mymodule.ModelOne, mymodule.ModelTwo, and etc) so you'd need an __init__.py that does something like code:
I know that's probably a lot to absorb for a newbie, but if you read up just a little bit on Python importing and modules, it should make sense.
|
# ? Nov 6, 2008 21:37 |
|
MEAT TREAT posted:Hey guys I'm going to be asking a whole lot of dumb questions the next few weeks because I'm making an online Dungeons and Dragons game in django for a school project. 200 lines isn't very long; don't worry about splitting it up unless the length is interfering with your understanding of the code. That said, the usual way of splitting up models.py is to convert it into a "package"; a directory containing a file named "__init__.py". If your models.py looks like this: code:
code:
|
# ? Nov 6, 2008 21:52 |
|
|
# ? May 16, 2024 04:10 |
|
Janin posted:
I was under the impression that class Meta ordering was "bad", something to do with inheritance, I forget. It was something mentioned by David Cramer at djangocon. Does anyone remember the exact reason why?
|
# ? Nov 6, 2008 22:03 |