Rectangle 27 2

os.environ.setdefault only sets the value if it is not set. When you run in production, export the environment variable DJANGO_SETTINGS_MODULE and set it to your production/staging settings file, and you don't have to set anything when running in development (if you set it by default to your development settings). This is the DRY-est method.

The method with a local_settings.py (which is most of the times kept out of the repo!) is not best practice and should be avoided.

I definitely want all settings files in the repo. And do you agree the best approach to setting the DJANGO_SETTINGS_MODULE env variable locally is to create a postactivate hook in virtualenvwrapper?

Locally you don't need to set the environment variable, since the default should be your local settings. On the production server, I recommend using the combination supervisor + gunicorn. Set the environment variable in a script that starts the application and you never have to set it again.

If the DJANGO_SETTINGS_MODULE env variable is not set locally, how does Django know which settings file to use?

For development, the postactivate hook is cool, but other tools like honcho, foreman or envdir might help you. For production it is probably easier to set the variables using supervisord. See blog.doismellburning.co.uk/2014/10/06/

@Johan in your WSGI file it would use the production file, while using manage.py it defaults to "{{projectname}}.settings". django-admin.py would probably fail.

python - Selecting the correct settings file to use in Django - Stack ...

python django virtualenvwrapper pythonanywhere
Rectangle 27 37

Since you are in Django, you could add these lines to your settings.py:

import sys
import logging

if len(sys.argv) > 1 and sys.argv[1] == 'test':
    logging.disable(logging.CRITICAL)

That way you don't have to add that line in every setUp() on your tests. :)

You could also do a couple of handy changes for your test needs this way.

There is another "nicer" or "cleaner" way to add specifics to your tests and that is making your own test runner.

Just create a class like this:

import logging

from django.test.simple import DjangoTestSuiteRunner
from django.conf import settings

class MyOwnTestRunner(DjangoTestSuiteRunner):
    def run_tests(self, test_labels, extra_tests=None, **kwargs):

        # Don't show logging messages while testing
        logging.disable(logging.CRITICAL)

        return super(MyOwnTestRunner, self).run_tests(test_labels, extra_tests, **kwargs)
TEST_RUNNER = "PATH.TO.PYFILE.MyOwnTestRunner"
#(for example, 'utils.mytest_runner.MyOwnTestRunner')

This lets you do one really handy modification that the other approach doesn't, which is to make Django just tests the applications that you want. You can do that by changing the test_labels adding this line to the test runner:

if not test_labels:
    test_labels = ['my_app1', 'my_app2', ...]

Sure - putting it in settings.py would make it global.

Sometimes in unit tests, I want to assert that an error was logged thus this method is not ideal. Still, it is a good answer.

How can I disable logging while running unit tests in Python Django? -...

python django unit-testing logging
Rectangle 27 36

Here's a Django view to do this:

import os
import zipfile
import StringIO

from django.http import HttpResponse


def getfiles(request):
    # Files (local path) to put in the .zip
    # FIXME: Change this (get paths from DB etc)
    filenames = ["/tmp/file1.txt", "/tmp/file2.txt"]

    # Folder name in ZIP archive which contains the above files
    # E.g [thearchive.zip]/somefiles/file2.txt
    # FIXME: Set this to something better
    zip_subdir = "somefiles"
    zip_filename = "%s.zip" % zip_subdir

    # Open StringIO to grab in-memory ZIP contents
    s = StringIO.StringIO()

    # The zip compressor
    zf = zipfile.ZipFile(s, "w")

    for fpath in filenames:
        # Calculate path for file in zip
        fdir, fname = os.path.split(fpath)
        zip_path = os.path.join(zip_subdir, fname)

        # Add file, at correct path
        zf.write(fpath, zip_path)

    # Must close zip for all contents to be written
    zf.close()

    # Grab ZIP file from in-memory, make response with correct MIME-type
    resp = HttpResponse(s.getvalue(), mimetype = "application/x-zip-compressed")
    # ..and correct content-disposition
    resp['Content-Disposition'] = 'attachment; filename=%s' % zip_filename

    return resp

Not needed in this example, but in general make sure the filename in the content-disposition header is quoted and escaped as needed. For example, if there's a space in the filename, most browsers will only use the part up to the space for the filename (e.g. attachment; filename=Test File.zip gets saved as Test.)

