Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
unixbeard
Dec 29, 2004

hey, here's a conceptual question that's hopefully really obvious and dumb. I'm writing an app which is multiple todo lists, which is really just a toy app to get familiar with django

If I want it so users can sign up, create/manage their own todo lists (creating/deleting lists, adding/removing items, etc), it seems like the pre built admin stuff can do a lot of this already. Should I just be making some custom view over the existing admin functionality? Or should I be doing my own views/form objects etc

Adbot
ADBOT LOVES YOU

Yay
Aug 4, 2007

unixbeard posted:

it seems like the pre built admin stuff can do a lot of this already. Should I just be making some custom view over the existing admin functionality?
No. The admin site is for administration, not standard users.

unixbeard posted:

Or should I be doing my own views/form objects etc
Yes. Its not hard, and you may be one of those who can get by using the generic objects for CRUD.

unixbeard
Dec 29, 2004

Yay posted:

Yes. Its not hard, and you may be one of those who can get by using the generic objects for CRUD.

yeah I have a feeling I can. Maybe I missed it in the docs but do the generics come with crud operations or do you have to write your own? The generic views page seems to only cover views, and the tutorial keeps the vote handler in urls.py when it gets to generics

unixbeard
Dec 29, 2004

ok i just found the crud objects in the docs, thanks for your help

unixbeard
Dec 29, 2004

it doesnt seem to be working, the form displays and i can post etc but the data isn't being stored, what am i missing here?

pre:
(r'^new/$', 'django.views.generic.create_update.create_object', dict(model=TodoList)),

<form action="" method="post">
<p>{{ form.title.label_tag }} {{ form.title}}</p>
<input type="submit" value="create">
</form>

<p><a href="/todo/">Back</a>

unixbeard
Dec 29, 2004

so it was a problem with my model, learning new frameworks :sigh:

Escher
Dec 22, 2005

If only...
What hosting servies do you guys use/recommend? I am a few months away from deploying a decent sized Django application, and this will be my first time doing anything serious with Django.

Does anyone have good experiences with Mediatemple? They are the supposed official django hosts, says some list I found somewhere.

Doh004
Apr 22, 2007

Mmmmm Donuts...
I'm doing something wrong:

The Error posted:

TemplateSyntaxError: Caught TypeError while rendering: unpack non-sequence

It's dying when (I think) trying to display a MultipleChoiceField. Here's my form:
code:
class RegisterAccountForm(forms.Form):
...
    chapter = forms.ModelChoiceField(Chapter.objects.all())
...
The template:
code:
<tr>
    <th>Chapter</th>
    <td>{{ form.chapter }}</td>
    <td class="tip">Please select the chapter which you're from</td>
</tr>
With the debugger, I see that the choices are being set and are in the form chapter object, yet it still yells at me. Any help would be greatly appreciated.

king_kilr
May 25, 2007

Escher posted:

What hosting servies do you guys use/recommend? I am a few months away from deploying a decent sized Django application, and this will be my first time doing anything serious with Django.

Does anyone have good experiences with Mediatemple? They are the supposed official django hosts, says some list I found somewhere.

Either webfaction for small things or a VPS for larger things (either Rackspace Cloud or Slicehost)

spencer for hire
Jan 27, 2006

we just want to dance here, someone stole the stage
they call us irresponsible, write us off the page

Doh004 posted:

I'm doing something wrong:

The template:
code:
<tr>
    <th>Chapter</th>
    <td>{{ form.chapter }}</td>
    <td class="tip">Please select the chapter which you're from</td>
</tr>
With the debugger, I see that the choices are being set and are in the form chapter object, yet it still yells at me. Any help would be greatly appreciated.

