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
353
votes
15 answers

How do I clone a Django model instance object and save it to the database?

Foo.objects.get(pk="foo") In the database, I want to add another object which is a copy of the object above. Suppose my table has one row. I want to insert the first row object into another row with a different primary key. How can I do…
user426795
  • 11,073
  • 11
  • 35
  • 35
352
votes
4 answers

Django: Display Choice Value

models.py: class Person(models.Model): name = models.CharField(max_length=200) CATEGORY_CHOICES = ( ('M', 'Male'), ('F', 'Female'), ) gender = models.CharField(max_length=200, choices=CATEGORY_CHOICES) …
Shankze
  • 3,813
  • 3
  • 17
  • 11
352
votes
12 answers

Django auto_now and auto_now_add

For Django 1.1. I have this in my models.py: class User(models.Model): created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) When updating a row I get: [Sun Nov 15 02:18:12 2009] [error]…
Paul Tarjan
  • 48,968
  • 59
  • 172
  • 213
348
votes
22 answers

How to manage local vs production settings in Django?

What is the recommended way of handling settings for local development and the production server? Some of them (like constants, etc) can be changed/accessed in both, but some of them (like paths to static files) need to remain different, and hence…
akv
  • 3,693
  • 3
  • 17
  • 11
344
votes
27 answers

How to execute a Python script from the Django shell?

I need to execute a Python script from the Django shell. I tried: ./manage.py shell << my_script.py But it didn't work. It was just waiting for me to write something.
user2429940
342
votes
4 answers

How to update an existing Conda environment with a .yml file

How can a pre-existing conda environment be updated with another .yml file. This is extremely helpful when working on projects that have multiple requirement files, i.e. base.yml, local.yml, production.yml, etc. For example, below is a base.yml file…
user6536435
338
votes
6 answers

Best practices for adding .gitignore file for Python projects?

I'm trying to collect some of my default settings, and one thing I realized I don't have a standard for is .gitignore files. There's a great thread showing a good .gitignore for Visual Studio projects, but I don't see many recommendations for Python…
ewall
  • 27,179
  • 15
  • 70
  • 84
337
votes
14 answers

Can't compare naive and aware datetime.now() <= challenge.datetime_end

I am trying to compare the current date and time with dates and times specified in models using comparison operators: if challenge.datetime_start <= datetime.now() <= challenge.datetime_end: The script errors out with: TypeError: can't compare…
sccrthlt
  • 3,974
  • 5
  • 20
  • 24
336
votes
20 answers

Numeric for loop in Django templates

How do I write a numeric for loop in a Django template? I mean something like for i = 1 to n
Lev
  • 6,487
  • 6
  • 28
  • 29
332
votes
7 answers

How to check if a user is logged in (how to properly use user.is_authenticated)?

I am looking over this website but just can't seem to figure out how to do this as it's not working. I need to check if the current site user is logged in (authenticated), and am trying: request.user.is_authenticated despite being sure that the…
Rick
  • 16,612
  • 34
  • 110
  • 163
326
votes
32 answers

Setting DEBUG = False causes 500 Error

Once I change the DEBUG = False, my site will generate 500 (using wsgi & manage.py runserver), and there is no error info in Apache error log and it will run normally when I change debug to True . I'm using Django 1.5 & Python 2.7.3 here is Apache…
zhiguo.wang
  • 4,393
  • 4
  • 16
  • 8
322
votes
7 answers

What is reverse()?

When I read Django code sometimes, I see in some templates reverse(). I am not quite sure what this is but it is used together with HttpResponseRedirect. How and when is this reverse() supposed to be used?
lakshmen
  • 28,346
  • 66
  • 178
  • 276
319
votes
17 answers

Using Python's os.path, how do I go up one directory?

I recently upgrade Django from v1.3.1 to v1.4. In my old settings.py I have TEMPLATE_DIRS = ( os.path.join(os.path.dirname( __file__ ), 'templates').replace('\\', '/'), # Put strings here, like "/home/html/django_templates" or…
hobbes3
  • 28,078
  • 24
  • 87
  • 116
318
votes
20 answers

Cron and virtualenv

I am trying to run a Django management command from cron. I am using virtualenv to keep my project sandboxed. I have seen examples here and elsewhere that show running management commands from within virtualenv's like: 0 3 * * * source…
John-Scott
  • 3,213
  • 3
  • 16
  • 7
311
votes
10 answers

Django template how to look up a dictionary value with a variable

mydict = {"key1":"value1", "key2":"value2"} The regular way to lookup a dictionary value in a Django template is {{ mydict.key1 }}, {{ mydict.key2 }}. What if the key is a loop variable? ie: {% for item in list %} # where item has an attribute…
Stan
  • 37,207
  • 50
  • 124
  • 185