@MikeDeSimone Good point. Is there a good method to escape the filename for such a context?

for django Version > 1.7 use content_type instead of mimetype

b = BytesIO.BytesIO()

python - Serving dynamically generated ZIP archives in Django - Stack ...

python django
Rectangle 27 36

Here's a Django view to do this:

import os
import zipfile
import StringIO

from django.http import HttpResponse


def getfiles(request):
    # Files (local path) to put in the .zip
    # FIXME: Change this (get paths from DB etc)
    filenames = ["/tmp/file1.txt", "/tmp/file2.txt"]

    # Folder name in ZIP archive which contains the above files
    # E.g [thearchive.zip]/somefiles/file2.txt
    # FIXME: Set this to something better
    zip_subdir = "somefiles"
    zip_filename = "%s.zip" % zip_subdir

    # Open StringIO to grab in-memory ZIP contents
    s = StringIO.StringIO()

    # The zip compressor
    zf = zipfile.ZipFile(s, "w")

    for fpath in filenames:
        # Calculate path for file in zip
        fdir, fname = os.path.split(fpath)
        zip_path = os.path.join(zip_subdir, fname)

        # Add file, at correct path
        zf.write(fpath, zip_path)

    # Must close zip for all contents to be written
    zf.close()

    # Grab ZIP file from in-memory, make response with correct MIME-type
    resp = HttpResponse(s.getvalue(), mimetype = "application/x-zip-compressed")
    # ..and correct content-disposition
    resp['Content-Disposition'] = 'attachment; filename=%s' % zip_filename

    return resp

Not needed in this example, but in general make sure the filename in the content-disposition header is quoted and escaped as needed. For example, if there's a space in the filename, most browsers will only use the part up to the space for the filename (e.g. attachment; filename=Test File.zip gets saved as Test.)

@MikeDeSimone Good point. Is there a good method to escape the filename for such a context?

for django Version > 1.7 use content_type instead of mimetype

b = BytesIO.BytesIO()

python - Serving dynamically generated ZIP archives in Django - Stack ...

python django
Rectangle 27 155

Follow these steps to change an app's name in Django:

  • Rename the folder which is in your project root
  • Change any references to your app in their dependencies, i.e. the app's views.py, urls.py , 'manage.py' , and settings.py files.
django_content_type
UPDATE django_content_type SET app_label='<NewAppName>' WHERE app_label='<OldAppName>'
  • Also if you have models, you will have to rename the model tables. For postgres use ALTER TABLE <oldAppName>_modelName RENAME TO <newAppName>_modelName. For mysql too I think it is the same (as mentioned by @null_radix)
  • (For Django >= 1.7) Update the django_migrations table to avoid having your previous migrations re-run: UPDATE django_migrations SET app='<NewAppName>' WHERE app='<OldAppName>'. Note: there is some debate (in comments) if this step is required for Django 1.8+; If someone knows for sure please update here.
  • If your models.py 's Meta Class has app_name listed, make sure to rename that too (mentioned by @will).
static
templates
old_app/static/old_app
new_app/static/new_app
models
django_content_type.name
UPDATE django_content_type SET name='<newModelName>' where name='<oldModelName>' AND app_label='<OldAppName>'

Meta point (If using virtualenv): Worth noting, if you are renaming the directory that contains your virtualenv, there will likely be several files in your env that contain an absolute path and will also need to be updated. If you are getting errors such as ImportError: No module named ... this might be the culprit. (thanks to @danyamachine for providing this).

Other references: you might also want to refer the below links for a more complete picture

ALTER TABLE <oldAppName>_modelName RENAME TO <newAppName>_modelName

If your Models.py's Meta Class has 'app_name' listed, make sure to rename that too

And if you're using the new migrations, you'll need to change the app name in the existing migrations files and django_migrations table. It might be better to squash migrations first so there's less to edit.

For Postgres, If you want to rename the sequence too, then use ALTER SEQUENCE <oldAppName>_<modelName>_<PK>_seq RENAME TO <newAppName>_<modelName>_<PK>_seq;. Although it is not necessary, the system itself doesn't care about the name. The column DEFAULT stores an OID ('foo_pkey_seq'::regclass), you can change the name of the sequence without breaking that - the OID stays the same.

