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
511k views

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
209k views

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
669k views

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
674k views

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
458k views

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
851k views

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
256k views

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
466k views

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
430k views

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
1.1m views

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
507k views

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
851k views

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
420k views

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
322k views

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
1.2m views

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
525k views

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
883k views

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
502k views

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
149k views

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 models.py. 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
219k views

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
223k views

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
739k views

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
723k views

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
2.5m views

'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 get-pip.py Requirement already up-to-date: ...
user3597950's user avatar
  • 9,291
555 votes
12 answers
194k views

OneToOneField() vs ForeignKey() in Django

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

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
924k views

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
414k views

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
304k views

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
368k views

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
210k views

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
535k views

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
440k views

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

How can I get the full/absolute URL (e.g. https://example.com/some/path) 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
737k views

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
767k views

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
285k views

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
570k views

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
575k views

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
363k views

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
368k views

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
303k views

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
770k views

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
197k views

Can I access constants in settings.py from templates in Django?

I have some stuff in settings.py 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
495k views

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
349k views

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
284k views

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
243k views

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
261k views

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
299k views

(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 ...
Mulagala's user avatar
  • 8,271
406 votes
5 answers
195k views

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

1
2 3 4 5
6192