Do you have the html form tags (http://w3schools.com/tags/tag_form.asp)? If you do, ignore this entire post. I'm worthless.
code:
<form action="{% url yourapp.view %}" method="post">
 {{ form.chapter }}
<input type="submit" value="Submit" />
</form>
then in between the <form></form> you can call {{ form }} for the whole thing or {{ form.chapter }} for just the chapter field. The django documentation is really great and there's a lot of neat customization you can do. http://docs.djangoproject.com/en/dev/topics/forms/#displaying-a-form-using-a-template

Lamacq
Jun 15, 2001

Breezeblock RIP

Doh004 posted:

code:
class RegisterAccountForm(forms.Form):
...
    chapter = forms.ModelChoiceField(Chapter.objects.all())
...

Try:
code:
class RegisterAccountForm(forms.Form):
...
    chapter = forms.ModelChoiceField(queryset=Chapter.objects.all())
...
I'm pretty sure ModelChoiceField just wants keyword arguments (not positional, as you've done above)

Doh004
Apr 22, 2007

Mmmmm Donuts...

spencer for hire posted:

Do you have the html form tags (http://w3schools.com/tags/tag_form.asp)? If you do, ignore this entire post. I'm worthless.
code:
<form action="{% url yourapp.view %}" method="post">
 {{ form.chapter }}
<input type="submit" value="Submit" />
</form>
then in between the <form></form> you can call {{ form }} for the whole thing or {{ form.chapter }} for just the chapter field. The django documentation is really great and there's a lot of neat customization you can do. http://docs.djangoproject.com/en/dev/topics/forms/#displaying-a-form-using-a-template
Yes, it's in between a <form>.

Lamacq posted:

Try:
code:
class RegisterAccountForm(forms.Form):
...
    chapter = forms.ModelChoiceField(queryset=Chapter.objects.all())
...
I'm pretty sure ModelChoiceField just wants keyword arguments (not positional, as you've done above)

Same error as before :(

*edit* Got it fixed! Turns out I had left an old line of code from one of my many changes when trying to get it working. Thanks guys :)

Doh004 fucked around with this message at 05:16 on May 4, 2010

MonkeyMaker
May 22, 2006

What's your poison, sir?

Escher posted:

What hosting servies do you guys use/recommend? I am a few months away from deploying a decent sized Django application, and this will be my first time doing anything serious with Django.

Does anyone have good experiences with Mediatemple? They are the supposed official django hosts, says some list I found somewhere.

Have not had good luck with MT, actually. Slicehost and Linode are both awesome to use, though.

geera
May 20, 2003

Escher posted:

What hosting servies do you guys use/recommend? I am a few months away from deploying a decent sized Django application, and this will be my first time doing anything serious with Django.
I haven't used them personally, but I've read good things about WebFaction's Django hosting.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

geera posted:

I haven't used them personally, but I've read good things about WebFaction's Django hosting.

I'm also going to recommend these guys. Last year when I was looking for an easy Django hosting solution they were the only decent ones. They actually give you shell access which was a first time for me on a shared hosting.

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

Trying to set up flowplayer using django-flowplayer, run into this problem while testing:

code:
[06/May/2010 15:51:18] "GET /blog/2010/may/06/test-blog-post/ HTTP/1.1" 200 4049
[06/May/2010 15:51:18] "GET /home/****/django/tradezone/media/flowplayer/example/flowplayer-3.2.0.min.js HTTP/1.1" 404 2366
Through my testserver. The js file is 100% at that location, I've used the url itself in terminal to open it. Changed permissions to be as open as it can be. Moved it to different locations. Must be django doing something, what could it be?

nbv4
Aug 21, 2002

by Duchess Gummybuns
I think it's trying to grab http://localhost:8000/home/****/... which is why it's coming up 404. You need to look through whatever settings flowplayer is using to serve local media. MEDIA_PATH or MEDIA_URL, perhaps.

multigl
Nov 22, 2005

"Who's cool and has two thumbs? This guy!"

nbv4 posted:

I'm pretty sure the problem is with mod_wsgi. In my apache logs, I see the error "premature end of script headers", which is coming from mod_wsgi around the same time apache crashes. It's hard to debug because crashes only happen once every 8 days or so.

