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.

Filter by
Sorted by
Tagged with
1353 votes
28 answers

What is the difference between null=True and blank=True in Django?

When we add a model field in Django we generally write: models.CharField(max_length=100, null=True, blank=True) The same is done with ForeignKey, DecimalField etc. What is the basic difference ...
user993563's user avatar
  • 18.6k
1220 votes
29 answers

Has Django served an excess of 100k daily visits? [closed]

I'm building a web application with Django. The reasons I chose Django were: I wanted to work with free/open-source tools. I like Python and feel it's a long-term language, whereas regarding Ruby I ...
Roee Adler's user avatar
  • 33.5k
968 votes
10 answers

Is there a list of Pytz Timezones?

I would like to know what are all the possible values for the timezone argument in the Python library pytz. How to do it?
ipegasus's user avatar
  • 14.8k
960 votes
17 answers

How do I do a not equal in Django queryset filtering?

In Django model QuerySets, I see that there is a __gt and __lt for comparative values, but is there a __ne or != (not equals)? I want to filter out using a not equals. For example, for Model: bool ...
MikeN's user avatar
  • 45.1k
823 votes
16 answers

How to combine multiple QuerySets in Django?

I'm trying to build the search for a Django site I am building, and in that search, I am searching across three different models. And to get pagination on the search result list, I would like to use a ...
espenhogbakk's user avatar
  • 11.5k
808 votes
10 answers

How can I temporarily disable a foreign key constraint in MySQL?

Is it possible to temporarily disable constraints in MySQL? I have two Django models, each with a foreign key to the other one. Deleting instances of a model returns an error because of the foreign ...
jul's user avatar
  • 36.4k
760 votes
14 answers

What is a "slug" in Django?

When I read Django code I often see in models what is called a "slug". I am not quite sure what this is, but I do know it has something to do with URLs. How and when is this slug-thing ...
Jonas's user avatar
  • 19.4k
756 votes
14 answers

How to revert the last migration?

I've made a migration that added a new table and want to revert it and delete the migration, without creating a new migration. How do I do it? Is there a command to revert last migration and then I ...
Ronen Ness's user avatar
  • 9,993
743 votes
10 answers

How to upload a file in Django? [closed]

What is the minimal example code needed for a "hello world" app using Django 1.3, that enables the user to upload a file?
qliq's user avatar
  • 11.7k
730 votes
10 answers

Where does pip install its packages?

I activated a virtualenv which has pip installed. I did pip3 install Django==1.8 and Django successfully downloaded. Now, I want to open up the Django folder. Where is the folder located? Normally it ...
SilentDev's user avatar
  • 21.1k
704 votes
11 answers

What does on_delete do on Django models?

I'm quite familiar with Django, but I recently noticed there exists an on_delete=models.CASCADE option with the models. I have searched for the documentation for the same, but I couldn't find anything ...
All Іѕ Vаиітy's user avatar
702 votes
32 answers

How to check Django version

I have to use Python and Django for our application. So, I have two versions of Python, 2.6 and 2.7. Now I have installed Django. I could run the sample application for testing Django succesfuly. But ...
maheshgupta024's user avatar
658 votes
6 answers

What is related_name used for?

What is the related_name argument useful for on ManyToManyField and ForeignKey fields? For example, given the following code, what is the effect of related_name='maps'? class Map(db.Model): ...
zjm1126's user avatar
  • 63.6k
652 votes
27 answers

How to debug in Django, the good way? [closed]

So, I started learning to code in Python and later Django. The first times it was hard looking at tracebacks and actually figure out what I did wrong and where the syntax error was. Some time has ...
googletorp's user avatar
  • 33.1k
640 votes
11 answers

How can I upgrade specific packages using pip and a requirements file?

I'm using pip with a requirements file, in a virtualenv, for my Django projects. I'm trying to upgrade some packages, notably Django itself, and I'm getting an error about source code conflicts: ...
gcaprio's user avatar
  • 6,457
632 votes
24 answers

How to reset Django admin password?

