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
duck monster
Dec 15, 2004

Maluco Marinero posted:

Looking forward to straightforward positional and keyword arguments in template tags. Will make it much more appetising to use.

Also, does anyone know if there are any sort of conventions for building OO apps in Django? I'm assuming people do it, but I dont see much material about it.

Are there any standard patterns used for it, I've just turned my little CMS, Shop and Payments app each into objects so they can be inherited and change bits and pieces, and it seems like the sensible way to approach rolling out reusable applications that don't need to be forked every time you have to change something.

The experience has made me look into using Flask for a project, seems pretty attractive for that style of development, albeit with more work.

M-V-C dude. Know it + live it. All the "rails" type frameworks, Rails, Django, Cake, etc use it. poo poo, the only one that I know of that doesnt is seaside, but thats because its a crazy smalltalk framework for total berks

Adbot
ADBOT LOVES YOU

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
You can also look at http://www.pylonsproject.org/, which looks quite neat.

Fangs404
Dec 20, 2004

I time bomb.
I've been using Django for a project I'm working on for the last few months, and I've gotta admit, using any other language/framework is just painful now (well, other than something similar like Ruby). I can't loving stand PHP now.

duck monster
Dec 15, 2004

Fangs404 posted:

I've been using Django for a project I'm working on for the last few months, and I've gotta admit, using any other language/framework is just painful now (well, other than something similar like Ruby). I can't loving stand PHP now.

Wait till you get to the edge cases where you want it to do something slightly unusual! Django's awesome, but sometimes I feel like its a giant spiders nest just luuuuuring you in before you get raped by Ungoliant.

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
That's where I feel I'm at now. Django was absolutely excellent for finding my feet in Python and building my initial proof of concept, but having read around about people's experiences with it as they've grown their applications, you start to hit roadblocks where grappling with the hidden magic in Django starts to become more and more time consuming.

I'm having a bit of a play around with sqlalchemy and flask at the moment. Some elements of it are really cool, but there's no way I would've understood what I was doing before my time with Django.

duck monster
Dec 15, 2004

Maluco Marinero posted:

That's where I feel I'm at now. Django was absolutely excellent for finding my feet in Python and building my initial proof of concept, but having read around about people's experiences with it as they've grown their applications, you start to hit roadblocks where grappling with the hidden magic in Django starts to become more and more time consuming.

I'm having a bit of a play around with sqlalchemy and flask at the moment. Some elements of it are really cool, but there's no way I would've understood what I was doing before my time with Django.

I dont want to disuede anyone. The database layer is awesome and it CAN be used very flexibly if your smart. We built a controller system for a VOIP provider right down to real time interrogation of call logs and everything.

But there are some edges where sometimes its better to just treat it like PHP w/t Smarty and roll it all top to bottom and try and ignore the magic.

ufarn
May 30, 2009
I just went from Userena to django-registration, because I get the impression that the userena guys don't want to maintain the code, but holy crap, is django-registration really as bare-bones as my impression is, or do I just need to dig into the documentation to expand it?

It doesn't even have a "Remember Me" box when logging in - since it's using the admin log-in screen, obviously.

It, too, doesn't seem to understand Gmail's + filters, which means that you can make an infinite amount of accounts when signing up with the same e-mail address, as long as you use a different filter every time.

I hope his BitBucket takes pull requests ...

ufarn fucked around with this message at 14:51 on Jan 24, 2012

Captain Capacitor
Jan 21, 2008

The code you say?

ufarn posted:

I just went from Userena to django-registration, because I get the impression that the userena guys don't want to maintain the code, but holy crap, is django-registration really as bare-bones as my impression is, or do I just need to dig into the documentation to expand it?

It doesn't even have a "Remember Me" box when logging in - since it's using the admin log-in screen, obviously.

It, too, doesn't seem to understand Gmail's + filters, which means that you can make an infinite amount of accounts when signing up with the same e-mail address, as long as you use a different filter every time.

I hope his BitBucket takes pull requests ...

Depending on your application this isn't necessarily a bad thing. It sure as hell made my testing process easier.

deimos
Nov 30, 2006

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

ufarn posted:

It, too, doesn't seem to understand Gmail's + filters, which means that you can make an infinite amount of accounts when signing up with the same e-mail address, as long as you use a different filter every time.

