how can i increment a variable with an “if” in a django template?
up vote
0
down vote
favorite
I'm trying to increment a variable with "if" in the Django template, I've already tried all the suggestions on this site, but none have solved my problem.
there is views.py
class IndexView(generic.ListView):
template_name = 'home/index.html'
context_object_name = 'problemes'
def get_context_data(self, **kwargs):
context_data = super().get_context_data(**kwargs)
context_data['problemes'] = Probleme.objects.all()
context_data['commentaires'] = Commentaire.objects.all()
return context_data
def get_queryset(self):
result_list = self.get_context_data
return result_list
and index.html
....
% for problem in problemes %
% with number_comment= 0%
% for comment in commentaires %
% if comment.probleme.id == problem.id %
% number_comment ++ %
% endif %
% endfor %
<tr data-status="pagado">
<td>
<div class="votes">
<div class="mini-counts"><span title="74 votes">74</span></div>
<div>votes</div>
</div>
</td>
<td>
<div class="status answered-accepted" title="one of the answers was accepted as the correct answer">
<div class="mini-counts"><span title="2 answers"> number_comment </span></div>
<div>answers</div>
</div>
% endwith %
....
django django-models django-forms
add a comment |
up vote
0
down vote
favorite
I'm trying to increment a variable with "if" in the Django template, I've already tried all the suggestions on this site, but none have solved my problem.
there is views.py
class IndexView(generic.ListView):
template_name = 'home/index.html'
context_object_name = 'problemes'
def get_context_data(self, **kwargs):
context_data = super().get_context_data(**kwargs)
context_data['problemes'] = Probleme.objects.all()
context_data['commentaires'] = Commentaire.objects.all()
return context_data
def get_queryset(self):
result_list = self.get_context_data
return result_list
and index.html
....
% for problem in problemes %
% with number_comment= 0%
% for comment in commentaires %
% if comment.probleme.id == problem.id %
% number_comment ++ %
% endif %
% endfor %
<tr data-status="pagado">
<td>
<div class="votes">
<div class="mini-counts"><span title="74 votes">74</span></div>
<div>votes</div>
</div>
</td>
<td>
<div class="status answered-accepted" title="one of the answers was accepted as the correct answer">
<div class="mini-counts"><span title="2 answers"> number_comment </span></div>
<div>answers</div>
</div>
% endwith %
....
django django-models django-forms
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to increment a variable with "if" in the Django template, I've already tried all the suggestions on this site, but none have solved my problem.
there is views.py
class IndexView(generic.ListView):
template_name = 'home/index.html'
context_object_name = 'problemes'
def get_context_data(self, **kwargs):
context_data = super().get_context_data(**kwargs)
context_data['problemes'] = Probleme.objects.all()
context_data['commentaires'] = Commentaire.objects.all()
return context_data
def get_queryset(self):
result_list = self.get_context_data
return result_list
and index.html
....
% for problem in problemes %
% with number_comment= 0%
% for comment in commentaires %
% if comment.probleme.id == problem.id %
% number_comment ++ %
% endif %
% endfor %
<tr data-status="pagado">
<td>
<div class="votes">
<div class="mini-counts"><span title="74 votes">74</span></div>
<div>votes</div>
</div>
</td>
<td>
<div class="status answered-accepted" title="one of the answers was accepted as the correct answer">
<div class="mini-counts"><span title="2 answers"> number_comment </span></div>
<div>answers</div>
</div>
% endwith %
....
django django-models django-forms
I'm trying to increment a variable with "if" in the Django template, I've already tried all the suggestions on this site, but none have solved my problem.
there is views.py
class IndexView(generic.ListView):
template_name = 'home/index.html'
context_object_name = 'problemes'
def get_context_data(self, **kwargs):
context_data = super().get_context_data(**kwargs)
context_data['problemes'] = Probleme.objects.all()
context_data['commentaires'] = Commentaire.objects.all()
return context_data
def get_queryset(self):
result_list = self.get_context_data
return result_list
and index.html
....
% for problem in problemes %
% with number_comment= 0%
% for comment in commentaires %
% if comment.probleme.id == problem.id %
% number_comment ++ %
% endif %
% endfor %
<tr data-status="pagado">
<td>
<div class="votes">
<div class="mini-counts"><span title="74 votes">74</span></div>
<div>votes</div>
</div>
</td>
<td>
<div class="status answered-accepted" title="one of the answers was accepted as the correct answer">
<div class="mini-counts"><span title="2 answers"> number_comment </span></div>
<div>answers</div>
</div>
% endwith %
....
django django-models django-forms
django django-models django-forms
asked Nov 11 at 1:35
Hafid
52
52
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
If you can reference your number_comment
directly as you have it in your index.html, you can use an add
template tag to increment it. Of course this increments number only in template and doesn't save to the database.
number_comment
thanks, i already tried this line but it doesn't work :/
– Hafid
Nov 11 at 9:28
Where do you get this number_comment from? Is a field of your Commentaire model? In this case you should access it with commment.number_comment.
– Uroš Trstenjak
Nov 11 at 10:01
no, is not a field of commentaire, it's a variable that I want to increment when I explore a list of items with a condition if something ... (with template)
– Hafid
Nov 11 at 10:13
any help, please!
– Hafid
Nov 11 at 10:19
But you should pass this variable to the template from the view. You can't instantiate variables within the template. You could of course do it with a JavaScript, but that is something completely different. You can for example instantiate the variable in you view like that context_data['number_comment'] = 0. And than my answer would work.
– Uroš Trstenjak
Nov 11 at 10:27
|
show 3 more comments
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%2f53245102%2fhow-can-i-increment-a-variable-with-an-if-in-a-django-template%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
If you can reference your number_comment
directly as you have it in your index.html, you can use an add
template tag to increment it. Of course this increments number only in template and doesn't save to the database.
number_comment
thanks, i already tried this line but it doesn't work :/
– Hafid
Nov 11 at 9:28
Where do you get this number_comment from? Is a field of your Commentaire model? In this case you should access it with commment.number_comment.
– Uroš Trstenjak
Nov 11 at 10:01
no, is not a field of commentaire, it's a variable that I want to increment when I explore a list of items with a condition if something ... (with template)
– Hafid
Nov 11 at 10:13
any help, please!
– Hafid
Nov 11 at 10:19
But you should pass this variable to the template from the view. You can't instantiate variables within the template. You could of course do it with a JavaScript, but that is something completely different. You can for example instantiate the variable in you view like that context_data['number_comment'] = 0. And than my answer would work.
– Uroš Trstenjak
Nov 11 at 10:27
|
show 3 more comments
up vote
0
down vote
accepted
If you can reference your number_comment
directly as you have it in your index.html, you can use an add
template tag to increment it. Of course this increments number only in template and doesn't save to the database.
number_comment
thanks, i already tried this line but it doesn't work :/
– Hafid
Nov 11 at 9:28
Where do you get this number_comment from? Is a field of your Commentaire model? In this case you should access it with commment.number_comment.
– Uroš Trstenjak
Nov 11 at 10:01
no, is not a field of commentaire, it's a variable that I want to increment when I explore a list of items with a condition if something ... (with template)
– Hafid
Nov 11 at 10:13
any help, please!
– Hafid
Nov 11 at 10:19
But you should pass this variable to the template from the view. You can't instantiate variables within the template. You could of course do it with a JavaScript, but that is something completely different. You can for example instantiate the variable in you view like that context_data['number_comment'] = 0. And than my answer would work.
– Uroš Trstenjak
Nov 11 at 10:27
|
show 3 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
If you can reference your number_comment
directly as you have it in your index.html, you can use an add
template tag to increment it. Of course this increments number only in template and doesn't save to the database.
number_comment
If you can reference your number_comment
directly as you have it in your index.html, you can use an add
template tag to increment it. Of course this increments number only in template and doesn't save to the database.
number_comment
answered Nov 11 at 1:49
Uroš Trstenjak
553413
553413
thanks, i already tried this line but it doesn't work :/
– Hafid
Nov 11 at 9:28
Where do you get this number_comment from? Is a field of your Commentaire model? In this case you should access it with commment.number_comment.
– Uroš Trstenjak
Nov 11 at 10:01
no, is not a field of commentaire, it's a variable that I want to increment when I explore a list of items with a condition if something ... (with template)
– Hafid
Nov 11 at 10:13
any help, please!
– Hafid
Nov 11 at 10:19
But you should pass this variable to the template from the view. You can't instantiate variables within the template. You could of course do it with a JavaScript, but that is something completely different. You can for example instantiate the variable in you view like that context_data['number_comment'] = 0. And than my answer would work.
– Uroš Trstenjak
Nov 11 at 10:27
|
show 3 more comments
thanks, i already tried this line but it doesn't work :/
– Hafid
Nov 11 at 9:28
Where do you get this number_comment from? Is a field of your Commentaire model? In this case you should access it with commment.number_comment.
– Uroš Trstenjak
Nov 11 at 10:01
no, is not a field of commentaire, it's a variable that I want to increment when I explore a list of items with a condition if something ... (with template)
– Hafid
Nov 11 at 10:13
any help, please!
– Hafid
Nov 11 at 10:19
But you should pass this variable to the template from the view. You can't instantiate variables within the template. You could of course do it with a JavaScript, but that is something completely different. You can for example instantiate the variable in you view like that context_data['number_comment'] = 0. And than my answer would work.
– Uroš Trstenjak
Nov 11 at 10:27
thanks, i already tried this line but it doesn't work :/
– Hafid
Nov 11 at 9:28
thanks, i already tried this line but it doesn't work :/
– Hafid
Nov 11 at 9:28
Where do you get this number_comment from? Is a field of your Commentaire model? In this case you should access it with commment.number_comment.
– Uroš Trstenjak
Nov 11 at 10:01
Where do you get this number_comment from? Is a field of your Commentaire model? In this case you should access it with commment.number_comment.
– Uroš Trstenjak
Nov 11 at 10:01
no, is not a field of commentaire, it's a variable that I want to increment when I explore a list of items with a condition if something ... (with template)
– Hafid
Nov 11 at 10:13
no, is not a field of commentaire, it's a variable that I want to increment when I explore a list of items with a condition if something ... (with template)
– Hafid
Nov 11 at 10:13
any help, please!
– Hafid
Nov 11 at 10:19
any help, please!
– Hafid
Nov 11 at 10:19
But you should pass this variable to the template from the view. You can't instantiate variables within the template. You could of course do it with a JavaScript, but that is something completely different. You can for example instantiate the variable in you view like that context_data['number_comment'] = 0. And than my answer would work.
– Uroš Trstenjak
Nov 11 at 10:27
But you should pass this variable to the template from the view. You can't instantiate variables within the template. You could of course do it with a JavaScript, but that is something completely different. You can for example instantiate the variable in you view like that context_data['number_comment'] = 0. And than my answer would work.
– Uroš Trstenjak
Nov 11 at 10:27
|
show 3 more comments
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%2f53245102%2fhow-can-i-increment-a-variable-with-an-if-in-a-django-template%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