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
Hanpan
Dec 5, 2004

mwarkentin posted:

I've been using Heroku. Very cool service.

See, my thing with heroku is that I have to host several sites (about 12 django sites in all) and it seems heroku is geared more towards hosting larger, singular apps. I'm sure that isn't the case, but it's super tough finding decent documentation on how to manage multiple sites on one instance.

Adbot
ADBOT LOVES YOU

Pardot
Jul 25, 2001




Hanpan posted:

See, my thing with heroku is that I have to host several sites (about 12 django sites in all) and it seems heroku is geared more towards hosting larger, singular apps. I'm sure that isn't the case, but it's super tough finding decent documentation on how to manage multiple sites on one instance.

I'm not really sure what you're asking. Is this 12 sites with the same code or 12 separate apps? You can totally have 12 apps no problem. I have 65 right now ;)

If it's 12 sites with the same code, check out heroku domains:add. Also the support guys are really great and can answer a lot of questions, but I'm happy to help here as well.

And if anyone wants into our beta program drop me a PM.

Comrade Gritty
Sep 19, 2011

This Machine Kills Fascists
I use Gondor for all my sites and have been extremely happy with them.

mwarkentin
Oct 26, 2004
Yeah, you can create as many Heroku instances as you want.. and for each one, the first process is free..

Lister_of_smeg
May 25, 2005
I'm having some trouble with the Django test client, almost certainly down to a lack of understanding on my part somewhere. My google fu hasn't been strong enough to help, so hopefully one of you guys can point me in the right direction.

I've built a REST interface to a Django app and am now writing tests for it. I've hit a wall trying to get JSON data into my view code. The first thing I tried was to pass the data into the test client already json encoded.

code:
c = Client()
final_url = "%s/" % self.claim_base_url

resp = c.put(final_url, simplejson.dumps( { "resource_id" : self.test_res_abc.id,
                                      "site_id" : self.site_a.id }) )
This produces the output:

code:
Traceback (most recent call last):
  File "/data/user/p4/depot/webdev/main/django/web/../web/arc/global_license_manager/tests.py", line 68, in testClaim
    "site_id" : self.site_a.id }) )
  File "/tools/django/trunk/1.3.1/django/test/client.py", line 477, in put
    response = super(Client, self).put(path, data=data, content_type=content_type, **extra)
  File "/tools/django/trunk/1.3.1/django/test/client.py", line 292, in put
    put_data = self._encode_data(data, content_type)
  File "/tools/django/trunk/1.3.1/django/test/client.py", line 212, in _encode_data
    return encode_multipart(BOUNDARY, data)
  File "/tools/django/trunk/1.3.1/django/test/client.py", line 119, in encode_multipart
    for (key, value) in data.items():
AttributeError: 'str' object has no attribute 'items'
Ok, so it looks like the put method wants a raw data structure, not one that's already been JSON encoded. So I modify the put call to:

code:
resp = c.put(final_url, { "resource_id" : self.test_res_abc.id,
                                      "site_id" : self.site_a.id })

Now I get the following error:

code:
Traceback (most recent call last):
  File "/data/user/p4/depot/webdev/main/django/web/../web/arc/global_license_manager/tests.py", line 68, in testClaim
    "site_id" : self.site_a.id })
  File "/tools/django/trunk/1.3.1/django/test/client.py", line 477, in put
    response = super(Client, self).put(path, data=data, content_type=content_type, **extra)
  File "/tools/django/trunk/1.3.1/django/test/client.py", line 304, in put
    return self.request(**r)
  File "/tools/django/trunk/1.3.1/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/data/user/p4/depot/webdev/main/django/web/util/restresource.py", line 25, in __call__
    return callback(request, *args, **kwargs)
  File "/data/user/p4/depot/webdev/main/django/web/arc/global_license_manager/api.py", line 136, in PUT
    request_data = simplejson.loads(request.raw_post_data)
  File "build/bdist.linux-x86_64/egg/simplejson/__init__.py", line 384, in loads
    return _default_decoder.decode(s)
  File "build/bdist.linux-x86_64/egg/simplejson/decoder.py", line 402, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "build/bdist.linux-x86_64/egg/simplejson/decoder.py", line 420, in raw_decode
    raise JSONDecodeError("No JSON object could be decoded", s, idx)
JSONDecodeError: No JSON object could be decoded: line 1 column 0 (char 0)
So now my view code is being called, but the JSON decode is barfing because there are no JSON objects in the data.

Does anyone have any advice for how to solve this?