I am using Django (version 1.3) and have forgotten both admin username and password. How to reset both? And is it possible to make a normal user into admin, and then remove admin status?
IamH1kc's user avatar
  • 6,602
622 votes
19 answers

How to get GET request values in Django?

I am currently defining regular expressions in order to capture parameters in a URL, as described in the tutorial. How do I access parameters from the URL as part the HttpRequest object? My ...
sutee's user avatar
  • 12.6k
610 votes
9 answers

How to filter empty or NULL names in a QuerySet?

I have first_name, last_name & alias (optional) which I need to search for. So, I need a query to give me all the names that have an alias set. Only if I could do: Name.objects.filter(alias!="") ...
Val Neekman's user avatar
  • 17.7k
609 votes
9 answers

Separation of business logic and data access in django

I am writing a project in Django and I see that 80% of the code is in the file This code is confusing and, after a certain time, I cease to understand what is really happening. Here is what ...
defuz's user avatar
  • 26.8k
585 votes
26 answers

Set up a scheduled job?

I've been working on a web app using Django, and I'm curious if there is a way to schedule a job to run periodically. Basically I just want to run through the database and make some calculations/...
TM.'s user avatar
  • 108k
576 votes
5 answers

How to define two fields "unique" as couple

Is there a way to define a couple of fields as unique in Django? I have a table of volumes (of journals) and I don't want more then one volume number for the same journal. class Volume(models.Model):...
Giovanni Di Milia's user avatar
570 votes
17 answers

Creating a JSON response using Django and Python

I'm trying to convert a server side Ajax response script into a Django HttpResponse, but apparently it's not working. This is the server-side script: /* RECEIVE VALUE */ $validateValue=$_POST['...
Switch's user avatar
  • 14.8k
570 votes
38 answers

No module named pkg_resources

I'm deploying a Django app to a dev server and am hitting this error when I run pip install -r requirements.txt: Traceback (most recent call last): File "/var/www/mydir/virtualenvs/dev/bin/pip", ...
igniteflow's user avatar
  • 8,424
560 votes
42 answers

'pip' is not recognized as an internal or external command

I'm running into a weird error when trying to install Django on my computer. This is the sequence that I typed into my command line: C:\Python34> python Requirement already up-to-date: ...
user3597950's user avatar
  • 9,291
555 votes
12 answers

OneToOneField() vs ForeignKey() in Django

What's the difference between Django OneToOneField and ForeignKey?
redice's user avatar
  • 8,437
542 votes
17 answers

What's the best way to extend the User model in Django?

What's the best way to extend the User model (bundled with Django's authentication app) with custom fields? I would also possibly like to use the email as the username (for authentication purposes). ...
Farinha's user avatar
  • 17.6k
524 votes
33 answers

No module named MySQLdb

I am using Python version 2.5.4 and install MySQL version 5.0 and Django. Django is working fine with Python, but not MySQL. I am using it in Windows Vista.
jbcedge's user avatar
  • 19k
524 votes
30 answers

In a Django form, how do I make a field readonly (or disabled) so that it cannot be edited?

In a Django form, how do I make a field read-only (or disabled)? When the form is being used to create a new entry, all fields should be enabled - but when the record is in update mode some fields ...
X10's user avatar
  • 17.2k
522 votes
6 answers

What's the difference between select_related and prefetch_related in Django ORM?

In Django doc: select_related() "follows" foreign-key relationships, selecting additional related-object data when it executes its query. prefetch_related() does a separate lookup for each ...
NeoWang's user avatar
  • 17.5k
516 votes
12 answers

RuntimeWarning: DateTimeField received a naive datetime

I m trying to send a simple mail using IPython. I have not set up any models still getting this error. What can be done? Error : /home/sourabh/Django/learn/local/lib/python2.7/site-packages/...
shifu's user avatar
  • 6,616
505 votes
21 answers

Why does DEBUG=False setting make my django Static Files Access fail?

Am building an app using Django as my workhorse. All has been well so far - specified db settings, configured static directories, urls, views etc. But trouble started sneaking in the moment I wanted ...
JWL's user avatar
  • 13.7k