Anyways, I upgraded mod_wsgi to 2.8 from 2.5, and I haven't had a crash since, so maybe that'll fix it. I dunno we'll see.

if you still run into this problem, are you also logging flup? We had an issue where several of our sites were mysteriously dying, no errors in any logs except from lighttpd saying "unexpected EOF, nothing on the socket" or something similar.

Turns out we were using cmemcache instead of python-memcache (a recommendation in the django docs :argh:) and cmemcache is not threadsafe. It was throwing errors that weren't being caught, and they'd propagate all the way up and kill the thread.

I figured it out, only after starting the process like this:

code:
python $PROJECT_DIR/project/manage.py runfcgi socket=$SOCKET \
pidfile=$PIDFILE method=threaded deamonize=True \
errlog=/tmp/http_srv/ondemand.error outlog=/tmp/http_srv/ondemand.outlog debug=True
The errors were then being caught and written to the errlog and the site(s) stopped crashing. We switched to python-memcache and now the errors are completely gone, but the moral of the story was catching flup errors enabled us to find the real problem.

MonkeyMaker
May 22, 2006

What's your poison, sir?

multigl posted:

Turns out we were using cmemcache instead of python-memcache (a recommendation in the django docs :argh:) and cmemcache is not threadsafe. It was throwing errors that weren't being caught, and they'd propagate all the way up and kill the thread.

I noticed several errors in the logs once I upgraded mod_wsgi of failures of memcached. I switched from cmemcache to python-memcache and that seems to have fixed a lot/all of the errors. I don't know if it was a combo or not, though, and I don't really care to test it.

king_kilr
May 25, 2007
For the record the docs no longer recommend cmemcached, in 1.3 pylibmc will become the reccomended memcached lib.

nbv4
Aug 21, 2002

by Duchess Gummybuns
speaking of pylibmc, has anyone here ever managed to get it working in ubuntu? iirc, pylibmc requires libmemcache 0.32 or greater, yet lucid only includes 0.31. I manually built the latest version (0.38 I think), but ended up with this: http://stackoverflow.com/questions/2612515/pylibmc-undefined-symbol-memcached-server-list

Also, when I was using the filesystem cache backend, the cache directory seemed to never go above about 8 MB. I'm assuming django deleted the cache data after it expired. Now that I've switched to memcache, the cache seems to just fill up to the capacity limit and stay there. Is that normal or a sign that something is not right?

king_kilr
May 25, 2007
Don't use file based caches. Just don't.

nbv4
Aug 21, 2002

by Duchess Gummybuns

king_kilr posted:

Don't use file based caches. Just don't.

why not? I'm actually thinking of dumping memcache and going back to the file cache at least until my site grows a little more. Memcache uses memory which for me is in short supply, and the file cache performs just as well. For reference, my site gets about 2 access per second during peak times.

multigl
Nov 22, 2005

"Who's cool and has two thumbs? This guy!"

nbv4 posted:

why not? I'm actually thinking of dumping memcache and going back to the file cache at least until my site grows a little more. Memcache uses memory which for me is in short supply, and the file cache performs just as well. For reference, my site gets about 2 access per second during peak times.

Generally because web applications are io bound and pushing something that is supposed to alleviate io latencies onto io isn't the best solution. I don't think it's awful though, using file based caching with django is a known quantity and if it performs well & better than no caching, then why not?

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

I'm using generic views and I'm trying to get_absolute_url to work, but I'm having a problem because I think one of my values I'm passing for the url is a manytomany.

Code:

code:
def get_absolute_url(self):
        return ('flowplayer_videos_detail', (), { 'series': self.series,
                    'slug': self.slug })
    get_absolute_url = models.permalink(get_absolute_url)
series is a manytomany that links to another model. I can't get the url to properly link to the specific series/slug combo.