Just to nitpick; using + for subaddressing predates gmail and it's been an RFC proposal since around '03.

Opinion Haver
Apr 9, 2007

ufarn posted:

It, too, doesn't seem to understand Gmail's + filters, which means that you can make an infinite amount of accounts when signing up with the same e-mail address, as long as you use a different filter every time.

Why do you care about this? If someone really wants to make throwaway accounts they'll do it using mailinator or something else.

ufarn
May 30, 2009

yaoi prophet posted:

Why do you care about this? If someone really wants to make throwaway accounts they'll do it using mailinator or something else.
It's also convenient for people who don't know nor can't remember that they registered at the site.

Mulozon Empuri
Jan 23, 2006

ufarn posted:

I just went from Userena to django-registration, because I get the impression that the userena guys don't want to maintain the code, but holy crap, is django-registration really as bare-bones as my impression is, or do I just need to dig into the documentation to expand it?

It doesn't even have a "Remember Me" box when logging in - since it's using the admin log-in screen, obviously.

It, too, doesn't seem to understand Gmail's + filters, which means that you can make an infinite amount of accounts when signing up with the same e-mail address, as long as you use a different filter every time.

I hope his BitBucket takes pull requests ...

It's pretty easy to create your own backend in registration to do stuff like 'Remember Me'. I seem to recall that's he's being pretty nazi about accepting pull requests though, so good luck with that.

I'm currently doing a project that uses django-registration and django-registration-defaults which takes care of the templates.

ufarn
May 30, 2009

Mulozon Empuri posted:

It's pretty easy to create your own backend in registration to do stuff like 'Remember Me'. I seem to recall that's he's being pretty nazi about accepting pull requests though, so good luck with that.

I'm currently doing a project that uses django-registration and django-registration-defaults which takes care of the templates.
Thanks for django-registration-defaults. I'll get straight to using those, after I remember how the hell Jinja works after messing with Mako.

I think a lot of this is just feature creep on my side, especially since I am fairly new to programming and Django and just can't wait to fledge out all my project ideas.

On another note, does anyone have any idea why my deployed app would switch to my front page, whenever I don't include a trailing slash in the URLs? It works perfectly fine locally.

Comrade Gritty
Sep 19, 2011

This Machine Kills Fascists
Fwiw Pinax is pretty great to use w/ Django. I prefer it over userna/django-registration.

ufarn
May 30, 2009
If you do stick with django-registration, like I am doing right now, someone just released django-twostepauth, which, despite its abysmal name, is a great app that has some tremendous documentation and even comes with an example app. I found this just as I was looking for a way to implement two-factor authentication, and I am this close to implementing it properly.

Having to write "twostepauth" in my code drives me mental, though.

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
Heh yeah, I think it's worth naming things you're gonna use alot with a little personality, that's easy to read in full pages of code. That's why all of my reusable little applications are japanese occupations, cause there's no mistaking what and where they are in me code.

Meanwhile, had a good bit of fun with Flask and SQLAlchemy, but I think I'm just a bit too dumb to really build a full size application out of them at present, I don't understand enough about how contexts pass around and some other bits and pieces even though I love the power and speed of it. I'm gonna do a small side project with that when I have some time and just focus on getting the best out of django I can with my current work.</e/n>

duck monster
Dec 15, 2004

ufarn posted:

If you do stick with django-registration, like I am doing right now, someone just released django-twostepauth, which, despite its abysmal name, is a great app that has some tremendous documentation and even comes with an example app. I found this just as I was looking for a way to implement two-factor authentication, and I am this close to implementing it properly.

Having to write "twostepauth" in my code drives me mental, though.

I dunno. Seems pretty descriptive to me. At leasts not called something like "django-flungo" or some brain-damaged rails drone type thing.

ufarn
May 30, 2009

duck monster posted:

I dunno. Seems pretty descriptive to me. At leasts not called something like "django-flungo" or some brain-damaged rails drone type thing.
It's very descriptive, it's just that three one-syllable names lumped together in one word makes me paranoid about making a typo somewhere. It's also a bit of a pain to scan. Even the author has renamed it to "tsa" in his code, but I'll try to write it in full, as long as my sanity allows.

duck monster
Dec 15, 2004

More reasons it would be neat if python had macros!