499 votes
13 answers

How to query as GROUP BY in Django?

I query a model: Members.objects.all() And it returns: Eric, Salesman, X-Shop Freddie, Manager, X2-Shop Teddy, Salesman, X2-Shop Sean, Manager, X2-Shop What I want is to know the best Django way ...
simplyharsh's user avatar
  • 35.6k
498 votes
30 answers

How can I get the full/absolute URL (with domain) in Django?

How can I get the full/absolute URL (e.g. in Django without the Sites module? That's just silly... I shouldn't need to query my DB to snag the URL! I want to use it ...
mpen's user avatar
  • 273k
485 votes
12 answers

CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true

I have a setup involving Frontend server (Node.js, domain: localhost:3000) <---> Backend (Django, Ajax, domain: localhost:8000) Browser <-- webapp <-- Node.js (Serve the app) Browser (...
ixaxaar's user avatar
  • 6,413
481 votes
32 answers

How to convert JSON data into a Python object?

I want to convert JSON data into a Python object. I receive JSON data objects from the Facebook API, which I want to store in my database. My current View in Django (Python) (request.POST contains the ...
Sai Krishna's user avatar
  • 7,657
477 votes
8 answers

How do I do an OR filter in a Django query?

I want to be able to list the items that either a user has added (they are listed as the creator) or the item has been approved. So I basically need to select: item.creator = owner or item.moderated ...
Mez's user avatar
  • 24.4k
477 votes
12 answers

django order_by query set, ascending and descending

How can I order by descending my query set in django by date? Reserved.objects.all().filter(client=client_id).order_by('check_in') I just want to filter from descending all the Reserved by check_in ...
gadss's user avatar
  • 21.7k
473 votes
10 answers

How to delete a record in Django models?

I want to delete a particular record like: delete from table_name where id = 1; How can I do this in a django model?
user426795's user avatar
  • 11.1k
457 votes
18 answers

Convert Django Model object to dict with all of the fields intact

How does one convert a django Model object to a dict with all of its fields? All ideally includes foreign keys and fields with editable=False. Let me elaborate. Let's say I have a django model like ...
Zags's user avatar
  • 37.6k
452 votes
4 answers

How can I filter a Django query with a list of values?

I'm sure this is a trivial operation, but I can't figure out how it's done. There's got to be something smarter than this: ids = [1, 3, 6, 7, 9] for id in ids: MyModel.objects.filter(pk=id) I'...
ajwood's user avatar
  • 18.3k
451 votes
25 answers

How to see the raw SQL queries Django is running?

Is there a way to show the SQL that Django is running while performing a query?
spence91's user avatar
  • 77.3k
443 votes
10 answers

Where can I find the error logs of nginx, using FastCGI and Django?

I'm using Django with FastCGI + nginx. Where are the logs (errors) stored in this case?
ha22109's user avatar
  • 8,066
439 votes
17 answers

Can I access constants in from templates in Django?

I have some stuff in that I'd like to be able to access from a template, but I can't figure out how to do it. I already tried {{CONSTANT_NAME}} but that doesn't seem to work. Is this ...
Paul Wicks's user avatar
  • 63.2k
434 votes
16 answers

NumPy array is not JSON serializable

After creating a NumPy array, and saving it as a Django context variable, I receive the following error when loading the webpage: array([ 0, 239, 479, 717, 952, 1192, 1432, 1667], dtype=int64) ...
Karnivaurus's user avatar
  • 22.9k
431 votes
5 answers

Django Model() vs Model.objects.create()

What it the difference between running two commands: foo = FooModel() and bar = BarModel.objects.create() Does the second one immediately create a BarModel in the database, while for FooModel, the ...
0leg's user avatar
  • 13.5k
427 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(...
Elisa's user avatar
  • 6,875
413 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 ...
Huuuze's user avatar
  • 15.6k
410 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's user avatar
  • 9,166
406 votes
17 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= in Nginx server, I am getting the following ...
Mulagala's user avatar
  • 8,271
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 "...
Jonathan Gleason's user avatar

2 3 4 5