code:
urlpatterns += patterns('django.views.generic.list_detail',
                        (r'^(?P<series>[-\w]+)/$', 'object_list',
                            video_info_dict, 'flowplayer_videos_detail'),
                        (r'^(?P<series>[-\w]+)/(?P<slug>[-\w]+)/$', 'object_detail',
                            video_info_dict, 'flowplayer_videos_detail'),
                    )
This is part of my url, you can see what I'm trying to do, but in my newbishness I can't think of what to do to change this. My date based generic view works perfectly, its just using get_absolute_url to link to a template that will display a specific slug. Can anyone point me in the right direction or blast my thinking?

nbv4
Aug 21, 2002

by Duchess Gummybuns
when you do self.series what comes out? I'm guessing it's something that doesn't match [-\w]+ in your url

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!

nbv4 posted:

why not? I'm actually thinking of dumping memcache and going back to the file cache at least until my site grows a little more. Memcache uses memory which for me is in short supply, and the file cache performs just as well. For reference, my site gets about 2 access per second during peak times.

Contrary to popular belief, rdbms are not that slow, specially for indexed key/value storage (where the queries will be easily cached). The DB backend is probably going to be faster, more thread-safe and less io intensive than file caching.

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

nbv4 posted:

when you do self.series what comes out? I'm guessing it's something that doesn't match [-\w]+ in your url

My regex skills is near zero, whats the best way to come up with them? The url will be something like videos/random-series/random-video

duck monster
Dec 15, 2004

I'm having a problem with Django spitting blood about an underlying MySQL view.

I have a MySQL view that looks like this:

code:
SELECT 
    `prepaid`.`id` AS `id`,
    `prepaid`.`balance` AS `balance`,
    `line`.`customer_id` AS `customer_id`,
    `line`.`extension` AS `extension` 
FROM  `opensips`.`cdr_prepaid` AS `prepaid` 
JOIN 
      `opensips`.`vtel_line` AS `line` 
ON
  `line`.`extension` + _latin1'@url.of.client.com'  = `prepaid`.`account`   
The problem is when using inspectdb now , it vomits all over the floor when using inspectdb.
code:
class CustomerBalances(models.Model):
Traceback (most recent call last):
  File "./manage.py", line 11, in <module>
    execute_manager(settings)
  File "/usr/lib/python2.5/site-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/usr/lib/python2.5/site-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/python2.5/site-packages/django/core/management/base.py", line 195, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/lib/python2.5/site-packages/django/core/management/base.py", line 222, in execute
    output = self.handle(*args, **options)
  File "/usr/lib/python2.5/site-packages/django/core/management/base.py", line 351, in handle
    return self.handle_noargs(**options)
  File "/usr/lib/python2.5/site-packages/django/core/management/commands/inspectdb.py", line 22, in handle_noargs
    for line in self.handle_inspection(options):
  File "/usr/lib/python2.5/site-packages/django/core/management/commands/inspectdb.py", line 47, in handle_inspection
    relations = connection.introspection.get_relations(cursor, table_name)
  File "/usr/lib/python2.5/site-packages/django/db/backends/mysql/introspection.py", line 53, in get_relations
    my_field_dict = self._name_to_index(cursor, table_name)
  File "/usr/lib/python2.5/site-packages/django/db/backends/mysql/introspection.py", line 46, in _name_to_index
    return dict([(d[0], i) for i, d in enumerate(self.get_table_description(cursor, table_name))])
  File "/usr/lib/python2.5/site-packages/django/db/backends/mysql/introspection.py", line 38, in get_table_description
    cursor.execute("SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name))
  File "/usr/lib/python2.5/site-packages/django/db/backends/util.py", line 19, in execute
    return self.cursor.execute(sql, params)
  File "/usr/lib/python2.5/site-packages/django/db/backends/mysql/base.py", line 86, in execute
    return self.cursor.execute(query, args)
  File "/var/lib/python-support/python2.5/MySQLdb/cursors.py", line 168, in execute
    if not self._defer_warnings: self._warning_check()
  File "/var/lib/python-support/python2.5/MySQLdb/cursors.py", line 82, in _warning_check
    warn(w[-1], self.Warning, 3)
  File "/usr/lib/python2.5/warnings.py", line 62, in warn
    globals)
  File "/usr/lib/python2.5/warnings.py", line 102, in warn_explicit
    raise message