Thanks!

mwarkentin
Oct 26, 2004
Do you need to set content-type to application/json?

See the third arg here: https://docs.djangoproject.com/en/1.3/topics/testing/#django.test.client.Client.post

Lister_of_smeg
May 25, 2005
Added content_type="application/json" and still getting the same error messages, with and without the simplejson.dumps.

Hanpan
Dec 5, 2004

So I've been spending some time investigating each of the Django compatible PaaS that have been recommended in this thread. It's crazy how many there are now, I'm so glad to see Django finally getting the attention it deserves.

After tinkering around, I decided that DotCloud works the best for me, although it was a close call between that and Gondor. Thanks to everyone in this thread who made some suggestions. In the interests of 'giving back', I was going to write up a step by step deployment guide using DotCloud and CloudFront for static files. If anyone thinks they can gleam anything from it, feel free to drop me a PM and I'll share.

ufarn
May 30, 2009
DotCloud's support also owns bones. The even have a StackExchange-like knowledge base. (Although I don't know if it's any good.)

The main argument against DotCloud is that Windows is basically unsupported, which seems ridiculous.

ufarn fucked around with this message at 00:15 on Mar 9, 2012

raymond
Mar 20, 2004

Lister_of_smeg posted:

So now my view code is being called, but the JSON decode is barfing because there are no JSON objects in the data.

Does anyone have any advice for how to solve this?

Thanks!

It looks like Client put/post methods will URL encode your data dictionary no matter what content type you use. The request.raw_post_data you are getting in your view will be something like "resource_id=123&site_id=1" which isn't a JSON string.

You might be able to use this (entirely untested) Client class to make your test work:
code:
from django.test.client import Client

class JsonClient(Client):
    """A test client that converts data into JSON instead of URL encoding it."""

    def _encode_data(self, data, content_type):
        assert content_type == 'application/json'
        return simplejson.dumps(data)

DICTATOR OF FUNK
Nov 6, 2007

aaaaaw yeeeeeah
Okay Django thread, time for me to ask a question without being dumb and gay (hopefully):

I just started using django-registration, and while it's pretty great, I can't seem to figure out why anything having to do with password management renders using the Django admin templates.

For instance, on the password reset form (registration/password_reset_form.html), it claims to extend my base.html and yada yada, yet... it doesn't. In fact, no changes I make to the template seem to apply. Seeing as django-registration doesn't come with any default templates, I have no idea where the hell it's rendering these forms from.

All of the other templates (log in/out, registration form, etc) seem to render fine.

Uhm, why?

:wtf:

ufarn
May 30, 2009

root beer posted:

Okay Django thread, time for me to ask a question without being dumb and gay (hopefully):

I just started using django-registration, and while it's pretty great, I can't seem to figure out why anything having to do with password management renders using the Django admin templates.

For instance, on the password reset form (registration/password_reset_form.html), it claims to extend my base.html and yada yada, yet... it doesn't. In fact, no changes I make to the template seem to apply. Seeing as django-registration doesn't come with any default templates, I have no idea where the hell it's rendering these forms from.

All of the other templates (log in/out, registration form, etc) seem to render fine.

Uhm, why?

:wtf:
Download django-registration-defaults and use that as your default templates, until you modify them. Much, much easier than the pain in the rear end of figuring out django-registration.

205b
Mar 25, 2007

This is only semi-related to Django, but I'm trying to move an app from SQLite to PostgreSQL, and I can't get Django to connect to the local database. I've created a database and user like so:
code:
$ createdb projectname
$ createuser -P projectname
and my settings.py contains the following (where password is the actual password):
code:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'projectname',           # Or path to database file if using sqlite3.
        'USER': 'projectname',           # Not used with sqlite3.
        'PASSWORD': 'pass',              # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}
Every time I try to run syncdb, it throws the following exception:
code:
psycopg2.OperationalError: could not connect to server: Permission denied
	Is the server running locally and accepting
	connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I have very little database experience and no prior exposure to postgres, so I'm assuming that I completely screwed up at some point. Does anything look obviously wrong to you guys?

ufarn
May 30, 2009

ninepints posted:

This is only semi-related to Django, but I'm trying to move an app from SQLite to PostgreSQL, and I can't get Django to connect to the local database. I've created a database and user like so:
code:
$ createdb projectname
$ createuser -P projectname
and my settings.py contains the following (where password is the actual password):
code:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'projectname',           # Or path to database file if using sqlite3.
        'USER': 'projectname',           # Not used with sqlite3.
        'PASSWORD': 'pass',              # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}