Would be great if you updated your answer to include what James said about upgrading migration files (such as dependency module names)... couldn't figure that out for quite a bit.

python - How to change the name of a Django app? - Stack Overflow

python django
Rectangle 27 629

null=True sets NULL (versus NOT NULL) on the column in your DB. Blank values for Django field types such as DateTimeField or ForeignKey will be stored as NULL in the DB.

blank=True determines whether the field will be required in forms. This includes the admin and your own custom forms. If blank=True then the field will not be required, whereas if it's False the field cannot be blank.

The combo of the two is so frequent because typically if you're going to allow a field to be blank in your form, you're going to also need your database to allow NULL values for that field. The exception is CharFields and TextFields, which in Django are never saved as NULL. Blank values are stored in the DB as an empty string ('').

models.DateTimeField(blank=True) # raises IntegrityError if blank

models.DateTimeField(null=True) # NULL allowed, but must be filled out in a form

Obviously those two options don't make logical sense to use (though, there might be a use case for null=True, blank=False if you want a field to always be required in forms, but optional when dealing with an object through something like the shell.)

models.CharField(blank=True) # No problem, blank is stored as ''

models.CharField(null=True) # NULL allowed, but will never be set as NULL

CHAR and TEXT types are never saved as NULL by Django, so null=True is unnecessary. However, you can manually set one of these fields to None to force set it as NULL. If you have a scenario where that might be necessary, you should still include null=True.

IntegrityError is raised when Django attempts to save the record to the database. The field is not required to be filled in by the user, and that's the problem because at the database level it's not null.

In your explanation, models.DateTimeField(blank=True) you say that if this DateTimeField is left blank, an IntegrityError is raised. But I thought that blank=True means that the field doesn't have to be filled in by the user, so why the IntegrityError?

I think user798719 is referring to the value of blank, which should be False instead of True in your example: models.DateTimeField(blank=False) # raises IntegrityError if blank

No, Chris is trying to point out why having blank=True without having null=True would cause problems in a DateTimeField.

NOTE to Oracle users: It is not true that "CHAR and TEXT are NEVER saved as NULL by Django". It is true for most backends, but Oracle will force an empty string to NULL, so the Django Oracle backend is an exception to the above statement Django Docs

@ChrisPratt Minor correction to your post: CharFields can get saved as NULL in the database (translating to None in Python) if you set null=True. The docs even say to avoid setting null=True because it allows two different kinds of "blanky" values. I just tested this behaviour with Django 1.8/MySQL 5.6

Note that you may blank=True for a field entered by the system.

python - differentiate null=True, blank=True in django - Stack Overflo...

python django django-models
Rectangle 27 53

Go to your Django project home directory and do:

./manage.py --version

Upvoted for being SHORT and SIMPLE answer.

python - How to check Django version - Stack Overflow

python django
Rectangle 27 53

Go to your Django project home directory and do:

./manage.py --version

Upvoted for being SHORT and SIMPLE answer.

python - How to check Django version - Stack Overflow

python django
Rectangle 27 18

I like Hassek's custom test runner idea. It should be noted that DjangoTestSuiteRunner is no longer the default test runner in Django 1.6+, it has been replaced by the DiscoverRunner. For default behaviour, the test runner should be more like:

import logging

from django.test.runner import DiscoverRunner

class NoLoggingTestRunner(DiscoverRunner):
    def run_tests(self, test_labels, extra_tests=None, **kwargs):

        # disable logging below CRITICAL while testing
        logging.disable(logging.CRITICAL)

        return super(NoLoggingTestRunner, self).run_tests(test_labels, extra_tests, **kwargs)

I found your solution after trying a lot of things. However I am not able to set the variable TEST_RUNNER in settings as its not able to import the module where the test_runner file is.

Sounds like an import issue. Are you setting TEST_RUNNER to a string path to the runner (not the actual Python module)? Also, where is your runner located? I have mine in a separate app named helpers, which only has utils that don't import from anywhere else within the project.

How can I disable logging while running unit tests in Python Django? -...

python django unit-testing logging
Rectangle 27 91

I know this is an old one, but it's the top Google result for "django 401", so I thought I'd point this out...

Assuming you've already imported django.http.HttpResponse, you can do it in a single line:

return HttpResponse('Unauthorized', status=401)

The 'Unauthorized' string is optional. Easy.

