I'm trying to filter objects across two templates. One (the parent) should display the five most recently updated, and the other should display all of them.
I have the latter working perfectly with the following code:
views.py:
...
class ChannelProjectList(generic.ListView):
context_object_name = 'projects_by_channel'
template_name = 'channels/projects_by_channel.html'
def get_queryset(self):
self.channel = get_object_or_404(Channel, slug=self.kwargs['slug'])
return Project.objects.filter(channel=self.channel)
...
HTML:
{% for project in projects_by_channel %}
{{project.name}}
{% endfor %}
But when I go to "include" it on the parent page it breaks. After some research I understand why that is happening and why that isnt the proper way to do it. I dug around and found this, which seems to be exactly what I'm trying to do but when I implemented it, not only did it not work it but also broke the page that is working.
This feels like a pretty simple thing, but since this is my first project I'm running into new things every day and this is one of them.
Final Solution:
With the help of this I realised I needed to copy in the same get_queryset into the second template view, which I was then able to call into the template using "view.channel_projects"
You have two possibilities. First you could define two context variables (like its done in your linked solution) or you could slice the qs in the template.
1. Option slice:
This one would display all:
{% for project in projects_by_channel %}
{{project.name}}
{% endfor %}
This on only displays 5 entries:
{% for project in projects_by_channel|slice:":5" %}
{{project.name}}
{% endfor %}
2. Option define two query sets:
(views.py)
def get_queryset(self):
self.channel = get_object_or_404(Channel, slug=self.kwargs['slug'])
self.channel2 = get_object_or_404(Channel, id=12)#whatever
context["list"] = Project.objects.filter(channel=self.channel)
context["list2"] = Project.objects.filter(channel=self.channel2)[0:5] #this slices the query set for the first entries. If you want to order them first(by date or whatever) use "order_by()"
return context
(html)
{% for project in list %}
{{project.name}}
{% endfor %}
{% for project in list2 %}
{{project.name}}
{% endfor %}
If you want to display a single qs but one time the whole thing and in another template just the first 5 you are better suited with using the slice argument in the template. It keeps the view clean and simple and you don't have to query two times.
I hope that helps if not leave a comment.
Related
I have Django model with CharFields 'flname1', 'date1', and 'time1'. My goal in my HTML is to have a {{ forloop }} that runs through only the 'date1' and 'time1' fields and displayed all of them. My Problem is that in my views file I can't find a way to create a python variable that only contains two of the three fields from one model. Ie tried a lot but what I'm trying to avoid is...
posts = DocPost.objects.all()
This puts all three fields into a variable and displays them in my for loop which I don't want. I've also tried a lot of work with filters and go things that other people on the internet had success with that didn't work for me like...
posts = DocPost.objects.filter('flname1').only('date1', 'time1')
This didn't work and didn't section off date1 and time1 and pack them away in a variable that I could loop through. Ive tried a lot more than this at this point to no prevail. Thank for any help.
There are two things you can do to only get certain fields in a query and iterate over them. The template for both is pretty much the same
First, you can use only() to generate a queryset where each object only has certain fields populated and all the rest are deferred
# View
context['posts'] = DocPost.objects.only('date1', 'time1')
# Template
{% for post in posts %}
{{ post.date1 }}
{{ post.time1 }}
{% endfor %}
Second, you can use values() to generate a queryset of dictionaries that only contain the fields specified
# View
context['posts'] = DocPost.objects.values('date1', 'time1')
# Template
{% for post in posts %}
{{ post.date1 }}
{{ post.time1 }}
{% endfor %}
I am trying to speed up my code. In development, everything ran very smooth, but once I put it in production, and started adding more depth of data into the database, I realize that it is running very slow.
I noticed on django-toolbar that it is running THOUSANDS of queries, where it should only be maybe 10-20. I am wondering if it may be because of the way I have a lot of content being delivered.
For example, I have code that looks like this:
{% if user.profile.is_admin %}
...
{% endif %}
and
{% for stuff in user.profile.get_somestuff %}
...
{{ stuff.info }}
{{ stuff.other_info }}
...
{% endfor %}
Does each one of these execute a new query?
Should I run the query for get_somestuff in the view, pass it through context? I am asking from a performance perspective.
If profile.get_somestuff is an expensive operation and you call it multiple times in the template, and yes, you should call that in the view once and pass the result to the template via context.
def view(request):
...
stuff = request.user.profile.get_somestuff()
return render(request, 'page.html', {'stuff': stuff})
Alternatively, you can use {% with %} tag to create a scope containing the value in its own context:
{% with stuff=user.profile.get_somestuff %}
...
{{ stuff.info }}
{{ stuff.other_info }}
... do some other things with stuff
{% endwith %}
Personally, I would go with the first option because, with the help of django.db.connection.queries, it is relatively easier to monitor db queries you make in the view. Make sure you avoid sending template querysets, lazy expressions etc. as much as possible.
BTW, please note that DEBUG must be set to True for connection.queries to work.
If stuff.info or stuff.other_info are foreign keys to other models then yes, each time you hit each of those for a new stuff obj you could be doing another select query for each one.
select_related might help you here. It'll effectively join the relevant tables on the fk fields you specify in the sql query upfront. The sql query will be more complex than the queries you're running now but far less numerous.
I have a page in my Django app that needs to do one of the following depending on the status of the logged in user in relation to a group (not a Django user group; something custom to my app) represented on the page:
If the user can join the group, display a link to join the group.
If the user is in the group, display a link to leave the group.
If the user can't join the group, don't display either link.
One way of doing this would be to create three templates (one with the join link, one with the leave link, and one with no link) and choose the appropriate one in the view. I feel like it may be overkill to have three different templates that will only differ in one line of code, so I have not gone that route as of yet.
Displaying the correct content for conditions 1 and 2 exclusively using a template is not possible, and if it were I do not think it would be advisable. Users to groups is a many-to-many relationship, and determining group membership requires passing a user to the group or passing a group to the user.
Since Django templates don't allow passing function arguments, I am trying to solve this by passing a context variable to the template using get_context_data.
def get_context_data(self, **kwargs):
context = super(NetworkDetails, self).get_context_data(**kwargs)
user = ???
context['in_group'] = user.in_group(context['group_detail'])
return context
If I do that, how can I get the currently logged in user within that method? If that isn't possible, where else can I get that information outside of the template? Is there an accepted method of doing something like this?
Thanks!
One way of doing this would be to create three templates (one with the
join link, one with the leave link, and one with no link) and choose
the appropriate one in the view
That's funny because if you can choose what template to include, you can just choose what html to display. Instead of:
{% if join_link %}
{% include 'join_link.html' %}
{% endif %}
{% if leave_link %}
{% include 'leave_link.html' %}
{% endif %}
{% if not join_link and not leave_link %}
you can't join
{% endif %}
You could just have:
{% if join_link %}
join
{% endif %}
{% if leave_link %}
leave
{% endif %}
{% if not join_link and not leave_link %}
you can't join
{% endif %}
So, I don't understand why you want to use template inclusion.
If I do that, how can I get the currently logged in user within that method?
self.request.user
self.request.user.is_authenticated() # return True if the user is logged in
You can determine the condition in your view and pass appropriate flag(s) to the template using context.
If there are multiple views/templates that need this info, you could implement custom context processor which can add this info in context and its available in each template.
Or If you have any OneToOne or any such relationship with User in your app, you can implement method in that model.
You can check if a user is logged in by checking permissions
https://docs.djangoproject.com/en/dev/topics/auth/ also you can do the similar for your other needs.
I have the following code in my template:
{% for req in user.requests_made_set.all %}
{% if not req.is_published %}
{{ req }}
{% endif %}
{% empty %}
No requests
{% endfor %}
If there are some requests but none has the is_published = True then how could I output a message (like "No requests") ?? I'd only like to use Django templates and not do it in my view!
Thanks
Even if this might be possible to achieve in the template, I (and probably many other people) would advise against it. To achieve this, you basically need to find out whether there are any objects in the database matching some criteria. That is certainly not something that belongs into a template.
Templates are intended to be used to define how stuff is displayed. The task you're solving is determining what stuff to display. This definitely belongs in a view and not a template.
If you want to avoid placing it in a view just because you want the information to appear on each page, regardless of the view, consider using a context processor which would add the required information to your template context automatically, or writing a template tag that would solve this for you.
How can you perform complex sorting on an object before passing it to the template? For example, here is my view:
#login_required
def overview(request):
physicians = PhysicianGroup.objects.get(pk=physician_group).physicians
for physician in physicians.all():
physician.service_patients.order_by('bed__room__unit', 'bed__room__order', 'bed__order')
return render_to_response('hospitalists/overview.html', RequestContext(request, {'physicians': physicians,}))
The physicians object is not ordered correctly in the template. Why not?
Additionally, how do you index into a list inside the template? For example, (this doesn't work):
{% for note_type in note_types %}
<div><h3>{{ note_type }}</h3>
{% for notes in note_sets.index(parent.forloop.counter0) %}
#only want to display the notes of this note_type!
{% for note in notes %}
<p>{{ note }}</p>
{% endfor %}
{% endfor %}
</div>
{% endfor %}
Thanks a bunch, Pete
As others have indicated, both of your problems are best solved outside the template -- either in the models, or in the view. One strategy would be to add helper methods to the relevant classes.
Getting a sorted list of a physician's patients:
class Physician(Model):
...
def sorted_patients(self):
return self.patients.order_by('bed__room__unit',
'bed__room__order',
'bed__order')
And in the template, use physician.sorted_patients rather than physician.patients.
For the "display the notes of this note_type", it sounds like you might want a notes method for the note_type class. From your description I'm not sure if this is a model class or not, but the principle is the same:
class NoteType:
...
def notes(self):
return <calculate note set>
And then the template:
{% for note_type in note_types %}
<div><h3>{{ note_type }}</h3></div>
{% for note in note_type.notes %}
<p>{{ note }}</p>
{% endfor %}
</div>
{% endfor %}
"I'd like to do this from within a template:"
Don't. Do it in the view function where it belongs.
Since the question is incomplete, it's impossible to guess at the data model and provide the exact solution.
results= physician.patients.order_by('bed__room__unit', 'bed__room__order', 'bed__order')
Should be sufficient. Provide results to the template for rendering. It's in the proper order.
If this isn't sorting properly (perhaps because of some model subtletly) then you always have this kind of alternative.
def by_unit_room_bed( patient ):
return patient.bed.room.unit, patient.bed.room.order, patient.bed.order
patient_list = list( physician.patients )
patient_list.sort( key=by_unit_room_bed )
Provide patient_list to the template for rendering. It's in the proper order.
"how do you index into a list inside the template"
I'm not sure what you're trying to do, but most of the time, the answer is "Don't". Do it in the view function.
The template just iterate through simple lists filling in simple HTML templates.
If it seems too complex for a template, it is. Keep the template simple -- it's only presentation. The processing goes in the view function
You should be able to construct the ordered query set in your view and pass it to your template:
def myview(request):
patients = Physician.patients.order_by('bed__room__unit',
'bed__room__order',
'bed__order')
return render_to_response('some_template.html',
dict(patients=patients),
mimetype='text/html')
Your template can then loop over patients which will contain the ordered results. Does this not work for you?
EDIT: For indexing, just use the dot syntax: mylist.3 in a template becomes mylist[3] in python. See http://docs.djangoproject.com/en/dev/ref/templates/api/#rendering-a-context for more information.
This is one way of doing it, although very ugly :
{% for note in note_sets|slice:"forloop.counter0"|first %}