How to get "price_retail" field value in tempate? - python

I am trying access price_retail field in product detail page using template tag purchase_info_for_product. but am not getting the value of price_retail.
{% purchase_info_for_product request product as session %}
{{ session.price.price_retail|currency:session.price.currency }}
but i am able to access fields like incl_tax, excl_tax

It looks like you're trying to access a field from the stock record - this is not the same as the Price object that the strategy provides.
The stock record is available on the parent object though, and can be accessed with:
session.stockrecord.price_retail
Note: The price_retail field will be removed in Oscar 2.1

Related

Unable to retrieve Category name using templates in Django

I am working on a Django project and I want to retrieve the category name in my template like Adventure, Hiking.. but instead, it's displaying ids of the category like 1,2,3. Instead of displaying the name of the category. Can someone help me out with this?
{% for data in tourData %}
{{data.category}}
{% endfor %}
models.py
class Tour(models.Model):
category_choices=[('1','Adventure'),('2','Trekking'),('3','Hiking')]
category=models.CharField(max_length=1,choices=category_choices,default='1')
view.py
def recommendations(request):
if request.method=='POST':
contents=Tour.objects.all()
category1= request.POST['category'] #Retrieves the category entered by the user
category2=request.POST['place']
tourData = Tour.objects.all().filter(category=category1,place=category2).order_by('-rating').values()
context={
'tourData':tourData
}
return render(request,"base/recommendations.html",context)
else:
tourData=Tour.objects.all().order_by('-rating').values()
context={'tourData':tourData
}
return render(request,"base/recommendations.html",context)
You need to use get_field_display in your template, i.e.
{{ data.get_category_display }}
This will show the second tuple value in your model choices rather than the first (the first is what actually goes into the database).
As an aside, I would recommend changing the format of your tourData variable from camelCase to snake_case - tour_data - it's more pythonic.

How to use Django session variable in models.py

I wanna use the Django session variable on models.py file. How can I do it?
I want to get the user's Pincode/zip and have to run a query using that.
pin_code = #wanna get from session
price = ProductPrice.objects.get(product=self.pk, pincode=pin_code)
I need to get Pincode from the session.
In your model method you don't have access to the request so you do not have access to the session. One way that you could call the method with the session is to add a template tag that passes the value from the session to your method
#register.simple_tag(takes_context=True)
def price(context, product):
return product.get_price(pin_code=context.request.session.get('pin_code'))
In your template you would use the following, if the above code was added to a file with the path app_name/templatetags/product_tags.py
{% load product_tags %}
{% price item %}
Docs on where to put custom template tags

Using Foreign Keys in Django Template w/django-simple-history

I'm trying to display values associated with a foreign key while rendering a Django Template. I've emulated other answers on this site to no avail.
I'm using the package django-simple-history to track changes to all the records in my database's primary table. This table has a foreign key named history_user_id which corresponds to the id in the django table auth_user.
According to this example (Display foreign key value in django template) I should be able to display the usernames of users who have amended the database by using the following code:
<ul>{% for item in history %}
<li>{{item.history_user_id.username}}</li>
</ul>{% endfor %}
where history is defined in my views.py as
def view_history(request,pk):
project = Project.objects.get(pk=pk)
history = project.history.all()
return render(
request,
"projects/view_history.html",
{
"project": project,
"history": history,
}
)
The template I create can interpret item.history_user_id, and I can manually look into the table auth_user to the corresponding username, but when I try to use the template to render the username I get a blank instead. Am I missing a step?
If I see it correctly item.history_user_id is the id of the user who did the change. This is an integer, which of course does not have any property called user. If a django template hits a non-existant variable it will just leave it blank (and not raise an error). Therefore you a seeing nothing in your template.
So change your code to get the user rather than the user_id
<ul>{% for item in history %}
<li>{{item.history_user}}</li>
</ul>{% endfor %}
This is described in the docs.

Django: How to return all models associated with a ForeignKey including all attributes of those models?

I have an app that I want to simply display all the URL links a page has associated with it when that page is visited.
It's similar to reddit in that there are many userpages (aka subreddits) and each page has an infinite possible amount of submitted links associated with it. The newlinkposts records are associated with a certain page via a ForeignKey.
Given a page, wow can I get all the related newlinkpost objects (including their corresponding likes, link comment, and post date) returned, in order to display them in a template?
My newlinkpost object is defined as follows:
class newlinkpost(models.Model):
newlink_tag = models.ForeignKey('userpagename') #tags link to which userpage it belongs to
link_comment = models.CharField(max_length=128) #comment to go along with post
post_date = models.DateField(auto_now=True, auto_now_add=False, null=False) #submission datestamp. later make it track editable posts if edit function is implemented
url = models.URLField(max_length = 1024, null=False) #actual submitted link
link_likes = models.IntegerField(null=False, default=0) #need to later create like button which will +1 the value
def __unicode__(self):
return self.url
When you add a ForeignKey within a model, as well as creating an attribute in the source model (in your case, newlinkpost) allowing you to find the one associated object, Django also creates a corresponding attribute inside the target model (in your case apparently userpagename).
By default this attribute is named after the source table, so in your case it will be newlinkpost_set.
That allows you to ask the question you're looking to answer: which newlinkpost objects have this userpagename?:
all_links = userpagename_instance.newlinkpost_set.all()
If you wish to apply additional filters, you can use the filter method instead:
some_links = userpagename_instance.newlinkpost_set.filter(...)
The newlinkpost_set attribute contains a RelatedManager object, which is a subtype of Manager, allowing you to use the same set of methods you could use on newlinkpost.objects, along with some additional methods allowing you to create new related objects.
Here's an example view using this technique: (this assumes you've got the model classes imported into the views module):
from django.shortcuts import render
def user_page(request, user_id):
page = userpagename.get(pk=user_id)
links = page.newlinkpost_set.all()
return render(
request,
"myapp/user_page.html",
{
page: page,
links: links,
}
)
...and here's an example of using that "links" variable in the template:
<ul>
{% for link in links %}
<li><a href="{{ link.url }}">{{ link.link_comment }} - {{ link.link_likes }} likes</li>
{% endfor %}
</ul>
You just use the reverse relationship.
my_userpagename.newlinkpost_set.all()

How to display doc id of results returned by search api in django template?

I am using app engine search api to search within documents. the results returned are displayed on the django template like this
{% for item in results %}
{{item.id.0.value}}
{{item.name.0.value}}
{% endfor %}
i am able to display all the fields in template except the doc_id of the result. can someone please help me to figure how to display doc_id in the template?
According to the documentation:
...you can examine the id attribute of the result to discover the
doc_id that was generated.
So the .id property in your example is in fact the doc_id you are looking for.

Categories

Resources