Django “for” loop by date
I'm doing blog app. I did:
{% for entry in entry.all %}
<div class="timelinestamp">
...
</div>
<br />
<br />
<br />
<br />
{% endfor %}
and almost everything works fine. I changed one Entry in my admin panel (The very first Entry...). Since then the order of my post has changed... Can anyone explain me why ? Or tell how to using loop render all Entries sorted by date ?
class Entry(models.Model):
title = models.CharField(max_length=120)
pub_date = models.DateField(null=False)
body = models.TextField()
image = models.ImageField(upload_to='images/', max_length = 100)
def __str__(self):
return self.title
The pub_date field is NOT primary key in my DB! I'm using Django 2.1
python html django
add a comment |
I'm doing blog app. I did:
{% for entry in entry.all %}
<div class="timelinestamp">
...
</div>
<br />
<br />
<br />
<br />
{% endfor %}
and almost everything works fine. I changed one Entry in my admin panel (The very first Entry...). Since then the order of my post has changed... Can anyone explain me why ? Or tell how to using loop render all Entries sorted by date ?
class Entry(models.Model):
title = models.CharField(max_length=120)
pub_date = models.DateField(null=False)
body = models.TextField()
image = models.ImageField(upload_to='images/', max_length = 100)
def __str__(self):
return self.title
The pub_date field is NOT primary key in my DB! I'm using Django 2.1
python html django
add a comment |
I'm doing blog app. I did:
{% for entry in entry.all %}
<div class="timelinestamp">
...
</div>
<br />
<br />
<br />
<br />
{% endfor %}
and almost everything works fine. I changed one Entry in my admin panel (The very first Entry...). Since then the order of my post has changed... Can anyone explain me why ? Or tell how to using loop render all Entries sorted by date ?
class Entry(models.Model):
title = models.CharField(max_length=120)
pub_date = models.DateField(null=False)
body = models.TextField()
image = models.ImageField(upload_to='images/', max_length = 100)
def __str__(self):
return self.title
The pub_date field is NOT primary key in my DB! I'm using Django 2.1
python html django
I'm doing blog app. I did:
{% for entry in entry.all %}
<div class="timelinestamp">
...
</div>
<br />
<br />
<br />
<br />
{% endfor %}
and almost everything works fine. I changed one Entry in my admin panel (The very first Entry...). Since then the order of my post has changed... Can anyone explain me why ? Or tell how to using loop render all Entries sorted by date ?
class Entry(models.Model):
title = models.CharField(max_length=120)
pub_date = models.DateField(null=False)
body = models.TextField()
image = models.ImageField(upload_to='images/', max_length = 100)
def __str__(self):
return self.title
The pub_date field is NOT primary key in my DB! I'm using Django 2.1
python html django
python html django
asked Nov 12 '18 at 0:50
DrJoe
165
165
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
From the docs:
If a query doesn’t have an ordering specified, results are returned from the database in an unspecified order. A particular ordering is guaranteed only when ordering by a set of fields that uniquely identify each object in the results. For example, if a name field isn’t unique, ordering by it won’t guarantee objects with the same name always appear in the same order.
It looks like you want to order by pub_date
? Use ordering
:
class Entry(models.Model):
...
class Meta:
ordering = ['-pub_date']
add a comment |
As far as I can see you haven't defined a sort order for your Entry
model. This means that you will process those entries in a non-defined order.
To order your entries you could set a default sort order on Entry
:
class Entry(models.Model):
title = models.CharField(max_length=120)
pub_date = models.DateField(null=False)
body = models.TextField()
image = models.ImageField(upload_to='images/', max_length = 100)
def __str__(self):
return self.title
class Meta:
ordering = ('-pub_date',)
Or, if that's not what you're looking for, you could order your queryset in your view:
Entry.objects.all().order_by('-pub_date')
add a comment |
If you have created model definition without the ordering
meta option items in the database are not enforced any ordering, everytime when you do Model.objects.all()
it will give you items without any order. If you want to queries to be in specific order you can:
- Add
ordering
option to Meta options to model definition - which would require
database migrations - Modify your query to enforce ordering like
Model.objects.all().order_by('-pub_date')
- also pass the query as context object to template like:
views.py -
entries_by_pub_date = Model.objects.all().order_by('-pub_date')
context['entries_by_pub_date'] = entries_by_pub_date
template
{% for entry in entries_by_pub_date %}
...
{% enfor %}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254743%2fdjango-for-loop-by-date%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
From the docs:
If a query doesn’t have an ordering specified, results are returned from the database in an unspecified order. A particular ordering is guaranteed only when ordering by a set of fields that uniquely identify each object in the results. For example, if a name field isn’t unique, ordering by it won’t guarantee objects with the same name always appear in the same order.
It looks like you want to order by pub_date
? Use ordering
:
class Entry(models.Model):
...
class Meta:
ordering = ['-pub_date']
add a comment |
From the docs:
If a query doesn’t have an ordering specified, results are returned from the database in an unspecified order. A particular ordering is guaranteed only when ordering by a set of fields that uniquely identify each object in the results. For example, if a name field isn’t unique, ordering by it won’t guarantee objects with the same name always appear in the same order.
It looks like you want to order by pub_date
? Use ordering
:
class Entry(models.Model):
...
class Meta:
ordering = ['-pub_date']
add a comment |
From the docs:
If a query doesn’t have an ordering specified, results are returned from the database in an unspecified order. A particular ordering is guaranteed only when ordering by a set of fields that uniquely identify each object in the results. For example, if a name field isn’t unique, ordering by it won’t guarantee objects with the same name always appear in the same order.
It looks like you want to order by pub_date
? Use ordering
:
class Entry(models.Model):
...
class Meta:
ordering = ['-pub_date']
From the docs:
If a query doesn’t have an ordering specified, results are returned from the database in an unspecified order. A particular ordering is guaranteed only when ordering by a set of fields that uniquely identify each object in the results. For example, if a name field isn’t unique, ordering by it won’t guarantee objects with the same name always appear in the same order.
It looks like you want to order by pub_date
? Use ordering
:
class Entry(models.Model):
...
class Meta:
ordering = ['-pub_date']
edited Nov 12 '18 at 1:15
answered Nov 12 '18 at 1:06
rikAtee
4,77542956
4,77542956
add a comment |
add a comment |
As far as I can see you haven't defined a sort order for your Entry
model. This means that you will process those entries in a non-defined order.
To order your entries you could set a default sort order on Entry
:
class Entry(models.Model):
title = models.CharField(max_length=120)
pub_date = models.DateField(null=False)
body = models.TextField()
image = models.ImageField(upload_to='images/', max_length = 100)
def __str__(self):
return self.title
class Meta:
ordering = ('-pub_date',)
Or, if that's not what you're looking for, you could order your queryset in your view:
Entry.objects.all().order_by('-pub_date')
add a comment |
As far as I can see you haven't defined a sort order for your Entry
model. This means that you will process those entries in a non-defined order.
To order your entries you could set a default sort order on Entry
:
class Entry(models.Model):
title = models.CharField(max_length=120)
pub_date = models.DateField(null=False)
body = models.TextField()
image = models.ImageField(upload_to='images/', max_length = 100)
def __str__(self):
return self.title
class Meta:
ordering = ('-pub_date',)
Or, if that's not what you're looking for, you could order your queryset in your view:
Entry.objects.all().order_by('-pub_date')
add a comment |
As far as I can see you haven't defined a sort order for your Entry
model. This means that you will process those entries in a non-defined order.
To order your entries you could set a default sort order on Entry
:
class Entry(models.Model):
title = models.CharField(max_length=120)
pub_date = models.DateField(null=False)
body = models.TextField()
image = models.ImageField(upload_to='images/', max_length = 100)
def __str__(self):
return self.title
class Meta:
ordering = ('-pub_date',)
Or, if that's not what you're looking for, you could order your queryset in your view:
Entry.objects.all().order_by('-pub_date')
As far as I can see you haven't defined a sort order for your Entry
model. This means that you will process those entries in a non-defined order.
To order your entries you could set a default sort order on Entry
:
class Entry(models.Model):
title = models.CharField(max_length=120)
pub_date = models.DateField(null=False)
body = models.TextField()
image = models.ImageField(upload_to='images/', max_length = 100)
def __str__(self):
return self.title
class Meta:
ordering = ('-pub_date',)
Or, if that's not what you're looking for, you could order your queryset in your view:
Entry.objects.all().order_by('-pub_date')
answered Nov 12 '18 at 1:11
Sören Weber
1918
1918
add a comment |
add a comment |
If you have created model definition without the ordering
meta option items in the database are not enforced any ordering, everytime when you do Model.objects.all()
it will give you items without any order. If you want to queries to be in specific order you can:
- Add
ordering
option to Meta options to model definition - which would require
database migrations - Modify your query to enforce ordering like
Model.objects.all().order_by('-pub_date')
- also pass the query as context object to template like:
views.py -
entries_by_pub_date = Model.objects.all().order_by('-pub_date')
context['entries_by_pub_date'] = entries_by_pub_date
template
{% for entry in entries_by_pub_date %}
...
{% enfor %}
add a comment |
If you have created model definition without the ordering
meta option items in the database are not enforced any ordering, everytime when you do Model.objects.all()
it will give you items without any order. If you want to queries to be in specific order you can:
- Add
ordering
option to Meta options to model definition - which would require
database migrations - Modify your query to enforce ordering like
Model.objects.all().order_by('-pub_date')
- also pass the query as context object to template like:
views.py -
entries_by_pub_date = Model.objects.all().order_by('-pub_date')
context['entries_by_pub_date'] = entries_by_pub_date
template
{% for entry in entries_by_pub_date %}
...
{% enfor %}
add a comment |
If you have created model definition without the ordering
meta option items in the database are not enforced any ordering, everytime when you do Model.objects.all()
it will give you items without any order. If you want to queries to be in specific order you can:
- Add
ordering
option to Meta options to model definition - which would require
database migrations - Modify your query to enforce ordering like
Model.objects.all().order_by('-pub_date')
- also pass the query as context object to template like:
views.py -
entries_by_pub_date = Model.objects.all().order_by('-pub_date')
context['entries_by_pub_date'] = entries_by_pub_date
template
{% for entry in entries_by_pub_date %}
...
{% enfor %}
If you have created model definition without the ordering
meta option items in the database are not enforced any ordering, everytime when you do Model.objects.all()
it will give you items without any order. If you want to queries to be in specific order you can:
- Add
ordering
option to Meta options to model definition - which would require
database migrations - Modify your query to enforce ordering like
Model.objects.all().order_by('-pub_date')
- also pass the query as context object to template like:
views.py -
entries_by_pub_date = Model.objects.all().order_by('-pub_date')
context['entries_by_pub_date'] = entries_by_pub_date
template
{% for entry in entries_by_pub_date %}
...
{% enfor %}
answered Nov 12 '18 at 6:57
Sanchit
9614
9614
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254743%2fdjango-for-loop-by-date%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown