|
Larry Horse posted:edit: I just blanked the strings in the enum structure. I don't know if this is the "right" way to do this, but it works. By which I assume you mean your "enum"s look like this: code:
If so, that's one way to do it, since you don't actually intend for each individual radio button to have a label. It's also probably the quickest way, given that the other alternative is to manually spit out the HTML yourself (and checking e.g. {{ form.some_field.data }} to determine which radio buttons are selected, if you ever display the form in a previously-filled-out state, like if you redisplay on user error). I say this assuming you currently do something like {{ form.some_field }} for the display...which is what is automatically spitting out labels. If you're on trunk instead of 0.96, I think there may be a way to print the formfield sans label, with newforms (the form/manipulator stuff in 0.96 is now 'oldforms'; newforms is a lot better and gives you more granular control) but I don't know for sure off the top of my head.
|
# ? May 8, 2008 03:35 |
|
|
# ? May 15, 2024 03:23 |
|
Yep, that's exactly how it looks. And it works! I think it's a testament to how good Django is that whenever I have to do anything somewhat "manually" I wonder if I'm doing it right.
|
# ? May 8, 2008 13:01 |
|
Yea, Django's awesome like that One minor note, the approach you're taking is potentially a little "off" accessibility-wise and/or markup-wise, in that it is probably littering your code with lots of <label for="field_name"></label> tags, i.e. tags which are generally intended to have text in them but which are now empty. Chances are this isn't a huge problem, or that it's not an issue in your case (tho it could potentially cause style problems in e.g. IE6 even if you don't care about people with screen-readers), but I wanted to make sure you knew the diff between this approach and one where the <label> tags aren't even displayed
|
# ? May 8, 2008 13:10 |
|
Strangely enough, I happen to work for a non-profit where accessibility is an issue. Eventually I'll have to create a subclass and render it myself, I guess. But for now I'll get the thing working. I'm using the reportlab python package to make PDFs right now as part of the package, actually. It's pretty cool
|
# ? May 8, 2008 20:25 |
|
Larry Horse posted:Yep, that's exactly how it looks. And it works! For example if you didn't know. You have a model already pre-built. Need a form for that model? ModelForm = form_from_model(SomeModel) ModelForm.as_ul() Poof..
|
# ? May 8, 2008 21:05 |
|
ATLbeer posted:I had the same reaction when I started using Django myself. I found myself implementing something that was already built into Django. This is no longer the correct way to do it actually, if you are using SVN you should use ModelForms.
|
# ? May 9, 2008 18:03 |
|
deimos posted:
Just another response to this: I remember trying sqlreset before but couldn't remember why I'm not using that command. Tried it again today. The thing with sqlreset is that you have to specify all the app names that you want to reset. That's a bit of a hassle when your project is decoupled into a bunch of apps. So I guess I'm still sticking with my yes "no" script
|
# ? May 9, 2008 18:29 |
|
I'm trying to filter objects by a foreign key. In my models.py I have: code:
What I'd like to do is get all entries added by this user, but code:
Filtering by slug works without trouble, so I'm guessing it has something to do with the field being a ForeignKey?
|
# ? May 12, 2008 16:47 |
|
Use a double underscore to follow foreign key relationships when filtering, selecting, etc.code:
code:
|
# ? May 12, 2008 16:59 |
|
I just created my first Django project and I am having some trouble figuring out where certain work should happen (I'm new to Web development and Django). Hopefully someone can help. For example: I am writing a simple referee scheduler for youth soccer. I have a game model, a referee model, and a basic template where I list out each game and referee. What I would like to do is have games listed in a column on the left, referees listed in a column on the right, and let the user click a game then click a ref and have the assignment made. This is where my confusion comes in, because I'm not sure where I should do the work of assigning the referee to the game. Is this in a separate view? How do I get information into this other view, such as the selected game? Then, finally, after this assignment is made, how do I reload the original page (with the updated assignment) so the user can continue to make selections? Thanks.
|
# ? May 12, 2008 18:32 |
|
Not sure if you've been using generic views for your existing template(s), but for this you will need a normal, non-generic view. For input it'll probably just take the request, and you'll want to use a form with checkboxes or radio buttons to make this happen (you can use JavaScript so that clicking the name of the game/referee selects the appropriate form control, if you want to make it even easier for users). Then your view will do what's a pretty normal form-processing flow control: check to see if request.POST is non-empty (and request.POST is a dict, so you can just do if request.POST: since an empty dict is equal to False). If it is non-empty (i.e. the user has submitted the form) you see what game/referee were selected and make the association in the database (how exactly this is done depends on how you've related the two models, but I'm assuming a many-to-many given your description). After that if statement (but not in an else!) you will then want to render the template; users hitting the page for the first time will just get the normal display, while users who have submitted the form will get it back again, as you've said you want them to. Here's an example...please don't copy-paste this, as I'm going from memory, but instead make sure you understand what it's doing and look up the correct methods and such to use Copy-paste is a terrible way to learn! Also, I am very lazy when whipping up HTML for the first time, so this isn't awesome front-end work either (but CSS-styled divs and spans are generally better than tables, regardless). Template code:
code:
code:
|
# ? May 13, 2008 02:05 |
|
Can anyone recommend any talks or presentations about Django? I have already watched Web Development for Perfectionists with Deadlines(Google Tech Talk) given by Jacob Kaplan-Moss, and Snakes and Rubies. I seem to get a lot, including motivation, out of things like that.
|
# ? May 13, 2008 03:53 |
|
bitprophet posted:One way of accomplishing the task I wanted to show another way to accomplish this task, though I might be off a little on syntax. I thought modelchoicefields would be a better choice than radiobuttons, but that's just me of course, I am only 2 months old in django and web stuff in general, so take that as you will. Model I used the same one as Bitprophet Template code:
code:
Wulfeh fucked around with this message at 04:28 on May 13, 2008 |
# ? May 13, 2008 04:21 |
|
Wulfeh's approach is quite valid and leverages the forms framework more than mine does. Main difference template-wise is that his uses the default representation of a field with choices (forget what that is these days but it used to be a dropdown list or a multi-select box depending on field type), whereas mine has a more explicit/manual HTML setup. The former is quicker while the latter gives you more control. I guess I went with the latter myself because the way the problem was phrased made me think "ok, he's got a fairly specific page layout in mind, so that means custom form HTML", but that's not always necessary. Mine actually ended up involving less work than Wulfeh's, but is also less realistic as it omits all the error checking and such In the long run, the more you can leverage the forms stuff, the better off you'll be.
|
# ? May 13, 2008 05:23 |
|
Can somebody post an example of how to use the validators mentioned at the bottom of http://www.djangoproject.com/documentation/forms/ The one provided isn't to clear, for a newb like me, on the proper usage. Specifically I have three fields that are only required if the user selects 'Yes' on a BooleanField with the RadioSelect widget. If the user selects or No or selects nothing the fields aren't required. I'm thinking RequiredIfOtherFieldGiven but prove me wrong
|
# ? May 14, 2008 06:29 |
|
FrontLine posted:Can somebody post an example of how to use the validators mentioned at the bottom of http://www.djangoproject.com/documentation/forms/ Don't use that, you should be using newforms. And also, for everyone using pretty much any version of django there is a security update to fix an XSS issue in the Admin, more info on the django blog.
|
# ? May 14, 2008 06:49 |
|
Wulfeh posted:checkboxes and newforms. Interestingly I recently wanted to select the available check boxes based on data passed to my view. This isn't that well documented so I thought i'd share my example. forms: code:
views: code:
|
# ? May 14, 2008 19:41 |
|
I'd usually do that in the __init__ method, rather than in the view. [code] class SampleForm(forms.Form): field = forms.ModelChoiceField(queryset=Model.objects.none()) def __init__(self, *args, **kwargs): user = kwargs.pop('user') super(SampleForm, self).__init__(*args, **kwargs) self.fields['field'].queryset = Model.objects.filter(owner=user)
|
# ? May 14, 2008 21:02 |
|
FrontLine posted:I want to use validation errors king_kilr posted:Use newforms Yea the old forms and manipulators are on the way out I believe. To do it in newforms, you would put something like this in your form class: code:
|
# ? May 14, 2008 21:02 |
|
I'd usually do that in the __init__ method, rather than in the view. for example: code:
|
# ? May 14, 2008 21:02 |
|
Something I am having trouble with are perms, I want to do something like user.has_perm(perm_object), but IRC told me that has_perm won't validate a perm object. I am not sure how to go about checking some custom permissions, I am open to ideas. ( I am trying to restrict a fields choices with permissions ). Invalid code code:
ModelMultipleChoiceField(queryset=Tags.objects.filter(perms__exact=user__user_permissions)) Wulfeh fucked around with this message at 21:26 on May 14, 2008 |
# ? May 14, 2008 21:19 |
|
king_kilr posted:Don't use that, you should be using newforms. I am using newforms I'm just a bit to new to this to realise what's deprecated and what's not. Wulfeh posted:http://www.djangoproject.com/documentation/newforms/#custom-form-and-field-validation I'll give this a try and post a trip report. Thanks!
|
# ? May 15, 2008 01:09 |
|
Wulfeh posted:
Gah, this is a pet peeve of mine, it's a bit wasteful and IMO makes it harder to read when you have a single method that does both: self.cleaned_data.get('bool_field', False) works fine to replace both of those.
|
# ? May 15, 2008 03:40 |
|
deimos posted:Gah, this is a pet peeve of mine, it's a bit wasteful and IMO makes it harder to read when you have a single method that does both: self.cleaned_data.get('bool_field', False) works fine to replace both of those. Even better, just .get('foo'). It'll return None when the key doesn't exist, which evaluates to False in a boolean context. I'd be more concerned about his tabs for indentation, which is definitely not part of any accepted Python coding standard or style guideline that I've ever heard of or seen before (the official Python style guide specifies 4 spaces for tabs). The if not ... in ... is also kind of hard to read. I'd use if ... not in ...
|
# ? May 15, 2008 03:48 |
|
deimos posted:Gah, this is a pet peeve of mine, it's a bit wasteful and IMO makes it harder to read when you have a single method that does both: self.cleaned_data.get('bool_field', False) works fine to replace both of those. That's pretty cool, i'll try to start using that. Milde posted:.get('foo') Even better for the bool Honestly, it must be from the way I copied it from my editor, because my tab settings are set to 4 spaces. I don't see why indentation should even be mentioned, it's obvious I am totally trying to "break the standard" and be "unique". for the: if not ... in ... and the if ... not in ... is that just a preference of yours, or do most Python developers try to strive for the latter? (it's like asking "if there isn't this field in the cleaned data", or "if the field isn't in the cleaned data")
|
# ? May 15, 2008 05:27 |
|
Wulfeh posted:for the: if not ... in ... and the if ... not in ... is that just a preference of yours, or do most Python developers try to strive for the latter? (it's like asking "if there isn't this field in the cleaned data", or "if the field isn't in the cleaned data") I'd use not in because it reads more like English. It's just easier for me to read, that's all. Using not in reads to me like both of your examples, whereas using not ... in ... makes me pause for a second to think about it. It's obviously not a huge deal, but readability is a pretty important tenet of Python, so I guess I notice these things more than in other languages.
|
# ? May 15, 2008 05:32 |
|
bitprophet posted:awesome stuff... Wulfeh posted:awesome stuff...
|
# ? May 16, 2008 21:27 |
|
It took me a few weeks of playing with the whole django framework and finally having that "Aha!" moment where you understand how all the different parts interact. I never knew about MVC frameworks, web development, databases, or anything of that sort before I started learning django and python, I had mostly only known how to do console stuff with C or C++. So it is kind of fun seeing how easy it is to create something and see results pretty much instantly. I also love how python is so easy to work with, compared to literally "fighting" with C++.
|
# ? May 17, 2008 02:54 |
|
Wulfeh posted:I also love how python is so easy to work with, compared to literally "fighting" with C++. IMHO that's Python's greatest strength: most of the time, your biggest problem is...the problem at hand...not the language or the libraries. When you get down to it, programming is just problem solving and domain modeling, and Python makes it very easy to focus almost entirely on what you're doing, not how you're doing it. Jacko posted:I have been backtracking to make sure I understand the fundamentals Good! It'll take a bit of time, but I'm glad you're doing this - too many people care only about the end result and "getting it working" instead of understanding the tool and/or the right way to solve the problem at hand. If you can manage to juggle both the desire to learn the "Right Way" of doing stuff, and the desire to get things done on time, you'll be a much better developer than most of 'em
|
# ? May 17, 2008 03:54 |
|
I've made one or two custom template filters which I would like to use on every or almost every single page of the web site. I have a utils application, in it I've created a templatetags folder and added my_filters.py in there. How can I have access to the methods in my_filters without loading it from every template? That would be easier. Currently, I always have to put {% load my_filters %} to the top of each one.
|
# ? May 17, 2008 10:48 |
|
http://www.djangosnippets.org/snippets/160/ Found by googling for django templatetags "every template" I knew this existed but couldn't remember the specific method to use, and it's not documented for some reason (pretty unusual for Django, but could imply that it's not considered 'stable' yet). bitprophet fucked around with this message at 15:22 on May 17, 2008 |
# ? May 17, 2008 15:20 |
|
It's not documented because it's considered internals, my guess is it's that way because sometimes DRY and explicit bump heads.
|
# ? May 17, 2008 21:54 |
|
bitprophet posted:http://www.djangosnippets.org/snippets/160/ Cool, in order to get it to work I had to use something like this but it works. code:
I would be concerned, that if importing does use up resources, then more resources are being used than necessary. Thoughts?
|
# ? May 18, 2008 03:09 |
|
Nolgthorn posted:This raises the question about how liberally imports should or should not be used. How much extra processing is required when you use urls.py and views.py to import a lot of stuff, instead of just directing everything in your urls.py file to you view, to only import there. I'm reasonably sure that it doesn't matter; everything except for templates is loaded once, and only once, per Apache child process (or whatever Web server process you're using which has loaded up a Python interpreter) so whether something is imported to your URLconf or your view, it's going to take up memory for that particular process either way. (Also pretty sure that Python interpreters are smart enough that only one copy of each module gets loaded, so importing your Foo model class everywhere won't be a problem...) This is a good thing, though, because re-loading and re-interpreting all your Django/Python code on each request would be pretty god-awful performance-wise.
|
# ? May 18, 2008 04:17 |
|
Wulfeh posted:Something I am having trouble with are perms, I want to do something like user.has_perm(perm_object), but IRC told me that has_perm won't validate a perm object. I am not sure how to go about checking some custom permissions, I am open to ideas. ( I am trying to restrict a fields choices with permissions ). Answering my own question again, in case anyone finds it useful. This allowed me to add permissions from the admin panel to someone, and if they have those permissions, it restricts what options they can view in that choicefield. If they are a superuser, then they get all permissions. code:
code:
|
# ? May 20, 2008 03:54 |
|
Anyone have any Django-based calendaring code they wouldn't mind sharing? It seems like a common use, but I don't see much sample code via Google. (If no one's got any, though, I'll post what I have once I finish it up.)
|
# ? May 21, 2008 17:16 |
|
Larry Horse posted:Anyone have any Django-based calendaring code they wouldn't mind sharing? It seems like a common use, but I don't see much sample code via Google. http://www.djangosnippets.org/tags/calendar/ There a couple there, and if those aren't exactly what your looking for, I am sure they would be good references
|
# ? May 21, 2008 18:10 |
|
Wulfeh posted:http://www.djangosnippets.org/tags/calendar/ One of those is exactly what I needed. It even works with the models I've already created! I totally forgot about djangosnippets, too (doh!) Thanks!
|
# ? May 21, 2008 19:23 |
|
okay I've crawled out of my hole - the tutorial was a bit confusing with the str methods- time to see if I'm doing things correctly. A few posts to make sure I'm on track First) performing a len on database results - either djangobook or the documentation says not to do this: code:
Second random result generator: code:
Third: Static pages code:
Finally I would like to make sure that my grammar is correct on this site I'm building. So I get something like "0 results" verses "1 result" - models.py has the meta tag. code:
one more thing: code:
So, does this all look okay?
|
# ? May 22, 2008 17:38 |
|
|
# ? May 15, 2024 03:23 |
|
politicorific posted:performing a len on database results - either djangobook or the documentation says not to do this: code:
politicorific posted:Third: Static pages code:
code:
politicorific posted:Finally I would like to make sure that my grammar is correct on this site I'm building. So I get something like "0 results" verses "1 result" - models.py has the meta tag. code:
So for what you wanted you could do code:
|
# ? May 22, 2008 20:09 |