_mysql_exceptions.Warning: Truncated incorrect DOUBLE value: '@customer.url.com'
Why the gently caress is it nosying about in the view structure like that? Can I tell it "Django, that view is for another app, not for you, just pay attention to the tables please."

Occasionally and very randomly the app crashes with the same bug, despite the fact that the table/view in question isn't used at all, nor is there a schema in models.py for it.

Very loving annoying. This , by the way, is why magic is bad for frameworks.

Any solutions?

edit: seems like changing the string "addition" to a CONCAT() does the trick, but I still dont understand why Django cares about the views. Not for you Django!

duck monster fucked around with this message at 04:58 on May 11, 2010

Yay
Aug 4, 2007
I would guess that it just does
code:
SHOW TABLES
or equivalent, without testing for it being 'BASE TABLE', or 'VIEW', or whatever. Which isn't magic, so much as a lack of constraints.

Edit: specifically, its used here without any filtering. It may be intentional (because django can make use of views if they're marked unmanaged in the models), but should maybe be filtered for inspectdb? I'm not sure whether the problem is local to you and your view (after all, concat fixed it) or in how django's parsing that. There's nothing currently listed regarding it, so if you can make a reproducible case, I'd file a ticket.

Yay fucked around with this message at 10:44 on May 11, 2010

raymond
Mar 20, 2004

LuckySevens posted:

I'm using generic views and I'm trying to get_absolute_url to work, but I'm having a problem because I think one of my values I'm passing for the url is a manytomany.

Try passing it a string instead of "self.series" which you said is a ManyToMany field. Decorators are neat too.

code:
@models.permalink
def get_absolute_url(self):
    return ('flowplayer_videos_detail', (), {'series': 'testing', 'slug': self.slug})
You'll probably have to figure out a way of picking a single series out of the series list. Example:

code:
@models.permalink
def get_absolute_url(self):
    return ('flowplayer_videos_detail', (), {'series': self.series.all().order_by('id')[0].slug, 'slug': self.slug})
Of course that would break if there are no series selected, but this is just an example.

inveratulo
May 14, 2002
wat
I've been playing with Django for a few hours and I guess I am still trying to wrap my brain around the many-to-many relationship and whether I really need it just to create a widget that display a multiple choice field. I don't mind overridding the admin models in admin.py, but I still can't seem to get a real, multiple choice, form to work properly.

Does anyone know of a little walkthrough or some kind of pre-built example that can get me over this first hump of understanding?

Yay
Aug 4, 2007

inveratulo posted:

I still can't seem to get a real, multiple choice, form to work properly.
If you've got a multiple choice field (select, checkbox etc), the resulting key in request.POST/GET will be a querydict. IIRC, printed to stdout, or into a template, it always displays the first (or is it last?) one selected, giving the appearance that its not working—It comes up on #django IRC all the time—even though if iterated over, all the values are there.

I'm assuming the common pitfall here, so if thats not the problem you're having, can you explain the problem in a more expansive way? Not being able to wrap your head around the idea of various relationships isn't the easiest thing to rubberduck. (Edit: are you having trouble with the idea of M2M, or with forms, or ModelForms, or ModelAdmins?)

Yay fucked around with this message at 19:47 on May 11, 2010

inveratulo
May 14, 2002
wat

Yay posted:

If you've got a multiple choice field (select, checkbox etc), the resulting key in request.POST/GET will be a querydict. IIRC, printed to stdout, or into a template, it always displays the first (or is it last?) one selected, giving the appearance that its not working—It comes up on #django IRC all the time—even though if iterated over, all the values are there.

I'm assuming the common pitfall here, so if thats not the problem you're having, can you explain the problem in a more expansive way? Not being able to wrap your head around the idea of various relationships isn't the easiest thing to rubberduck. (Edit: are you having trouble with the idea of M2M, or with forms, or ModelForms, or ModelAdmins?)

I guess I set out to make a scheduling app for watching automated jobs. I was going to use Chronograph but it has too many features and I would just rather learn how to do all this myself anyway, because it would have to be heavily retooled anyway. The idea was to have a field where one can select multiple months, or days, or whatever the repeat interval (think of it in terms of cron jobs that run in regular intervals). So, I am trying to override the admin models and create multiple select checkboxes, but I have not been able to make it work. The Django admin page just ignores all of my models if something has gone wrong.

Yay
Aug 4, 2007

inveratulo posted:

I guess I set out to make a scheduling app for watching automated jobs. I was going to use Chronograph but it has too many features and I would just rather learn how to do all this myself anyway, because it would have to be heavily retooled anyway. The idea was to have a field where one can select multiple months, or days, or whatever the repeat interval (think of it in terms of cron jobs that run in regular intervals). So, I am trying to override the admin models and create multiple select checkboxes, but I have not been able to make it work. The Django admin page just ignores all of my models if something has gone wrong.

At its simplest, you wouldn't require any relationship—handling the idea of something that occurs at N steps only requires two fields:
code:
class Schedule(models.Model):
    interval = models.PositiveIntegerField()
    occuring = models.PositiveIntegerField(choices=((1, 'Minutes'),(2, 'Hours'), (3, 'Days'))) #etc
I'm assuming that field type accepts choices; I really can't remember. But yeah. How many different levels of occurance can there be? And they'll never change, unless y'know, we start measuring time differently.

If, for example, you wanted a Job to be able to run at multiple times, I'd create Job class, which has inlines (generic or FK'd) of Schedule objects.

