I am trying to call a python view method res() on django template but its not getting call.
This is my View
class make_incrementor(object):
def __init__(self, start):
self.count=start
def __call__(self, jump=1):
self.count += jump
return self.count
#property
def res(self):
self.count =0
return self.count
def EditSchemeDefinition(request, scheme_id):
iterator_subtopic = make_incrementor(0)
scheme_recs = scheme.objects.get(id=scheme_id)
view_val = {
'iterator_subtopic': iterator_subtopic,
"scheme_recs": scheme_recs,
}
return render(request, "edit.html", view_val)
I am Trying to Increment the count and later resetting it to 0 after a level But its reset method is not getting call from the Django template.
This is my edit.html page
<td id="subTopic" class="subTopic">
{% for strand in scheme_recs.stand_ids.all %}
{{ iterator_subtopic.res }}
{% for sub_strand in strand.sub_strand_ids.all %}
{% for topic in sub_strand.topic_ids.all %}
{% for subtopic in topic.sub_topic_ids.all %}
<input id="subTopic{{ iterator_subtopic }}" class="len"
value="{{ subtopic.name }}">
<br>
{% endfor %}
{% endfor %}
{% endfor %}
{% endfor %}
</td>
my {{ iterator_subtopic.res }} is not getting the call and the value of iterator_subtopic is not get set to 0 again. The Function and its call is working fine on Terminal but not rendering in django template.
Please correct me where i am doing wrong.
Thanks in advance.
Since your make_incrementor class has a __call__ method, I believe that {{ iterator_subtopic.res }} will first call iterator_subtopic(), which will return the count integer. It will then try to access the res attribute of the count integer (which doesn't exist), so it will output the empty string ''.
I simply modify my make_incrementor class methods like:-
class make_incrementor(object):
count = 0
def __init__(self, start):
self.count = start
def inc(self, jump=1):
self.count += jump
return self.count
def res(self):
self.count = 0
return self.count
def EditSchemeDefinition(request, scheme_id):
iterator_subtopic = make_incrementor(0)
scheme_recs = scheme.objects.get(id=scheme_id)
view_val = {
'iterator_subtopic': iterator_subtopic,
"scheme_recs": scheme_recs,
}
return render(request, "edit.html", view_val)
Later in my django template I called its method like:-
<td id="subTopic" class="subTopic">
<p hidden value="{{ iterator_subtopic.res }}"></p>
{% for strand in scheme_recs.stand_ids.all %}
{{ iterator_subtopic.res }}
{% for sub_strand in strand.sub_strand_ids.all %}
{% for topic in sub_strand.topic_ids.all %}
{% for subtopic in topic.sub_topic_ids.all %}
<input id="subTopic{{ iterator_subtopic.inc }}" class="len"
value="{{ subtopic.name }}">
<br>
{% endfor %}
{% endfor %}
{% endfor %}
{% endfor %}
And its done. Now i am getting my expected output.
Related
I have created a custom html template with basic checkboxes to select a value and return the value to the Django admin page.
The value of the selected superprofile does not get captured by the variable "selected_value" in the admin.py
The if statement "if request.method == 'POST':" is getting triggered but i keep getting the value of "selected_value" as none
The Html template
{% extends "admin/base_site.html" %}
{% load i18n admin_urls static admin_modify %}
{% block extrahead %}
{{ media }}
{% endblock %}
{% block content %}
<form class="change_superprofile_parent_form" met`your text`hod="POST" class="change_superprofile_parent_form">{% csrf_token %}
{% for superprofile in superprofiles %}
<input type="checkbox" name="superprofile_selected" {{ superprofile.checked }} value="{{ superprofile }}"> {{ superprofile }}<br>
{% endfor %}
<input type="submit" value="Submit">
</form>
{% endblock %}
Django admin.py
def change_superprofile_parent(self, request, queryset):
"""
Action to change the superprofile of the selected
"""
queryset = queryset.values_list("id", flat=True)
if request.method == 'POST':
selected_value = request.POST.getlist('superprofile_selected')
eligible_superprofiles = SuperProfile.objects.filter(status='Active')
return render(
request, 'admin/auth/user/change_superprofile_parent.html', context={
'superprofiles': eligible_superprofiles,
}
)
I have created a custom html template with basic checkboxes to select a value and return the value to the Django admin page.
I have done a 100 times before, but now the value of the selected superprofile does not get captured by the variable "selected_value" in the admin.py
The if statement "if request.method == 'POST':" is getting triggered but i keep getting the value of "selected_value" as none
Driving me crazy, I cannot find anything wrong in the code
The Html template
{% extends "admin/base_site.html" %}
{% load i18n admin_urls static admin_modify %}
{% block extrahead %}
{{ media }}
{% endblock %}
{% block content %}
<form class="change_superprofile_parent_form" method="POST" class="change_superprofile_parent_form">{% csrf_token %}
{% for superprofile in superprofiles %}
<input type="checkbox" name="superprofile_selected" {{ superprofile.checked }} value="{{ superprofile }}"> {{ superprofile }}<br>
{% endfor %}
<input type="submit" value="Submit">
</form>
{% endblock %}
Django admin.py
def change_superprofile_parent(self, request, queryset):
"""
Action to change the superprofile of the selected
"""
queryset = queryset.values_list("id", flat=True)
if request.method == 'POST':
selected_value = request.POST.getlist('superprofile_selected')
eligible_superprofiles = SuperProfile.objects.filter(status='Active')
return render(
request, 'admin/auth/user/change_superprofile_parent.html', context={
'superprofiles': eligible_superprofiles,
}
)
So I am doing a django project. I have sent a dictionary(which holds a foreign key) to my html template. It has 'name'(which is a foreignkey),'bills','flat' keys. So when I am trying to print the name of a user it is actually printing the id of the user. But I want the name. I have used users = User.objects.all().values('name','bills','flat') and then passed the users in the template. The I tried to print like this:
{% for info in users %} {{info}} {% endfor %}
But instead of giving the name it is giving me the id number of the user.
My models.py file:
`
class Owner(models.Model):
name = models.CharField(max_length=100)
email = models.CharField(max_length=100)
class Payment(models.Model):
name=models.ForeignKey(Owner,on_delete=models.CASCADE,null=True)
flat = models.CharField(max_length=1,null=True,blank=True)
floor = models.IntegerField(default=0)`
My views.py file:
`
info = Payment.objects.all()
dict = {'1':[info.values('name','flat','floor','water_bill')]}
`
My Template file:
`
{% for j in dict.0 %}
<tr>
{% for i in j.values %}
<td>{{i}}</td>
{% endfor %}
</tr>
{% endfor %}
`
The return statement should look something like this,
return render_template(file.html, dict = dict)
Then you can use the dict variable in jinja as follows,
{% for j,k in dict %}
<tr>
{% for i in j.values %}
<td>{{i}}</td>
{% endfor %}
</tr>
{% endfor %}
I am creating a to do list. I want to display the tasks if the user has any. If not, then display something else. I kept the design simple.
<h2>Here is the list of tasks! Start working!</h2>
{% if obj in task %}
<ul>
{% for obj in task %}
<li>{{ obj }}</li>
{% endfor %}
</ul>
{% else %}
<p>You dont have anything on this list yet!</p>
{% endif %}
The 'task' is the queryset and currently consists of 2 objects. But none of them are being displayed. Everything was working fine before I tried to apply the presence check. Now it just jumps to that else statement.
views.py:
def task(request):
task = Task.objects.filter(user=request.user)
queryset = task.order_by('-start_date')
context = {
'task': queryset,
}
return render(request, 'task-list.html', context)
Try in this way
<h2>Here is the list of tasks! Start working!</h2>
{% if task %}
<ul>
{% for obj in task %}
<li>{{ obj }}</li>
{% endfor %}
</ul>
{% else %}
<p>You dont have anything on this list yet!</p>
{% endif %}
Its {% if task %} not {% if obj in task %}
Hope this helps you, If anything please let me know
As the title says, here's what I've got:
form = F(obj = myobject)
myfieldlist= FieldList(FormField(form))
{% for subfield in form.myfieldlist %}
{{ subfield.field }}
{{ subfield.label }}
{% endfor %}
This outputs nothing, any ideas? Also, not entirely sure if FormField is required.
Thanks
FormField takes a class not an instance:
class GuestForm(Form):
email = TextField()
vip = BooleanField()
class VenueForm(Form):
name = TextField()
guests = FieldList(FormField(GuestForm))
Then in your controller:
form = VenueForm(obj=myobject)
render("template-name.html", form=form)
In your template you will need to iterate over the FieldList field as if it was its own form:
{% for guest_form in form.guests %}
<ul>
{% for subfield in guest_form %}
<li>{{ subfield.label }} {{ subfield }}</li>
{% endfor %}
</ul>
{% endfor %}