Every time I try to run syncdb, it throws the following exception:
code:
psycopg2.OperationalError: could not connect to server: Permission denied
	Is the server running locally and accepting
	connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I have very little database experience and no prior exposure to postgres, so I'm assuming that I completely screwed up at some point. Does anything look obviously wrong to you guys?
You need to define HOST as 'localhost' with postgreSQL for it to work. It confounded me as well the first time.

It may not be the solution to your error in question, but it's nevertheless something you need to do.

I believe this is the guide I originally used to wrap my head around postgreSQL on Ubuntu with Django.

ufarn fucked around with this message at 20:19 on Mar 12, 2012

epswing
Nov 4, 2003

Soiled Meat
This is really strange. Would there be any reason why just the admin account's password is reset every time I run syncdb (no changes have occurred) and restart apache? Doesn't happen for other user accounts.

:confused:

Edit: Heh, whoops. I had initial_data.json sitting in my project root, which contained said admin user.

epswing fucked around with this message at 22:03 on Mar 12, 2012

205b
Mar 25, 2007

ufarn posted:

You need to define HOST as 'localhost' with postgreSQL for it to work. It confounded me as well the first time.

It may not be the solution to your error in question, but it's nevertheless something you need to do.

I believe this is the guide I originally used to wrap my head around postgreSQL on Ubuntu with Django.

That did the trick! Thanks. There's a very misleading comment on that line...

ufarn
May 30, 2009

ninepints posted:

That did the trick! Thanks. There's a very misleading comment on that line...
Was it 'localhost' or the guide that did it?

And what's the misleading part, just so I can clarify myself, if I see someone with the same problem another time? :)

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

ufarn posted:

Was it 'localhost' or the guide that did it?

And what's the misleading part, just so I can clarify myself, if I see someone with the same problem another time? :)

code:

        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
It says to leave it empty and I would expect it to work with any of the supported databases. He had to explicitly set equal to 'localhost'.

ufarn
May 30, 2009

MEAT TREAT posted:

code:

        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
It says to leave it empty and I would expect it to work with any of the supported databases. He had to explicitly set equal to 'localhost'.
Ah, sorry, I glossed over that when I retrieved it from my bookmarks for inclusion.

Hed
Mar 31, 2004

Fun Shoe
Do you guys have any rules of thumb between where you separate your view and template logic? I am building what should be a straightforward app for time reporting and display and I feel really confident about my models. For the views, I can get it to work but it feels clunky. For example, for a given reporting period (month) I'm pulling back all of the records and for unique charge codes in a week I'm building a table (list of lists), and displaying it out. Sounds straightforward enough, but getting things into stuff that's "easy" for the template to spit out (tabular data in lists) is a real pain to do additional things with later, such as refer to attributes that I could have in the code with a simple dictionary. For example, when I want to add row-wise hourly totals or label the charge codes with descriptions from my models, the template language has to be blown up and what I write feels really odd, which usually means I'm doing it wrong.

In fact, I'm pretty sure I'm doing a poor job of it because I have considered just making weeks packaged responses in JSON and coding up javascript to control how to view the data.

Lamacq
Jun 15, 2001

Breezeblock RIP

Hed posted:

Do you guys have any rules of thumb between where you separate your view and template logic? I am building what should be a straightforward app for time reporting and display and I feel really confident about my models. For the views, I can get it to work but it feels clunky. For example, for a given reporting period (month) I'm pulling back all of the records and for unique charge codes in a week I'm building a table (list of lists), and displaying it out. Sounds straightforward enough, but getting things into stuff that's "easy" for the template to spit out (tabular data in lists) is a real pain to do additional things with later, such as refer to attributes that I could have in the code with a simple dictionary. For example, when I want to add row-wise hourly totals or label the charge codes with descriptions from my models, the template language has to be blown up and what I write feels really odd, which usually means I'm doing it wrong.

In fact, I'm pretty sure I'm doing a poor job of it because I have considered just making weeks packaged responses in JSON and coding up javascript to control how to view the data.

I run into this sort of situation too, where I feel like I'm putting too much display logic in my view function to get around the limitations of the template language. I believe the answer is simply that you need to move that code into a custom template tag.

Yay
Aug 4, 2007
My basic rule of thumb is, I think, different to most people's (at least in the code I see, both at work and in the community at large), in that the view is pretty dumb.
Its sole purpose is to get Some Data to display - that is, render a template (usually) into a response object. So if there's logic required to display that template (say, if it requires an authenticated user, or the view could receive an ajax request and load a different template), that goes in the view. Obvious things like required filtering of querysets, or raising a different status code (404, 401, etc) also go in the view. Everything else goes into the context, and leaves the template to make decisions about what to do with it.

Mostly, it keeps views and templates easy to read months later, at the expense things being in multiple files.

Mind, if I'm not using classy-tags, then everything in the world is in the view, but that's usually legacy stuff.

DICTATOR OF FUNK
Nov 6, 2007

aaaaaw yeeeeeah

ufarn posted:

Download django-registration-defaults and use that as your default templates, until you modify them. Much, much easier than the pain in the rear end of figuring out django-registration.

That's what I did.

They're being overridden somehow.

vvv Nope.

DICTATOR OF FUNK fucked around with this message at 02:06 on Mar 14, 2012

ufarn
May 30, 2009

root beer posted:

That's what I did.

They're being overridden somehow.
Hmm. Did you install it through INSTALLED_APPS in settings.py instead of just copying the templates to your template folder?

Hanpan
Dec 5, 2004

So I'm having some trouble with collectstatic, here is my settings file:

code:
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'static/')
MEDIA_URL = '/static/'
STATIC_ROOT = ''
STATIC_URL = os.environ.get('STATIC_URL', 'https://s3.amazonaws.com/mybucket/')
ADMIN_MEDIA_PREFIX = '/static/admin/'

STATICFILES_DIRS = (
	os.path.join(PROJECT_DIR, 'static/'),
)
Now, what I am trying to do is collect all the static files EXCEPT the admin files, I tried this as per the documentation:

code:
python myapp/manage.py collectstatic -i admin
And a few other variations but it always seems to copy my admin files too. Anyone come across this?

avidal
May 19, 2006
bawl-some

Hanpan posted:

So I'm having some trouble with collectstatic, here is my settings file:

code:
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'static/')
MEDIA_URL = '/static/'
STATIC_ROOT = ''
STATIC_URL = os.environ.get('STATIC_URL', 'https://s3.amazonaws.com/mybucket/')
ADMIN_MEDIA_PREFIX = '/static/admin/'

STATICFILES_DIRS = (
	os.path.join(PROJECT_DIR, 'static/'),
)
Now, what I am trying to do is collect all the static files EXCEPT the admin files, I tried this as per the documentation:

code:
python myapp/manage.py collectstatic -i admin
And a few other variations but it always seems to copy my admin files too. Anyone come across this?

It seems like the `ignore` option only globs against the filename and doesn't include the path. I'm not sure if that's a bug or intentional, and I'm not sure offhand of a way to do what you want to do.

Hanpan
Dec 5, 2004

avidal posted:

It seems like the `ignore` option only globs against the filename and doesn't include the path. I'm not sure if that's a bug or intentional, and I'm not sure offhand of a way to do what you want to do.

Ahh rubbish, I hope they re-think that. It's a bit of pain having to push all the admin files with each collection, not to mention it's taking a huge chunk out of my S3 push allowance.

Mulozon Empuri
Jan 23, 2006

avidal posted:

It seems like the `ignore` option only globs against the filename and doesn't include the path. I'm not sure if that's a bug or intentional, and I'm not sure offhand of a way to do what you want to do.

Am I missing something, or should this not work for dirs as well? [edit] oh I guess I was.

django docs posted:

--ignore <pattern>
Ignore files or directories matching this glob-style pattern. Use multiple times to ignore more.

Mulozon Empuri fucked around with this message at 10:16 on Mar 22, 2012

ufarn
May 30, 2009
I am running into this error:

quote:

OperationalError at (...)

(1366, "Incorrect string value: '\xE2\x86\xA9</a...' for column 'content_html' at row 1")

Exception Type: OperationalError
Exception Value: (1366, "Incorrect string value: '\xE2\x86\xA9</a...' for column 'content_html' at row 1")

I happens when I want to save this plain text as an entry:

code:
Testing footnotes[^1].

[^1]: Note.
Using Markdown, Smartypants, and Bleach, it *should* turn into this:

code:
<p>Testing footnotes.<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup></p>

<div class="footnotes">
    <hr />
    <ol>
        <li id="fn:1"><p>Note.<a href="#fnref:1" rev="footnote">&#8617;</a></p></li>
    </ol>
</div>
But instead, I get the error.

I have found out that the error is caused by Bleach, a wrapper that sanitizes a string. It comes with this caveat:

