here's what my models look like:
http://i.imgur.com/fFBqq.png
I'm trying to fill out a table full of disks, each has a serial and model number. I'm going to assume I'll do something like {% for disks in "something" %}, but I'm not quite sure what that something would be.
Here's what i was hoping for:
<table>
<thead>
<tr>
<th>Serial Number</th>
<th>Model Number</th>
</tr>
</thead>
<tbody>
{% for disks in "something" %}
<tr>
<td>{{ disk.serial }}</td>
<td>{{ disk.model }}</td>
</tr>
{% endfor %}
The template is only part of your issue. It's actually the less complicated aspect, as all you are doing is passing it a context (a dictionary) for it to access. The step before the template is the view that is organizing the data. Lets start with that...
View
The function (view) that gathers the data needs to build a context containing your "disk" objects, assumably a result of a database model query. For simplicity, lets just say you did this:
disks = Disk.objects.all()
With your disks queryset, you can now deliver that to your template in the context.
context = {"disks": disks}
return render_to_response('my_template.html', context)
The context will now be passed to your template.
Template
Simply refer to the objects in your context:
{% for disk in disks %}
<tr>
<td>{{ disk.serial }}</td>
<td>{{ disk.model }}</td>
</tr>
{% endfor %}
#jdi is right, but since this is something very commonly done in web development - there is a generic view for it.
In your urls.py:
from django.conf.urls import patterns, url, include
from django.views.generic import ListView
from myapp.models import Disk
urlpatterns = patterns('',
(r'^disk_list/$', ListView.as_view(
model=Disk,
template_name='disk_list.html'
)),
)
Create a file called disk_list.html, that is any directory listed in TEMPLATE_DIRS, and in it add this:
<table>
<thead>
<tr>
<th>Serial Number</th>
<th>Model Number</th>
</tr>
</thead>
<tbody>
{% for disk in object_list %}
<tr>
<td>{{ disk.serial }}</td>
<td>{{ disk.model }}</td>
</tr>
{% endfor %}
Finally, navigate to http://localhost:8000/disk_list/
Related
order history is not displayed on orders_list.html file.
order_list.html
{% if order_details %}
<table class="table table-striped">
<thead>
<tr>
<th>Order Number</th>
<th>Order Date</th>
<th>Total Amount</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{% for order in order_details %}
<tr>
<td>{{ order.id }}</td>
<td>{{ order.created|date:"d M Y" }}</td>
<td>{{ order.total }}</td>
<td><i class="fas fa-check"></i></td>
<td>View Order</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>
You have not placed any orders yet.
<br><br>
Continue shopping
</p>
{% endif %}
views.py file
def orderHistory(request):
global order_details
if request.user.is_authenticated:
email = str(request.user.email)
order_details = Order.objects.filter(emailAddress=email)
return render(request, 'orders_list.html', {'order_details': order_details})
the result to above is
ORDER HISTORY
You have not placed any orders yet.
Continue shopping
this shows that the if statement always returns false while there are rows of orders in order table.
i expect the result to be the list of orders placed from the account.
Most of the time this has something to do with
order_details = Order.objects.filter(emailAddress=email)
You have to check if this queryset returns any value at all. Because when you use filter if queryset is empty you won't get any error and this may be the reason why you can't display anything.
However, you will need to use some simple print statements in your view to check the queryset.
If the queryset has values then try adding
return render(request,
'orders_list.html',
{'order_details': Order.objects.filter(emailAddress=email)})
as a last try.
First of all, I know there are lot of questions about accessing model in template but let me explain why this is different.
I want profile page where User can see their details. I am able to do that but with one little bug. If there are 3 Users(say A, B,C) and User A want to see his profile he sees it three time. Like this:
How do I stop the loop after one iteration so that the User only get's his profile information once.
This is my URL:
url(r'^profile/$', views.IndexView.as_view(), name='profile'),
Views.py:
class IndexView(generic.ListView):
template_name = 'profile.html'
def get_queryset(self):
return Profile.objects.all()
and profile.html:
{% if user.is_authenticated %}
{% for profile in object_list %}
<h1>{{ request.user }}'s Profile</h1>
<table>
<tr>
<td>Username:</td>
<td>{{ user.username }}</td>
</tr>
<tr>
<td>First Name:</td>
<td>{{ user.first_name }}</td>
</tr>
<tr>
<td>Last Name:</td>
<td>{{ user.last_name }}</td>
</tr>
<tr>
<td>Email:</td>
<td>{{ user.email }}</td>
</tr>
<tr>
<td>Personal Info:</td>
<td>{{ user.profile.personal_info }}</td>
</tr>
<tr>
<td>Job Title:</td>
<td>{{ user.profile.job_title }}</td>
</tr>
<tr>
<td>Department:</td>
<td>{{ user.profile.department }}</td>
</tr>
<tr>
<td>Location:</td>
<td>{{ user.profile.location }}</td>
</tr>
<tr>
<td>Expertise:</td>
<td>{{ user.profile.expertise }}</td>
</tr>
<tr>
<td>Phone Number:</td>
<td>{{ user.profile.phone_number }}</td>
</tr>
<tr>
<td>Contact Skype:</td>
<td>{{ user.contact_skype }}</td>
</tr>
<tr>
<td>Contact Facebook:</td>
<td>{{ user.contact_facebook }}</td>
</tr>
<tr>
<td>Contact Linkedin:</td>
<td>{{ user.profile.contact_linkedin }}</td>
</tr>
</table>
<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% include 'form-template.html' %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<a href={% url 'profile_edit' %}><input type="button" class = " col-sm-offset-2 btn bk-bs-btn-warning " name="cancel" value="Edit" /></a>
</div>
</div>
</form>
{% endfor %}
{% else %}
<h2>Please login to see your Profile</h2>
{% endif %}
I am a newbie to django, thanks is advance.
You're looping over all Profile's but not actually using this data in the loop, instead you're using user.profile.
This means that you likely have 3 Profile objects in the database, then for each you display the details of the current user, which isn't desirable.
So you can remove the loop entirely, and just keep its contents that refer to all the user.profile attributes to achieve the desired result.
edit
Looks like user is passed into your template by default, and points to the currently logged in user. So user.profile will return the profile, and this works without doing anything else. So user.profile.personal_info is valid and should work. When you tried to use profile directly, that is not the same thing, and wasn't defined, so profile.personal_info didn't work. Then in your loop you used profile to loop over Profile objects, but this wasn't necessary or was used. Hope this makes sense.
I am kinda stuck.
Here is the situation. I have 2 for loops. One that loops through categories and the other one that loops through a match set. Here is the problem:
I need to get all the matches from that category...
Here is what I have:
{% for category in tournament.get_categories %}
<div class="line" style="margin-top: 25px; margin-bottom: 40px;"></div>
<div class="container">
<h3 class="margin-reset" id="{{ category.slug }}">{% trans "Matches schedule" %}<span> | {{ category.name }}</span></h3>
<table class="standard-table table">
<thead>
<tr>
<th>Match</th>
<th>Heure</th>
<th>Plateau</th>
<th>Équipe bleu</th>
<th>Équipe gris</th>
<th>Équipe noir</th>
<th>Résultat</th>
</tr>
</thead>
<tbody>
{% for match in tournament.match_set.filter(category=category) %}
<tr>
<td>{{ match.match_num }}</td>
<td>{{ match.time }}</td>
<td>{{ match.plateau }}</td>
<td>{{ match.blue_team.name }}</td>
<td>{{ match.grey_team.name }}</td>
<td>{{ match.black_team.name }}</td>
<td>{{ match.get_url_string }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endfor %}
As you guys probably guessed it, I get this error:
Could not parse the remainder: '(category=category)' from 'tournament.match_set.filter(category=category)'
What can I do?
Thanks,
Ara
EDIT:
Here is the solution:
The custom tag:
#register.filter
def get_matches_by_category(value, category):
return value.match_set.filter(category=category)
And the use:
{{ tournament|get_matches_by_category:category }}
I created a custom templatetag and used that filter.
It's kinda an overkill but...oh well.
What I have done in a very similar case is pass the list of categories to the template, adding them a new attribute with the matches, like:
for category in categories:
category.matches = Match.objects.filter(tournament=tournament, category=category)
It's kinda slow, but you can work it and reduce the number of queries
I would pass the categories list to the template after this
I have a question regarding counting objects, filtering the results and finally putting them into my template.
in Models.py:
class Fms(models.Model):
department = models.CharField(max_length=255, verbose_name='Department')
date = models.DateField()
in Views.py:
def raport(request):
raport = Fms.objects.filter(date__year='2013').extra(select={'month': "EXTRACT(month FROM date)"}).values('month', 'department').annotate(Count('department'))
return render_to_response ('Fms/raport.html',
{'raport': raport},
context_instance=RequestContext(request))
Question is, how to show a result in a table like:
Or at least make it show each month and how many times department was mentioned in that month.
you need something like:
{% for each_model in raport %}
{{each_model.department}}
{{each_model.date}}
{% endfor %}
If you do not pass a keyword to annotate, it uses the name of the field with __count appended to it. https://docs.djangoproject.com/en/dev/topics/db/aggregation/#generating-aggregates-for-each-item-in-a-queryset To access that in your template you would simply do:
<table>
<thead>
<tr>
<th>Month</th>
<th>Count</th>
</tr>
</thead>
<tbody>
{% for item in raport %}
<tr>
<td>{{ item.month }}</td>
<td>{{ item.department__count }}</td>
</tr>
{% endfor %}
</tbody>
</table>
To display it horizontally...
<table>
<thead>
<tr>
{% for item in raport %}
<th>{{ item.month }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
<tr>
{% for item in raport %}
<td>{{ item.department__count }}</td>
{% endfor %}
</tr>
</tbody>
</table>
I have dict with hyderlink, example :
data = [{a:<\a href="http://someexample.com/a">a</a>,
b:'<\a href="http://someexample.com/b">b</a>'}]
Note : here i have add /a href because stack overflow takes it has hyperlink
if i want to output this in html , it displays a normal html text instead of hyperlink
template
<table>
{% for fetch in data %}
<tr>
<td>{{ fetch.a }}</td>
<td>{{ fetch.b }}</td>
</tr>
{% endfor %}
</table>
it gives output like html text instead of hyperlink
<\a href="http://someexample.com/a">a
<\a href="http://someexample.com/b">b
any help it really appreciate it.
Instead of storing the entire anchor tag, you should be storing just the URL (using URLField if you're storing it in a model), and then include it in your template as follows:
<table>
{% for fetch in data %}
<tr>
<td>{{ fetch.a }}</td>
<td>{{ fetch.b }}</td>
</tr>
{% endfor %}
</table>
This is happening because of automatic string escaping in the template engine. You can prevent escaping with the safe filter, like:
<table>
{% for fetch in data %}
<tr>
<td>{{ fetch.a|safe }}</td>
<td>{{ fetch.b|safe }}</td>
</tr>
{% endfor %}
</table>