Questions tagged [django]

Django is an open-source server-side web application framework written in Python. It is designed to reduce the effort required to create complex data-driven websites and web applications, with a special focus on less code, no-redundancy and being more explicit than implicit.

PyPI

Django: The Web framework for perfectionists with deadlines

Django is a high-level web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers and maintained by the non-profit Django Software Foundation (DSF). it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.

Django follows the (model-view-controller) architectural pattern. This consists of:

  • an object-relational mapper that mediates between data models
    (Python classes) and a relational database ("Model")

  • a system for processing requests with a web templating system ("View")

  • a regular-expression-based URL dispatcher ("Controller")

The team prefers to think of Django as an "MTV" framework, where 'M' is the model, 'T' is the template and 'V' is view.

In , 'views' describe which data are presented while 'templates' describe how that data is presented.

Resources

Useful books

Tutorials

Newsletters

Podcasts

Popular Third Party Apps

Websites Using Django

See also:

309060 questions
426
votes
6 answers

How to perform OR condition in django queryset?

I want to write a Django query equivalent to this SQL query: SELECT * from user where income >= 5000 or income is NULL. How to construct the Django queryset filter? User.objects.filter(income__gte=5000, income=0) This doesn't work, because it ANDs…
Elisa
  • 6,865
  • 11
  • 41
  • 56
412
votes
15 answers

Can "list_display" in a Django ModelAdmin display attributes of ForeignKey fields?

I have a Person model that has a foreign key relationship to Book, which has a number of fields, but I'm most concerned about author (a standard CharField). With that being said, in my PersonAdmin model, I'd like to display book.author using…
Huuuze
  • 15,528
  • 25
  • 72
  • 91
408
votes
5 answers

Getting the SQL from a Django QuerySet

How do I get the SQL that Django will use on the database from a QuerySet object? I'm trying to debug some strange behavior, but I'm not sure what queries are going to the database.
exupero
  • 9,136
  • 8
  • 47
  • 63
406
votes
5 answers

What's the difference between CharField and TextField in Django?

The documentation says that CharField() should be used for smaller strings and TextField() should be used for larger strings. Okay, but where is the line drawn between "small" and "large"? What's going on under the hood here that makes this the…
Jonathan Gleason
  • 4,567
  • 3
  • 17
  • 18
403
votes
15 answers

(13: Permission denied) while connecting to upstream:[nginx]

I am working with configuring Django project with Nginx and Gunicorn. While I am accessing my port gunicorn mysite.wsgi:application --bind=127.0.0.1:8001 in Nginx server, I am getting the following error in my error log file; 2014/05/30 11:59:42…
Mulagala
  • 8,231
  • 11
  • 29
  • 48
397
votes
8 answers

Is it bad to have my virtualenv directory inside my git repository?

I'm thinking about putting the virtualenv for a Django web app I am making inside my git repository for the app. It seems like an easy way to keep deploy's simple and easy. Is there any reason why I shouldn't do this?
Lyle Pratt
  • 5,636
  • 4
  • 27
  • 28
395
votes
10 answers

Find object in list that has attribute equal to some value (that meets any condition)

I've got a list of objects. I want to find one (first or whatever) object in this list that has an attribute (or method result - whatever) equal to value. What's the best way to find it? Here's a test case: class Test: def __init__(self,…
seler
  • 8,803
  • 4
  • 41
  • 54
391
votes
16 answers

How to get the current URL within a Django template?

I was wondering how to get the current URL within a template. Say my current URL is: .../user/profile/ How do I return this to the template?
dotty
  • 40,405
  • 66
  • 150
  • 195
387
votes
24 answers

How do I get the object if it exists, or None if it does not exist in Django?

When I ask the model manager to get an object, it raises DoesNotExist when there is no matching object. go = Content.objects.get(name="baby") Instead of DoesNotExist, how can I have go be None instead?
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
383
votes
10 answers

Django datetime issues (default=datetime.now())

I have the below db model: from datetime import datetime class TermPayment(models.Model): # I have excluded fields that are irrelevant to the question date = models.DateTimeField(default=datetime.now(), blank=True) I add a new instance…
Shamanu4
  • 5,296
  • 2
  • 27
  • 31
382
votes
7 answers

Django - iterate number in for loop of a template

I have the following for loop in my django template displaying days. I wonder, whether it's possible to iterate a number (in the below case i) in a loop. Or do I have to store it in the database and then query it in form of days.day_number? {% for…
orschiro
  • 19,847
  • 19
  • 64
  • 95
381
votes
14 answers

How do I get user IP address in Django?

How do I get user's IP in Django? I have a view like this: # Create your views from django.contrib.gis.utils import GeoIP from django.template import RequestContext from django.shortcuts import render_to_response def home(request): g = GeoIP() …
avatar
  • 12,087
  • 17
  • 66
  • 82
371
votes
8 answers

How do I filter query objects by date range in Django?

I've got a field in one model like: class Sample(models.Model): date = fields.DateField(auto_now=False) Now, I need to filter the objects by a date range. How do I filter all the objects that have a date between 1-Jan-2011 and 31-Jan-2011?
user469652
  • 48,855
  • 59
  • 128
  • 165
366
votes
28 answers

When saving, how can you check if a field has changed?

In my model I have : class Alias(MyBaseModel): remote_image = models.URLField( max_length=500, null=True, help_text=''' A URL that is downloaded and cached for the image. Only used when the alias is made …
Paul Tarjan
  • 48,968
  • 59
  • 172
  • 213
355
votes
21 answers

DatabaseError: current transaction is aborted, commands ignored until end of transaction block?

I got a lot of errors with the message : "DatabaseError: current transaction is aborted, commands ignored until end of transaction block" after changed from python-psycopg to python-psycopg2 as Django project's database engine. The code remains…
jack
  • 17,261
  • 37
  • 100
  • 125