Django template variables not working - django

(I am new to Django)
I'm having a problem with my Django template.
The cruise_details filter should only return one row, but when I try and display this in the template with cruise_details.port for example, nothing is displayed. "code" is correctly getting passed from the URL.
If I remove .port and just put cruise_details I am presented with this on the page
<QuerySet [<Cruise: B724>]>
view.py
def cruise(request, code):
return render(request, 'cruise.html', {
'cruise_details': Cruise.objects.filter(code=code)
})
cruise.html
{{ cruise_details.port}}
models.py
class Cruise(models.Model):
code = models.CharField(max_length=10)
destination = models.CharField(max_length=60)
url = models.URLField
ship = models.ForeignKey(Ship)
duration = models.IntegerField
start = models.DateField
end = models.DateField
added = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
port = models.CharField(max_length=30)

The issue is that Cruise.objects.filter(code=code) returns a list, so if there are many possible matches you can modify your template to show them all
{% for cruise_detail in cruise_details %}
{{ cruise_details.port }}
{% endfor %}
Alternatively, if you know there can only be one result then you can use get instead:
Cruise.objects.get(code=code)
and your existing template should work.
Hope this helps.

Related

Queryset only allowing me to SELECT just one tables fields in a join

I have two tables, but am just trying to get just the dNm from T table (while joining), but instead I can only pull fields from TSF.
I have models file:
models.py
class T(models.Model):
emailVerified = models.EmailField(max_length=50)
dNm = models.CharField(max_length=40,unique=True)
FKToUser = models.ForeignKey('auth.user', default=None, on_delete=models.PROTECT)
class TSF(models.Model):
httpResponse = models.IntegerField(validators=[MaxValueValidator(3)])
FKToT = models.ForeignKey('T', on_delete=models.PROTECT)
In regular (pseudo) sql I'm trying to do something like:
SELECT dNm
FROM T, TSF
WHERE T.id=TSF.FKToT
AND T.FKToUser=<<THE CURRENTLY SIGNED IN USER>>
However, its only allowing me to do the following in pseudo sql:
SELECT <any field from TSF>
FROM T, TSF
WHERE T.id=TSF.FKToT
AND T.FKToUser=<<THE CURRENTLY SIGNED IN USER>>
My views.py:
def viewed(request):
AUS = TSF.objects.filter(FKToTld__FKToUser=request.user).values('dNm')
return render(request, 'file.html', {
'ATFS':ATFSs
})
Outputting in template
{{ t.dNm }}
UPDATE This is now not throwing an error on page, but sending a bunch of blanks.
What am I doing wrong here?
values() returns a Queryset of dictionaries. you can check here for more info.
You can do something like this:
views.py
AUS = TSF.objects.filter(FKToTld__FKToUser=request.user).values('FKToTld__dNm').disctinct()
template.html
{% for key, value in ATFS.items %}
{{ key }}: {{ value }}
{% endfor %}

Making a proper search function in Django

I'm trying to make a search function that allows me to enter the value of an object in an instance so I can display that instance and several of it's objects on the same page as the search page. Here's what I have so far:
#models.py
class Student(models.Model):
# STEP 1 BASIC INFO
student_id = models.CharField(max_length=128, unique=True)
first_name = models.CharField(max_length=128)
last_name = models.CharField(max_length=128)
ssn = USSocialSecurityNumberField(null=False)
gender = models.CharField(max_length=128, choices=GENDER_CHOICES)
dob = models.DateField(auto_now=False, auto_now_add=False, db_column="date of birth")
contact_number = models.CharField(max_length=128)
address = models.CharField(max_length=128)
city = models.CharField(max_length=128)
state = USStateField(choices=STATE_CHOICES, default='NJ')
zipcode = USZipCodeField(blank=True)
country = CountryField(default='US', blank=True)
home_phone = models.CharField(max_length=128)
cell_phone = models.CharField(max_length=128)
email = models.EmailField(max_length=254, validators=[validate_email])
def __str__(self):
return self.first_name + self.last_name
#views.py
def search_Student(request):
context_dict = {}
if request.method == 'POST':
query = request.POST['last_name_search']
results = Student.objects.filter(last_name=query)
if query:
context_dict['results'] = results
else:
context_dict['no_results'] = query
return render(request, "students/search_student.html", context_dict)
#search_student.html
{% block main_content %}
<form method="post" action="/students/search_student/">
{% csrf_token %}
<label for="last_name_search">Last Name:</label>
<input type="text" name="last_name_search" id="last_name_search">
<input type="submit" name="submit">
</form>
<div id="result_panel">
{% if no_results %}
No results returned for <q>{{ no_results }}</q>
{% else %}
{% for result in results %}
{{ result.last_name }}
{% endfor %}
{% endif %}
</div>
{% endblock %}
#urls.py
urlpatterns = [
url(r'^$', students_views.Students, name='students'),
url(r'^add_student/$', students_views.add_Student, name='add_student'),
url(r'^id=(?P<identify>[\w]+)/add_studentcourse/$', students_views.add_StudentCourse, name='add_studentcourse'),
url(r'^id=(?P<identify>[\w]+)/add_studentemployment/$', students_views.add_StudentEmployment, name='add_studentemployment'),
url(r'test/$', students_views.test, name='test'),
#URL for the search page.
url(r'^search_student/$', students_views.search_Student, name='search_student'),
url(r'^current_student/$', students_views.current_Student, name='current_student'),
url(r'^all_my_student/$', students_views.all_My_Student, name='all_my_student'),
url(r'^public_student/$', students_views.public_Student, name='public_student'),
url(r'^sales_reports/$', students_views.sales_Reports, name='sales_reports'),
url(r'^switch_counselor/$', students_views.switch_Counselor, name='switch_counselor'),
url(r'^source_admin/$', students_views.source_Admin, name='source_admin'),
url(r'^super_tool/$', students_views.super_Tool, name='super_tool'),
url(r'^help_and_settings/$', students_views.help_And_Settings, name='help_and_settings'),
]
Basically, if I type in a last name in the input, I want it to be able to grab all model instances that have a last name equal to that, and to be able to put any information I want about all the matching instances, like the name, gender, and address on the exact same page as the search. The POST method may be confusing, but someone insists that I do this (It didn't work when I changed the method to GET either). Could someone please point out any errors or missing pieces in my code? Thanks.
Edit: Added the urls.py.
How to debug this
Remove the action from the tag. The browser will automatically post to the same URL. This rules out any issues with redirects, mappings or upstream urlconfs.
Use View Source in the browser to make sure nothing is returned, as opposed to the div being hidden by CSS or JS.
Make sure results are found:
views.py
if query:
results = Student.objects.filter(last_name=query)
if results.count():
context_dict['results'] = results
else:
context_dict['no_results'] = query
template
<div id="result_panel">
{% if no_results %}
No results returned for <q>{{ no_results }}</q>
{% else %}
{% for result in results %}
{{ result.last_name }}
{% endfor %}
{% endif %}
</div>
Note that the above cannot ever be empty. It should always show something. If it does not, it is a display error and the data is there. See point 2.
If it shows the "no results" part, then try to replicate the query in the django shell:
python manage.py shell
>>> from yourapp.models import Student
>>> Student.objects.filter(last_name='what you typed')
>>> Student.objects.filter(last_name__iexact='what you typed')
>>> Student.objects.filter(last_name__icontains='what you typed')
>>> Student.objects.count()
If none of the first three returns results, then you made a typo or number 4 will show you have no students.
SO won't let me comment quite yet, so here to say that you can indeed (and are) supposed to treat dicts like a list in django templating seen here.
Can you please post your error? I'm not sure I'm seeing whats wrong. Your method adapts over to my code just fine, so I'm a bit lost.
Editing for formatting:
You should maybe make a compile time dictionary, IE:
context_dict = {
"results": results
}
return render(request, "students/search_student.html", context_dict)
Make sure to return in the if, so the scope is in the proper place, and if your if finds nothing return a variable saying so.
If you're bent on sticking to your answer, trying referencing it in the template as so..
{% for result in results.results %}
Your dictionary has a results entry, that points to the variable results. I believe you are iterating through the dictionary in your current example, as opposed to all the queries.
Of course you can use a view method and try to find the error in your custom search and template code.
OR you can do it in a more Django way, relying on pre-built features of Django:
Use Django's ListView to query on and display the Student Model objects. You get paging, error handling, context setup for free with this. See other StackOverflow questions like https://stackoverflow.com/a/33350839/621690 for example code.
Your code that filters the Students would go into get_queryset - or you can use django-filter.
For nice usability, you can add Select2 to your form input which allows autocomplete/lookahead. See django-select2

How to target a specific object with Django templates

I have a class called Features in my models.py. In my html, I am displaying a list on the right that excludes two of these Features, one is the active feature that has been selected, the other is the most recently added since they are the main content of my page. The remaining Features in the list are displayed by date and do show what I am expecting.
Now, I want to single out the first, second and third Features (title only) in THAT list so I can place them in their own separate divs - because each has unique css styling. There are probably numerous ways of doing this, but I can't seem to figure any of them out.
This is a link to my project to give a better idea of what I want (basically trying to get the content in those colored boxes on the right.)
I'm just learning Django (and Python really), so thanks for your patience and help!
HTML
{% for f in past_features %}
{% if f.title != selected_feature.title %}
{% if f.title != latest_feature.title %}
<h1>{{ f.title }}</h1>
{% endif %}
{% endif %}
{% endfor %}
VIEWS
def feature_detail(request, pk):
selected_feature = get_object_or_404(Feature, pk=pk)
latest_feature = Feature.objects.order_by('-id')[0]
past_features = Feature.objects.order_by('-pub_date')
test = Feature.objects.last()
context = {'selected_feature': selected_feature,
'latest_feature': latest_feature,
'past_features': past_features,
'test': test}
return render(request, 'gp/feature_detail.html', context)
MODELS
class Feature(models.Model):
title = models.CharField(db_index=True, max_length=100, default='')
content = models.TextField(default='')
pub_date = models.DateTimeField(db_index=True, default=datetime.now, blank=True)
def __str__(self):
return self.title
def __iter__(self):
return [
self.id,
self.title ]
You can either store the first three Features in separate variables in your context or add checks to your template loop like {% if forloop.first %} or {% if forloop.counter == 2 %}.
If all you want is to not have the
selected_feature
latest_feature
these two records out of the past_features queryset, then you can use exclude on the past_features query and pass the id's of the selected_features and latest_feature objects.
The views.py would look like:
def feature_detail(request, pk):
selected_feature = get_object_or_404(Feature, pk=pk)
latest_feature = Feature.objects.order_by('-id')[0]
# Collect all the id's present in the latest_feature
excluded_ids = [record.pk for record in latest_feature]
excluded_ids.append(selected_feature.pk)
#This would only return the objects excluding the id present in the list
past_features = Feature.objects.order_by('-pub_date').exclude(id__in=excluded_ids)
test = Feature.objects.last()
context = {'selected_feature': selected_feature,
'latest_feature': latest_feature,
'past_features': past_features,
'test': test}
return render(request, 'gp/feature_detail.html', context)
Django provides a rich ORM and well documented, go through the Queryset options for further information.
For access to a specific object in Django templates see following example:
For access to first object you can use {{ students.0 }}
For access to second object you can use {{ students.1 }}
For access to a specific field for example firstname in object 4 you can use {{ students.3.firstname }}
For access to image field in second object you can use {{ students.1.photo.url }}
For access to id in first object you can use {{ students.0.id }}

For loop in views

In views I want to get car-object, then change first_year var.
def cars(request, mark_id, year=year):
#BMW, etc.
mark = get_object_or_404(Mark, pk=mark_id, active=1)
#M, X-series, etc.
for group in mark.groups.iterator():
group.first_year.year = int(year)-int(group.first_year.year)
return render(request, 'cars.html', { 'mark':mark, })
So, in template I use:
{% for i in mark.groups.all %}
{{i.first_year}}
{% endfor %}
And it returns the values from db, not my group.first_year.year. As you can understand, there is 3 models - mark, groups and first_year. If you need, I can publish they here, but I think, that something wrong in my views.
Thanks.
Edit. Models.
class First_Year(models.Model):
year = models.IntegerField()
def __unicode__(self):
return str(self.year)
class Groups(models.Model):
many_other_fields
mark = models.ForeignKey(Mark, related_name='groups')
last_update = models.DateTimeField()
first_year = models.ForeignKey(First_Year, related_name='first_year')
def __unicode__(self):
return self.name
def altered_date(self, year):
altered_year = int(year)-int(self.first_year.year)
return altered_year
Without model Mark, bacause it has many-many fields without year and etc.
The for loop making changes in your view is not saving the changes anywhere so you will not see the changes when the values are passed to your template.
One solution here is to add a new model method to your model and do the date comparison there.
In your First_Year model add the altered_date function like so:
class First_Year(models.Model):
year = models.IntegerField()
def __unicode__(self):
return str(self.year)
def altered_date(self, this_year):
altered_year = int(this_year)-int(self.year)
return altered_year
This gets the altered year for every First_Year model by calling the function. Unfortunately this function requires an additional parameter - year - so it cannot be called directly from the template. You can create a custom template filter to get around this:
#register.filter
def get_altered_year(obj, gar):
return obj.altered_date(gar)
Now you just need to make sure that you pass year to your view by modifying your return like so:
def cars(request, mark_id, year=year):
...
return render(request, 'cars.html', {'mark':mark, 'year':year, })
And then in your template you can do:
{% for i in mark.groups_set.all %}
{{i.first_year|get_altered_year:year }}
{% endfor %}
You can look at the model method documentation here. And the documentation for custom template filters here.
You mean:
{% for i in mark.groups_set.all %}
{{i.first_year}}
{% endfor %}
This will only allow if groups has a foreign key from mark

Merging querysets from different models

I have 2 models in one app and 1 view. I'm currently pulling information from 1 model perfectly fine. However i wish to pull in another model from the same app and output them both to the same page.
The idea of the page is it being a a news hub so it's pulling through different types of news posts (from one model) and a different type of post which is from the other model.
I'm fairly new to Django so go easy! :) Anyway here is the code:
//VIEWS
def news_home(request):
page_context = details(request, path="news-hub", only_context=True)
recent_posts = NewsPost.objects.filter(live=True, case_study=False).order_by("-posted")[:5]
recent_posts_pages = Paginator(recent_posts, 100)
current_page = request.GET.get("page", 1)
this_page = recent_posts_pages.page(current_page)
notes = BriefingNote.objects.filter(live=True).order_by("-posted")
news_categories = NewsCategory.objects.all()
news_context = {
"recent_posts": this_page.object_list,
"news_categories": news_categories,
"pages": recent_posts_pages,
"note": notes,
}
context = dict(page_context)
context.update(news_context)
return render_to_response('news_hub_REDESIGN.html', context, context_instance=RequestContext(request))
//model 1
class BriefingNote(models.Model):
title = models.CharField(max_length=300)
thumbnail = models.ImageField(upload_to='images/briefing_notes', blank=True)
file = models.FileField(upload_to='files/briefing_notes')
live = models.BooleanField(help_text="The post will only show on the frontend if the 'live' box is checked")
categories = models.ManyToManyField("NewsCategory")
# Dates
posted = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __unicode__(self):
return u"%s" % self.title
// model 2
class NewsPost(models.Model):
title = models.CharField(max_length=400)
slug = models.SlugField(help_text="This will form the URL of the post")
summary = models.TextField(help_text="To be used on the listings pages. Any formatting here will be ignored on the listings page.")
post = models.TextField(blank=True)
#TO BE REMOVED????
thumbnail = models.ImageField(help_text="To be displayed on listings pages", upload_to="images/news", blank=True)
remove_thumbnail = models.BooleanField()
I'm outputting the content on the front end like so:
{% for post in recent_posts %}
<div class='news_first'>
<img class="news_thumb" src="/media/{% if post.article_type %}{{post.article_type.image}}{% endif %}{% if post.news_type %}{{post.news_type.image}}{% endif%}" alt="">
<h3><a href='{{post.get_absolute_url}}'>{% if post.article_type.title %}{{post.title}}{% endif %} <span>{{post.posted|date:"d/m/y"}}</span></a></h3>
<p class='news_summary'>
{% if post.thumbnail %}<a href='{{post.get_absolute_url}}'><img src='{% thumbnail post.thumbnail 120x100 crop upscale %}' alt='{{post.title}}' class='news_thumbnail'/></a>{% endif %}{{post.summary|striptags}} <a href='{{post.get_absolute_url}}'>Read full story ยป</a>
</p>
<div class='clearboth'></div>
</div>
{% endfor %}
I was thinking perhaps i could output them both within the same forloop however they need to ordered by -posted. So i though this could mess things up.
If you need anymore info please let me know.
Thanks in advance.
I've now solved the problem.
news_hub = list(chain(notes, recent_posts))
news_hub = sorted(
chain(notes, recent_posts),
key = attrgetter('posted'), reverse=True)[:10]