inveratulo
May 14, 2002
wat

Yay posted:

At its simplest, you wouldn't require any relationship—handling the idea of something that occurs at N steps only requires two fields:
code:
class Schedule(models.Model):
    interval = models.PositiveIntegerField()
    occuring = models.PositiveIntegerField(choices=((1, 'Minutes'),(2, 'Hours'), (3, 'Days'))) #etc
I'm assuming that field type accepts choices; I really can't remember. But yeah. How many different levels of occurance can there be? And they'll never change, unless y'know, we start measuring time differently.

If, for example, you wanted a Job to be able to run at multiple times, I'd create Job class, which has inlines (generic or FK'd) of Schedule objects.

This is really a better way of thinking about things than how I was going about it. But it still doesn't address the edge cases of say when I want a job to run daily, except on Sundays.

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

Something I'm missing here, I'm trying to use a generic view to display a list of a certain value in my database, but from what I'm reading to do this, it needs to be a primary key/slug? The url works fine when its a slug, but is there a way to do it if its a non-primary key value, or do I have to write a view?

king_kilr
May 25, 2007

LuckySevens posted:

Something I'm missing here, I'm trying to use a generic view to display a list of a certain value in my database, but from what I'm reading to do this, it needs to be a primary key/slug? The url works fine when its a slug, but is there a way to do it if its a non-primary key value, or do I have to write a view?

If you spend more than about 10 seconds thinking about it just write a real view. They aren't that much work.

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

Yeah, I just wrote the view myself, everything works smoothly now except:

code:
    @models.permalink
    def get_absolute_url(self):
        return ('flowplayer.views.videos_detail', (), { 'series': VidSeries.slug,
                    'slug': self.slug })
I can't work out how to get my get_absolute_url working correctly, series is a manytomany with a model called VidSeries, I'm using the slug from that model as part of the url dispatcher, I dpasted the other info here. I can't seem to find any info apart from the official django docs on get_absolute, I've also tried self.series.slug to no avail. Something fundamental I must not be getting here.

Adbot
ADBOT LOVES YOU

king_kilr
May 25, 2007
What you're missing is that you have multiple VidSeries for a given self, how do you know which slug, from all of those, you want?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply