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
310
votes
19 answers

How to use the variables from "views.py" in JavasScript, "" in Django Template?

When I render a page using the Django template renderer, I can pass in a dictionary variable containing various values to manipulate them in the page using {{ myVar }}. Is there a way to access the same variable in Javascript,
AlMcLean
  • 3,459
  • 2
  • 19
  • 14
310
votes
8 answers

How do I integrate Ajax with Django applications?

I am new to Django and pretty new to Ajax. I am working on a project where I need to integrate the two. I believe that I understand the principles behind them both, but have not found a good explanation of the two together. Could someone give me a…
tjons
  • 4,749
  • 5
  • 28
  • 36
309
votes
9 answers

Django rest framework, use different serializers in the same ModelViewSet

I would like to provide two different serializers and yet be able to benefit from all the facilities of ModelViewSet: When viewing a list of objects, I would like each object to have an url which redirects to its details and every other relation…
BlackBear
  • 22,411
  • 10
  • 48
  • 86
308
votes
12 answers

AngularJS with Django - Conflicting template tags

I want to use AngularJS with Django however they both use {{ }} as their template tags. Is there an easy way to change one of the two to use some other custom templating tag?
Endophage
  • 21,038
  • 13
  • 59
  • 90
308
votes
9 answers

Django set default form values

I have a Model as follows: class TankJournal(models.Model): user = models.ForeignKey(User) tank = models.ForeignKey(TankProfile) ts = models.IntegerField(max_length=15) title = models.CharField(max_length=50) body =…
Mike
  • 7,769
  • 13
  • 57
  • 81
308
votes
55 answers

Favorite Django Tips & Features?

Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of. Please, include only one tip per answer. Add Django version requirements if there are…
Haes
  • 12,891
  • 11
  • 46
  • 50
306
votes
2 answers

How do I make many-to-many field optional in Django?

When you have a many-to-many relationship (related_name, not through) and you are trying to use the admin interface you are required to enter one of the relationships even though it does not have to exist for you to create the first entry. I'm…
DZ.
  • 3,181
  • 2
  • 15
  • 10
304
votes
8 answers

How to use "get_or_create()" in Django?

I'm trying to use get_or_create() for some fields in my forms, but I'm getting a 500 error when I try to do so. One of the lines looks like this: customer.source = Source.objects.get_or_create(name="Website") The error I get for the above code…
Stephen
  • 5,959
  • 10
  • 33
  • 43
300
votes
11 answers

Django gives Bad Request (400) when DEBUG = False

I am new to django-1.6. When I run the django server with DEBUG = True, it's running perfectly. But when I change DEBUG to False in the settings file, then the server stopped and it gives the following error on the command prompt: CommandError: You…
codeimplementer
  • 3,303
  • 2
  • 14
  • 16
300
votes
1 answer

How to query Case-insensitive data in Django ORM?

How can I query/filter in Django and ignore the cases of my query-string? I've got something like and like to ignore the case of my_parameter: MyClass.objects.filter(name=my_parameter)
Ron
  • 22,128
  • 31
  • 108
  • 206
298
votes
8 answers

Django - limiting query results

I want to take the last 10 instances of a model and have this code: Model.objects.all().order_by('-id')[:10] Is it true that firstly pick up all instances, and then take only 10 last ones? Is there any more effective method?
krzyhub
  • 6,285
  • 11
  • 42
  • 68
298
votes
5 answers

Django values_list vs values

In Django, what's the difference between the following two: Article.objects.values_list('comment_id', flat=True).distinct() versus: Article.objects.values('comment_id').distinct() My goal is to get a list of unique comment ids under each Article.…
Hassan Baig
  • 15,055
  • 27
  • 102
  • 205
298
votes
9 answers

How to set a value of a variable inside a template code?

Say I have a template
Hello {{name}}!
While testing it, it would be useful to define the value of the variable without touching the python code that invokes this template. So I'm looking for something like this {% set…
Alexis
  • 2,983
  • 2
  • 17
  • 5
293
votes
18 answers

Django MEDIA_URL and MEDIA_ROOT

I'm trying to upload an image via the Django admin and then view that image either in a page on the frontend or just via a URL. Note this is all on my local machine. My settings are as follows: MEDIA_ROOT = '/home/dan/mysite/media/' MEDIA_URL =…
Dan
  • 3,041
  • 2
  • 17
  • 9
293
votes
9 answers

Fastest way to get the first object from a queryset in django?

Often I find myself wanting to get the first object from a queryset in Django, or return None if there aren't any. There are lots of ways to do this which all work. But I'm wondering which is the most performant. qs = MyModel.objects.filter(blah =…
Leopd
  • 41,333
  • 31
  • 129
  • 167