ufarn
May 30, 2009
Speaking of which, it's been ages since I got a very basic Django error, and in trying to implement django-twostepauth, I get a 404 - Page not found, and eff me if I can remember what that even means. This is not an urlconf error, as it would tell me tell me so explicitly, nor is it a view nor template error, but what is it then?

I simply get a 404 page with nothing else - just a blank debug 404 page with no traceback nor anything else. What does this error message even mean?

It's a @login_required() view, because I have to log in before using the view, but after that, I just get the infamous 404.

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
That's weird. If it's demanding a login at the exact same url, then it must be a 404 raised somewhere inside that view. If it's not that then the problem would be in whatever middleware gets in before the request yeah?

ufarn
May 30, 2009
I just had a look at the source, and it looks like it raises its own 404 :psyduck:: https://bitbucket.org/cogni/django-twostepauth/src/79bbf0ce3af0/twostepauth/views.py#cl-39.

I am on my phone right now, and will check it out when I get home.

EDIT: I found out what code caused the error, but I don't know how to fix it. This is what my models.py looks like:

(...)

EDIT2: I managed to find out what was missing by comparing files with the example project, and it turned out that the documentation left out some key pieces of information necessary to create the relevant profile object.

To those interested in using the app, the author had placed the post_save signals in its own signals.py and initiated it in __init__.py instead of putting the code in models.py, as the Django documentation recommends. Really weird.

ufarn fucked around with this message at 14:13 on Jan 28, 2012

Fangs404
Dec 20, 2004

I time bomb.
I've got a model that has a FileField, and I'm wondering how I can access the upload_to value for that field. I'm overriding the save_model function in the admin model to perform some extra operations, and I need to access the directory where the file is going to be saved. I'd rather not hard code it for 2 reasons: (1) in case it changes in the future in the model and (2) because the upload_to value contains %Y.

ufarn
May 30, 2009

Fangs404 posted:

I've got a model that has a FileField, and I'm wondering how I can access the upload_to value for that field. I'm overriding the save_model function in the admin model to perform some extra operations, and I need to access the directory where the file is going to be saved. I'd rather not hard code it for 2 reasons: (1) in case it changes in the future in the model and (2) because the upload_to value contains %Y.
Sounds like you need get_field.

Say your field is "avatar" in a "Profile" class:

code:
from myapp.models import Profile

Profile._meta.get_field("avatar").upload_to
Try it out and see if it works.

I use it to get my max_length values in my view for error messages.

ryo
Jan 15, 2003
Is there a way to override Django's default widgets? I want all textareas in my site to be three rows high. I know I can use field.widget.attrs.update to modify form widgets, but it would be better if I could do just set this once.
If not, does anyone know why, when I create an inlineformset_factory and pass a Form class, it ignores the formfield_callback?

for example:

model:
code:
def make_custom_field(f):
    formfield = f.formfield()
    if isinstance(f, models.DateField):
        formfield.widget.format = '%d/%m/%Y'
        formfield.widget.attrs.update({'class':'datePicker',})
    elif isinstance(f, models.TextField):
        formfield.widget.attrs.update({'rows':'3',})
    return formfield

class Wages(models.Model):
    post = models.ForeignKey(Post)
    funding = models.ForeignKey(Funding)
    start_date = models.DateField()
    end_date = models.DateField()
    percent = models.DecimalField(max_digits=4, decimal_places=2)
    notes = models.TextField(blank=True)

class WagesForm(ModelForm):
    formfield_callback = make_custom_field

    class Meta:
        model = Wages
view:
code:
def edit_wages(request,id):
    WagesFormSet = inlineformset_factory(Post, Wages, form=WagesForm, extra=1)
    post = Post.objects.get(pk=id)
    formset = WagesFormSet(instance=post)
    return render_to_response('edit_wages2.html',{'formset':formset,},
                              context_instance=RequestContext(request))
The form=WagesForm doesn't seem to be calling make_custom_field(), but it works fine when I'm just doing a normal form.

Captain Capacitor
Jan 21, 2008

The code you say?
Managed to get some work done this past weekend and got Django-nonrel, MongoDB and fanout support working. Works really well, especially for local testing or smaller deployments.

DICTATOR OF FUNK
Nov 6, 2007

aaaaaw yeeeeeah
So I think I'm missing something logically about how custom template tags work.

I have the following, random_quote.py, inside my templatetags folder.

code:
from django import template
from main.models import Testimonial
register = template.Library()

@register.inclusion_tag('testimonial.html')
def render_quote():
    try:
        quote = Testimonial.objects.order_by('?')[0]
    except:
        print "gently caress"
And right now, testimonial.html just has
pre:
{{ quote }}
in it.

My problem is that it's always blank no matter what I do.

My base.html is able to render any random crap that I put in testimonial.html so the template is loading fine, and running the query in the shell gives me the expected info from the database, but it won't display any quotes when I call the template tag.

vvv Herp derp, that fixed it. Thanks.

DICTATOR OF FUNK fucked around with this message at 07:47 on Feb 7, 2012

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
Haven't done much with inclusion tags, but if that's your code you're not actually returning a dictionary that can be used as a context. End with:

pre:
return { 'quote': quote }

Maluco Marinero fucked around with this message at 05:23 on Feb 7, 2012

nonathlon
Jul 9, 2004
And yet, somehow, now it's my fault ...
So, I'm using django-cms to roll my own website. (Which is a bit of work, but I'm porting the site from Plone, and I had some embedded apps and assorted content types which means I need and want something more than Wordpress. And djnago-cms seemed a good steppingoff point.) I'm new to django-cms but not djago itself although I've been out of the loop for about a year. Anyways, there's a few things I'd like to do, but I'm not sure about the _best_ way to do them.

(Note: posted this to the django-cms mailing list and the only reply was for me to read the template documentation. )

I'd like certain sets of pages to share the same structure. For example, each page has a title, description/abstract, main body, attachments, references and pointers to elsewhere in the site. This _seems_ to me to be something that should be done through using a common template with placeholders for the different parts, e.g.

code:
   # extract title from page and format appropriately 
   <p class="description">{% placeholder content_description with 
TextInput %}</p> 

   {% placeholder content_mainbody %} 

   <h3>References</h3> 
   {% placeholder "content_references" %} 

   <h3>Attachments</h3> 
   {% placeholder "content_attachments" with FileBrowseInput %} 

   <h3>Also see</h3> 
   {% placeholder "content_alsosee"  %} 
Awesome. However, when I consider how to handle empty sections, I get stuck. Using "or" I could automatically include "There are no attachments", but I'd prefer to just have the section not appear at all. So, with content:
code:
   <h3>Attachments</h3> 
   {% placeholder "content_attachments" with FileBrowseInput %} 
and without:
code:
   ... (nothing at all)
So what's the best way of doing this?

Second is a more general question: am I using placeholders and templates in the right way? Is this the neatest solution to produce structured content?

Yay
Aug 4, 2007
The simplest solution I can think of (as someone who spends significant time working with django CMS) to do what you're asking - display a placeholder with a heading if there's content, nothing if none - is by using the plugin context processors, but there's diminishing returns to the method, because you'll have to do it for each plugin:
Your plugin template:
code:
{% if placeholder == 'content_attachments' and plugin.first %}<h3>Attachments</h3>{% endif %}
There are probably other ways to go about it, but that's the first thing that comes to mind. I don't know why you were told to look at the Django documentation, it's absolutely irrelevant, unless you can at least get a plugin count from the template (which you probably can, either via reverse relations, or a custom templatetag, but neither is an obvious solution), and django CMS provides no such templatetag.

I have no idea what the FileBrowseInput bits signify, as I've literally never seen that syntax.

(Addendum: in my opinon, you're better off asking questions on IRC, if you have the patience to maybe idle.)

(Additional addendum: If you can stand the efficacy of it, the better solution would be to have one placeholder, and create a plugin for setting headings. This nets you a bunch of query optimisations, because you're not doing a lookup per placeholder, a lookup to get a list of plugins, and then (I think) N queries to sidecast to the actual concrete plugin instance)

Yay fucked around with this message at 21:07 on Feb 10, 2012

M.C. McMic
Nov 8, 2008

The Weight room
Is your friend
Hi, I'm dumb. I just installed Django for the first time and I'm trying to learn Python.

I installed Django, successfully edited my "settings.py" file (added sqlLite3, uncommented the admin app), uncommented the admin area in the urls.py file, and everything was working fine when I synced my db file and went to 127.0.0.1:8000.

However, now, when I go back to that same URL, it doesn't load. When I try to restart the server (python manage.py runserver), I get this:

pre:
Validating models...

0 errors found
Django version 1.3.1, using settings 'site_auth.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Error: [Errno 48] Address already in use
What am I doing wrong? It seemed to be working fine one minute and now I can't access the site anymore.

epswing
Nov 4, 2003

Soiled Meat
Sounds like you already have the server running in another console window...

Johnny Cache Hit
Oct 17, 2011

M.C. McMic posted:

Hi, I'm dumb. I just installed Django for the first time and I'm trying to learn Python.

I installed Django, successfully edited my "settings.py" file (added sqlLite3, uncommented the admin app), uncommented the admin area in the urls.py file, and everything was working fine when I synced my db file and went to 127.0.0.1:8000.

However, now, when I go back to that same URL, it doesn't load. When I try to restart the server (python manage.py runserver), I get this:

pre:
Validating models...

0 errors found
Django version 1.3.1, using settings 'site_auth.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Error: [Errno 48] Address already in use
What am I doing wrong? It seemed to be working fine one minute and now I can't access the site anymore.

Are you sure you aren't running the server in another tab? Because I do that at least once a day.

If not check netstat or your OSes variant to see if there's a hung process that's claimed that port.

M.C. McMic
Nov 8, 2008

The Weight room
Is your friend

Kim Jong III posted:

Are you sure you aren't running the server in another tab? Because I do that at least once a day.

Yep, I'm dumb. Now I need to get admin working. Apparently I configured that incorrectly.

ufarn
May 30, 2009
I get a weird database error, but only when I run a postgreSQL database, be it locally or on my DotCloud server. Everything works fine when I run it locally on MySQL.

The error happens on a fresh installation, when no thread objects have been submitted. After that, I believe it works fine.

This is a project that has worked perfectly fine until recently when this happened.

The error happens in the following view: http://dpaste.com/705363/
And this is the error: http://dpaste.com/705361/

I can't really surmise what the problem is when I try to read the error.

avidal
May 19, 2006
bawl-some

ufarn posted:

I get a weird database error, but only when I run a postgreSQL database, be it locally or on my DotCloud server. Everything works fine when I run it locally on MySQL.

The error happens on a fresh installation, when no thread objects have been submitted. After that, I believe it works fine.

This is a project that has worked perfectly fine until recently when this happened.

The error happens in the following view: http://dpaste.com/705363/
And this is the error: http://dpaste.com/705361/

I can't really surmise what the problem is when I try to read the error.

Can you paste your Thread model as well?

Fangs404
Dec 20, 2004

I time bomb.

ufarn posted:

Sounds like you need get_field.

Say your field is "avatar" in a "Profile" class:

code:
from myapp.models import Profile

Profile._meta.get_field("avatar").upload_to
Try it out and see if it works.

I use it to get my max_length values in my view for error messages.

Sorry for not responding sooner, but that worked! Thanks a lot. Now, to figure out how to get it to change %Y into the year....

[edit]
That was easy. date.today().strftime(Publication._meta.get_field('paper').upload_to) did the trick! The upload_to field for the FileField in the model Publication is defined as upload_to='publications/papers/%Y/', and using strftime() as shown correctly replaces %Y with today's year.

Fangs404 fucked around with this message at 18:01 on Feb 20, 2012

ufarn
May 30, 2009

avidal posted:

Can you paste your Thread model as well?
Of course. Here: http://dpaste.com/705797/.

2nd Rate Poster
Mar 25, 2004

i started a joke

ufarn posted:

DB Error.

Have you ran syncdb and any relevant migrations?

What happens if you run that SQL by hand?

You can use django debug toolbar to dump the SQL, if the default traceback doesn't give it all to you, the other option is to turn on query logging on your DB server.

Adbot
ADBOT LOVES YOU

hey mom its 420
May 12, 2007

Hey y'all, what do your fabric scripts for deploying look like? This is mine, and it seems awfully simple, is there something that I'm missing or is this OK as far as deployment goes? We still don't have tests so there's no testing, but I'll add those as well.

code:
from __future__ import with_statement
from fabric.api import *

env.hosts = ['django@server.com']

def deploy(rev='tip'):
    with cd('/srv/django-projects/toliman'):
        run('hg pull') #pulls from repo to production repo
        run('hg update %s' % (rev,))
        run('python manage.py migrate musicdiscipline')
        run('python manage.py collectstatic --noinput')

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