quote:

Bleach always returns a unicode object, whether you give it a bytestring or a unicode object, but Bleach does not attempt to detect incoming character encodings, and will assume UTF-8.

If you are using a different character encoding, you should convert from a bytestring to unicode before passing the text to bleach.

The - relevant part of the - view that parses the POST request is this:

code:
    if request.method == 'POST':
        if "submit" in request.POST:
            entry.content_plain = request.POST['content']
            entry.content_html  = bleach_sd_md(entry.content_plain)
            entry.save()
            return HttpResponseRedirect(...)
And the - relevant - fields are:

code:
    content_plain = models.TextField()
    content_html  = models.TextField()
I think that this points to a general problem in the way I handle string encoding, and I obviously still would prefer to keep using Bleach. I am not the best expert in how Django handles unicode, but could someone with more experience in the subject explain what I should do to fix this encoding problem in the future?

avidal
May 19, 2006
bawl-some
It works fine for me using 1.4rc2. I'm wondering if maybe there was a fix somewhere in there. ./manage.py collectstatic -i 'admin' doesn't collect the admin files for me.

raymond
Mar 20, 2004

ufarn posted:

Unicode nightmare
These two functions solve like 95% of unicode issues:
code:
from django.utils.encoding import smart_str, smart_unicode
You can probably use smart_unicode somewhere to fix your problem - perhaps:
code:
entry.content_html = bleach_sd_md(smart_unicode(entry.content_plain))

etcetera08
Sep 11, 2008

1.4 was officially released today, whoo

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
Finally got back to working on my main project, and am getting started with class based views and mixins and stuff. Holy poo poo did I not know what I was missing. It's just so nice to work with class views.

MonkeyMaker
May 22, 2006

What's your poison, sir?

Maluco Marinero posted:

Finally got back to working on my main project, and am getting started with class based views and mixins and stuff. Holy poo poo did I not know what I was missing. It's just so nice to work with class views.

Yes, yes it is.

Hanpan
Dec 5, 2004

I really don't understand the advantage of mixins. Is there a simple reason as to why they are so much better?

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
It just feels a tonne nicer to me, and you can do a lot more without things feeling like code soup.

There's a point where procedural just gets too unwieldly with a relatively complicated view, even if you're diligently extracting functions from the main function.

Being able to save common stuff to self is a very nice option to have in the view logic.

MonkeyMaker
May 22, 2006

What's your poison, sir?

Hanpan posted:

I really don't understand the advantage of mixins. Is there a simple reason as to why they are so much better?

They stop you from repeating yourself so much. It's not really a big deal if you're building some blog site or something, but when you get into a giant system (like I'm currently building), saving time implementing the same logic over and over again is a giant time- and sanity-saver.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

MonkeyMaker posted:

They stop you from repeating yourself so much. It's not really a big deal if you're building some blog site or something, but when you get into a giant system (like I'm currently building), saving time implementing the same logic over and over again is a giant time- and sanity-saver.

Couldn't you already do that with decorators?

2nd Rate Poster
Mar 25, 2004

i started a joke
Decorators are more helpful for changing the entire function arguments or results, or for changing when a function runs.

Mixins are useful for encapsulating functionality that will be used in more than one place.

Here's a simplified example...

code:
class ProfileMixin:
   def get_user_profile(self, user):
              profile = Profile.objects.filter(user=user)
              return profile

class ProfileView(ProfileMixin, View, TemplateResponseMixin):

   def get(request, user_id):
            user = get_object_or_404(User, pk=user_id)
            profile = self.get_user_profile(user)
          
            return self.render_to_response(self.template_name, {'profile': profile, 'user': user})

Now if you used a decorator on ProileView.get, you couldn't change how profiles are pulled, without fundamentally rewriting the view. Using a mixin allows you to swap out that get_profile logic as the need arises.

To continue with this example... Let's say a user notices the bug in get_user_profile where we return a queryset of profiles instead of an individual user profile. Instead of having to make change to methods across all our classes that get a user profiles, we can just change the method definition on the ProfileMixin. That's the usefulness of a Mixin.

2nd Rate Poster fucked around with this message at 03:14 on Mar 31, 2012

Adbot
ADBOT LOVES YOU

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Why not a function? You aren't using self anywhere in your mixin, so I don't see why you have it as a mixin...

code:
def get_user_profile(user):
    return Profile.objects.filter(user=user)
And even then, if you're using a OneToOneField, you could just say user.profile, no?

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