Template Inheritance doesnot work in Django - html

I have base.html (which is my home page). It has navbar which is in navbar.html and some sliders in base.html itself. I have created ProductList view which render context(i.e products), which is used in product_list.html to make values dynamic.
Now my problem is I don't want to include my ProductList view in my product.urls because i don't want it to be rendered in new page. Instead I want to include product_list.html in base.html inorder to show it in my home page rather than in product page just like in normal ecommerce site.
So, I didn't include ProductList in product.urls and included that template in base.html using include template-tag. But it didn't show my products. When i tried to render products in separate page it works.
Is there any method to do this?

I think you forget to pass the context.In your views.py, pass the context to the function which renders the base.html
For example:
def index(request):
context = {'product':products} #Pass your products
return render(request,'base.html',context)

Related

How can I differentiate between two url requests from different HTML pages but with the same namespace in views.py?

I am creating a simple eBay like e-commerce website to get introduced with django. For removing an item from the watchlist, I placed two same links in two different HTML files, that is, I can either remove the item from the watchlist.html page or either from the item's page which was saved as listing.html. The url for both the pages look like this:
Remove from watchlist
Now, in my views.py, I want to render different pages on the basis of the request. For example, if someone clicked Remove from watchlist from listing.html then the link should redirect again to listing.html and same goes for the watchlist.html.
I tried using request.resolver_match.view_name but this gave me 'removeFromWatchlist' as the url namespace for both of these request is same.
Is there any way I can render two different HTML pages based on the origin of the url request?
Also, this is my second question here so apologies for incorrect or bad formatting.
You could check the HTTP_REFERER in the request.META attribute of the view to get the url that referred the request as so:
from django.shortcuts import redirect
def myview(request):
...
return redirect(request.META.get("HTTP_REFERER"))#Or however you prefer redirecting
https://docs.djangoproject.com/en/3.1/ref/request-response/#django.http.HttpRequest.META

Django-CMS admin tool Template

I have created html template named testTemplate which is added to CMS_TEMPLATES:
CMS_TEMPLATES = (
## Customize this
('fullwidth.html', 'Fullwidth'),
('sidebar_left.html', 'Sidebar Left'),
('sidebar_right.html', 'Sidebar Right'),
('testTemplate.html', 'testTemplate')
)
Then in fullwidth.html file I have changed the {% extends "base.html" %} to {% extends "testTemplate.html" %}. In testTemplate.html file I can find few {% placehoders %}. And now if I want to go to django-cms to add a content to placeholders, my cms-admin does not look right. Layout of cms admin looks like the template elements. How to separate the cms-admin template and my site template to make it work correctly?
I'm not sure I completely understand your issue. Are you wanting to use testTemplate.html instead of base.html or are you wanting to insert testTemplate.html between base.html and fullwidth.html?
If you want to replace base.html, then I would simply save testTemplate.html in the same location as base.html and not bother registering it with CMS_TEMPLATES. Registering with CMS_TEMPLATES simply makes these templates available to your pages in cms-admin. If testTemplate.html is not a final template that you will want to use then you don't need to register it here. Note that base.html is not registered here for that very reason. Another approach would be to rename base.html as something else and renaming testTemplate.html to base.html, effectively replacing it if you don't want to use the original, which means you wouldn't need to change any of the dependant templates.
If you are trying to insert testTemplate.html between base.html and fullwidth.html, that is a different kettle of fish. You will need to make sure you have all your blocks nested correctly and you'll need to post some more details like your html code for each page for people to be able to assist you.

Is there any way to show all the components data from /jcr:content/par/ location

I have a query regarding the data rendering of the different page at one place. As every page is build using many components and all the components data gets stored under jcr location of page ie. /jcr:content/par/{components list}. The data is properly rendering on this page.
Now I have a situation where I need to create a component to search the page(i.e unique product), if this product page is available in the repository, I need to render its data just under the search box. For this I am creating json which I will use to render the content after search found.
But if there is any other way i can include this component from the /par location of the page to just display the data as it is, rather than building json(of all the components data) and then reading it at the time of display.
I am wondering if we have any method to display all the components data by just including the /par/{components} on a page. This way I can speed up the development, and it looks faster way to display the content as well.
thanks in advance....
If you have static page, then you can go through list of search results (product resources) and include component, which renders product, for each of them. Like:
<c:forEach items="${productsList}" var="productPath">
<cq:include path="${productPath}" resourceType="/apps/you-project/components/product-component-name"/>
</c:forEach>
If you show results dynamically - then you can do ajax requests for product resources. Something like this:
var productHtml = CQ.shared.HTTP.get(productPath + ".html");
Or the same using JQuery. Then you can add html to your page.
However, with second approach you should add clientlibs from component /apps/you-project/components/product-component-name to search results page yourself, because they will not be loaded with ajax request.

web2py controller for layout.html

I'm building a web application using web2py, and have currently implemented search on some of the view/ pages by creating a function in the appropriate controller and then using {{=form}} to render it in my HTML page like this...
<h3>Search</h3>
{{=form}}
{{if results:}}
<h3>Results</h3>
{{for result in results:}}
{{=A(result.title)}}
{{pass}}
Rather than repeat this functionality in multiple pages, I would like to have a search field in my layout.html file, which can then be used across all over views. However, there is no controller for layout.html. Is it possible to use a method in another controller in the layout.html page, or is there another way to link a controller to layout.html that I am missing?
Thanks.

Include plain HTML page inside a Play Framework view template

Is there a way to include a plain html page inside a Play Framework's view template? I have a scenario wherein there is a common view template and in the body of the template, I would like to include certain static html pages. I know that I can include other templates inside a certain template, but I'm not sure if I could include a plain html page?
One option is to just make your static HTML a template, eg, create myStaticPage.scala.html:
<h1>Static page</h1>
<p>This page is static, though it is still a template.</p>
Then your view template, myView.scala.html:
#(staticPage: Html)
<html>
<head>...</head>
<body>#staticPage</body>
</html>
And then in your action that renders the template:
def renderMyStaticPage = Action {
Ok(views.html.myView(views.html.myStaticPage())
}
You just need to make sure that your HTML page escapes any # symbols with ##.
On the other hand, if which HTML page that's being included is more dynamic, then simply load the HTML from the file/database/classloader/whereever it's coming from, eg:
def renderMyStaticPage = Action {
val staticPage: String = // code to load static page here
Ok(views.html.myView(Html(staticPage))
}
You could.
Just put something like that in your routes file:
GET /file controllers.Assets.at(path="/public", file="html/file.html")
Here is a duplicated post: Route to static file in Play! 2.0