This is passable and I upvoted it; however - for a similar scenario I would consider using error 403 (Forbidden) instead - for that there is a ready mechanism: docs.djangoproject.com/en/dev/topics/http/views/ - just define handler403 in root urls.py, and then raise django.core.exceptions.PermissionDenied.

@TomaszGandor Yep, or return HttpResponseForbidden works for 403s too (raising an exception as youve suggested it often more convenient). 401 and 403 arent quite the same though, although some argue over the exact difference.

According to Wikipedia, the 401 response must include a WWW-Authenticate header field. So, it's simpler to use 403. en.wikipedia.org/wiki/List_of_HTTP_status_codes

@SimonSteinberger Very true, but there isnt a standard WWW-Authenticate header that any systems will recognise for most auth systems today, so unless youre using Basic Auth Id argue this is a pragmatic case to violate the spec. IMO 403 has a separate purpose (i.e. youve authenticated but youre not allowed to access this [403] vs you havent authenticated yet [401])

python - How do I return a 401 Unauthorized in Django? - Stack Overflo...

python django
Rectangle 27 590

"What are the largest sites built on Django today?"

There isn't any single place that collects information about traffic on Django built sites, so I'll have to take a stab at it using data from various locations. First, we have a list of Django sites on the front page of the main Django project page and then a list of Django built sites at djangosites.org. Going through the lists and picking some that I know have decent traffic we see:

  • "Can Django deal with 100,000 users daily, each visiting the site for a couple of hours?" Yes, see above.

"Could a site like Stack Overflow run on Django?"

My gut feeling is yes but, as others answered and Mike Malone mentions in his presentation, database design is critical. Strong proof might also be found at www.cnprog.com if we can find any reliable traffic stats. Anyway, it's not just something that will happen by throwing together a bunch of Django models :)

There are, of course, many more sites and bloggers of interest, but I have got to stop somewhere!

I seem to remember curse going through an acquisition about 6 months ago - that might have influenced their decision to change technologies. I wouldn't put it down to any inadequacy in django.

Instangram also uses Django. Another site would be BringIt.com

python - Does Django scale? - Stack Overflow

python django web-applications scalability
Rectangle 27 590

"What are the largest sites built on Django today?"

There isn't any single place that collects information about traffic on Django built sites, so I'll have to take a stab at it using data from various locations. First, we have a list of Django sites on the front page of the main Django project page and then a list of Django built sites at djangosites.org. Going through the lists and picking some that I know have decent traffic we see:

  • "Can Django deal with 100,000 users daily, each visiting the site for a couple of hours?" Yes, see above.

"Could a site like Stack Overflow run on Django?"

My gut feeling is yes but, as others answered and Mike Malone mentions in his presentation, database design is critical. Strong proof might also be found at www.cnprog.com if we can find any reliable traffic stats. Anyway, it's not just something that will happen by throwing together a bunch of Django models :)

There are, of course, many more sites and bloggers of interest, but I have got to stop somewhere!

I seem to remember curse going through an acquisition about 6 months ago - that might have influenced their decision to change technologies. I wouldn't put it down to any inadequacy in django.

Instangram also uses Django. Another site would be BringIt.com

python - Does Django scale? - Stack Overflow

python django web-applications scalability
Rectangle 27 590

"What are the largest sites built on Django today?"

There isn't any single place that collects information about traffic on Django built sites, so I'll have to take a stab at it using data from various locations. First, we have a list of Django sites on the front page of the main Django project page and then a list of Django built sites at djangosites.org. Going through the lists and picking some that I know have decent traffic we see:

  • "Can Django deal with 100,000 users daily, each visiting the site for a couple of hours?" Yes, see above.

"Could a site like Stack Overflow run on Django?"

My gut feeling is yes but, as others answered and Mike Malone mentions in his presentation, database design is critical. Strong proof might also be found at www.cnprog.com if we can find any reliable traffic stats. Anyway, it's not just something that will happen by throwing together a bunch of Django models :)

There are, of course, many more sites and bloggers of interest, but I have got to stop somewhere!

I seem to remember curse going through an acquisition about 6 months ago - that might have influenced their decision to change technologies. I wouldn't put it down to any inadequacy in django.

Instangram also uses Django. Another site would be BringIt.com

python - Does Django scale? - Stack Overflow

python django web-applications scalability
Rectangle 27 9

One of the common causes of circular imports in Django is using foreign keys in modules that reference each other. Django provides a way to circumvent this by explicitly specifying a model as a string with the full application label:

class MyModel(models.Model):
    myfk = models.ForeignKey(
        'myapp.MyAppModel',  # avoid circular imports
        null=True)

If I could boost this by 10 points I would. This is a HUGE life saver.

Tool for pinpointing circular imports in Python/Django? - Stack Overfl...

python django import
Rectangle 27 238

Two Scoops of Django: Best Practices for Django 1.5 suggests using version control for your settings files and storing the files in a separate directory:

project/
    app1/
    app2/
    project/
        __init__.py
        settings/
            __init__.py
            base.py
            local.py
            production.py
    manage.py

The base.py file contains common settings (such as MEDIA_ROOT or ADMIN), while local.py and production.py have site-specific settings:

settings/base.py
INSTALLED_APPS = (
    # common apps...
)

In the local development settings file settings/local.py:

In the file production settings file settings/production.py:

from project.settings.base import *

DEBUG = False
INSTALLED_APPS += (
    # other apps for production site
)

Then when you run django, you add the --settings option:

# Running django for local development
$ ./manage.py runserver 0:8000 --settings=project.settings.local

# Running django shell on the production site
$ ./manage.py shell --settings=project.settings.production

Note that instead of using --settings every time, you could set the DJANGO_SETTINGS_MODULE envvar. This works nicely with, eg, Heroku: set it globally to production, then override it with dev in your .env file.

DJANGO_SETTINGS_MODULE
BASE_DIR
os.path.dirname(os.path.realpath(os.path.dirname(__file__) + "/.."))

@rsp according to the django docs, you import from django.conf import settings which is an object that abstracts the interface and decouples the code from the location of the settings, docs.djangoproject.com/en/dev/topics/settings/

If I set the DJANGO_SETTINGS_MODULE through an environmental variable, do I still need os.environ.setdefault("DJANGO_SETTINGS_MODULE", "projectname.settings.production") in my wsgi.py file? Also, I've set the environmental var using: export DJANGO_SETTINGS_MODULE=projectname.settings.local, but then it is lost when I close the terminal. What can I do to ensure it is saved? Should I add that line to the bashrc file?

python - How to manage local vs production settings in Django? - Stack...

python django deployment
Rectangle 27 238

Two Scoops of Django: Best Practices for Django 1.5 suggests using version control for your settings files and storing the files in a separate directory:

project/
    app1/
    app2/
    project/
        __init__.py
        settings/
            __init__.py
            base.py
            local.py
            production.py
    manage.py

The base.py file contains common settings (such as MEDIA_ROOT or ADMIN), while local.py and production.py have site-specific settings:

settings/base.py
INSTALLED_APPS = (
    # common apps...
)

In the local development settings file settings/local.py:

In the file production settings file settings/production.py:

from project.settings.base import *

DEBUG = False
INSTALLED_APPS += (
    # other apps for production site
)

Then when you run django, you add the --settings option:

# Running django for local development
$ ./manage.py runserver 0:8000 --settings=project.settings.local

# Running django shell on the production site
$ ./manage.py shell --settings=project.settings.production

Note that instead of using --settings every time, you could set the DJANGO_SETTINGS_MODULE envvar. This works nicely with, eg, Heroku: set it globally to production, then override it with dev in your .env file.

DJANGO_SETTINGS_MODULE
BASE_DIR
os.path.dirname(os.path.realpath(os.path.dirname(__file__) + "/.."))

@rsp according to the django docs, you import from django.conf import settings which is an object that abstracts the interface and decouples the code from the location of the settings, docs.djangoproject.com/en/dev/topics/settings/

If I set the DJANGO_SETTINGS_MODULE through an environmental variable, do I still need os.environ.setdefault("DJANGO_SETTINGS_MODULE", "projectname.settings.production") in my wsgi.py file? Also, I've set the environmental var using: export DJANGO_SETTINGS_MODULE=projectname.settings.local, but then it is lost when I close the terminal. What can I do to ensure it is saved? Should I add that line to the bashrc file?

python - How to manage local vs production settings in Django? - Stack...

python django deployment
Rectangle 27 586

"What are the largest sites built on Django today?"

There isn't any single place that collects information about traffic on Django built sites, so I'll have to take a stab at it using data from various locations. First, we have a list of Django sites on the front page of the main Django project page and then a list of Django built sites at djangosites.org. Going through the lists and picking some that I know have decent traffic we see:

  • "Can Django deal with 100,000 users daily, each visiting the site for a couple of hours?" Yes, see above.

"Could a site like Stack Overflow run on Django?"

My gut feeling is yes but, as others answered and Mike Malone mentions in his presentation, database design is critical. Strong proof might also be found at www.cnprog.com if we can find any reliable traffic stats. Anyway, it's not just something that will happen by throwing together a bunch of Django models :)

There are, of course, many more sites and bloggers of interest, but I have got to stop somewhere!

I seem to remember curse going through an acquisition about 6 months ago - that might have influenced their decision to change technologies. I wouldn't put it down to any inadequacy in django.

Instangram also uses Django. Another site would be BringIt.com

python - Does Django scale? - Stack Overflow

python django web-applications scalability
Rectangle 27 237

Two Scoops of Django: Best Practices for Django 1.5 suggests using version control for your settings files and storing the files in a separate directory:

project/
    app1/
    app2/
    project/
        __init__.py
        settings/
            __init__.py
            base.py
            local.py
            production.py
    manage.py

The base.py file contains common settings (such as MEDIA_ROOT or ADMIN), while local.py and production.py have site-specific settings:

settings/base.py
INSTALLED_APPS = (
    # common apps...
)

In the local development settings file settings/local.py:

In the file production settings file settings/production.py:

from project.settings.base import *

DEBUG = False
INSTALLED_APPS += (
    # other apps for production site
)

Then when you run django, you add the --settings option:

# Running django for local development
$ ./manage.py runserver 0:8000 --settings=project.settings.local

# Running django shell on the production site
$ ./manage.py shell --settings=project.settings.production

Note that instead of using --settings every time, you could set the DJANGO_SETTINGS_MODULE envvar. This works nicely with, eg, Heroku: set it globally to production, then override it with dev in your .env file.

DJANGO_SETTINGS_MODULE
BASE_DIR
os.path.dirname(os.path.realpath(os.path.dirname(__file__) + "/.."))

@rsp according to the django docs, you import from django.conf import settings which is an object that abstracts the interface and decouples the code from the location of the settings, docs.djangoproject.com/en/dev/topics/settings/

If I set the DJANGO_SETTINGS_MODULE through an environmental variable, do I still need os.environ.setdefault("DJANGO_SETTINGS_MODULE", "projectname.settings.production") in my wsgi.py file? Also, I've set the environmental var using: export DJANGO_SETTINGS_MODULE=projectname.settings.local, but then it is lost when I close the terminal. What can I do to ensure it is saved? Should I add that line to the bashrc file?

python - How to manage local vs production settings in Django? - Stack...

python django deployment
Rectangle 27 575

"What are the largest sites built on Django today?"

There isn't any single place that collects information about traffic on Django built sites, so I'll have to take a stab at it using data from various locations. First, we have a list of Django sites on the front page of the main Django project page and then a list of Django built sites at djangosites.org. Going through the lists and picking some that I know have decent traffic we see:

  • "Can Django deal with 100,000 users daily, each visiting the site for a couple of hours?" Yes, see above.

"Could a site like Stack Overflow run on Django?"

My gut feeling is yes but, as others answered and Mike Malone mentions in his presentation, database design is critical. Strong proof might also be found at www.cnprog.com if we can find any reliable traffic stats. Anyway, it's not just something that will happen by throwing together a bunch of Django models :)

There are, of course, many more sites and bloggers of interest, but I have got to stop somewhere!

I seem to remember curse going through an acquisition about 6 months ago - that might have influenced their decision to change technologies. I wouldn't put it down to any inadequacy in django.

Instangram also uses Django. Another site would be BringIt.com

python - Does Django scale? - Stack Overflow

python django web-applications scalability
Rectangle 27 13

In my case I had to go to Settings (Alt+F7, or the Spanner/Cog icon) and select a Python interpreter which has Django installed alongside it.

If Django isn't already installed, you should be able to install it via the green + icon (not shown below) once an interpreter has been selected.

python - Pycharm error Django is not importable in this environment - ...

python django pycharm