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

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

Related

How to get the current root URL on JSP

I need to dynamically get the root URL from a page to set as part of the Open Graph tags that will fetch the desired image. The closest I got was with ${request.requestURL}, but it returns the whole URL, like:
https://localhost:8080/abc/123/example/example.jsp
But I would like for it to return just the root URL, like:
https://localhost:8080/
Is there a way to do this?
This is the header of a page that will be the product page of a e-commerce. So, I would need to get the root URL of whatever part of the website the user is in to fill in with the product-specific URL. I've tried lots of methods, like ${request.requestURI} and ${request.contextpath} but none of them return what I want.
Don't know if it's the optimal solution, but I achieved what I needed assembling the url like this:
${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}

Passing Previous / Calling / Referrer URL from view to controller

I have course and pages controllers and views. Each of them have edit / details actions. The list of pages is displayed on the course view in both Edit and details mode. There's a "Back" button on page view that must take the user to the calling page. Below are the two sample urls, both of which will list pages with edit / details buttons :
root/edit/courses/course-url
root/details/courses/course-url
I way i found to work with this is using "Referer" header Request.UrlReferrer.ToString(). But i also read that the browser can block sending this header and also it can be easily tampered. Please suggest a way to send the referer Url from the view.
Details
May be you will send it as query parameter?
Details

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.

Django endless pagination and anchors

I'm creating a Django site that hosts a large set of long, transcribed debates.
I have two main views: a Haystack search view which indexes each individual speech, and a full view which indexes each transcript (containing hundreds of individual speeches). Both views use django endless pagination to display results.
I'm trying to link between these two views so that any search result (speech) can be viewed in context within its parent transcript, and I want the page to jump to the anchor of that speech ID on page load.
I calculate the page where the individual result appears and redirect to that URL while storing the pk of the result in messages so I can highlight the result:
def full_view_redirect(request, year, month, day, pk):
y=str(year)
m=str(month)
d=str(day)
qs = transcripts.objects.filter(speechdate__year=year).filter(
speechdate__month=month).filter(speechdate__day=day).order_by('basepk').all()
firstpk = int(qs[0].basepk)
pageNo = ((int(pk)-firstpk)//15)+1
messages.add_message(request, messages.INFO, pk)
if pageNo == 1:
return redirect("/full/"+y+"/"+m+"/"+d+"/"+"#"+str(pk))
else: ## this doesn't work
return redirect("/full/"+y+"/"+m+"/"+d+"/"+"?page="+str(pageNo)+"#"+str(pk))
My question is similar to http://htmlasks.com/how_to_make_this_link_work_page2reviews_reload_the_page_and_jump_to_the_anchor but the suggestion here to switch around the anchor and the ?page doesn't work.
I can't get the anchor to work so the page jumps to the desired result on page load. Am I missing something obvious?
Edit:
I verified the div I want to jump to has a proper id, eg.
<div class="panel panelhighlight" id="54969">
A url like /full/1903/04/29/?page=7#54969 loads the proper page but does not jump to the div.
A url like /full/1903/04/29/#54969?page=7 loads the first page and not page 7.
Edit 2:
I have switched from django-endless-pagination to django-digg-paginator so that pagination is handled within my view, not at the template level.
Then, I had to ensure the redirect reloads by omitting the slash between the page number and the anchor. /full/1903/04/29/7#54969 repositions the page successfully on load.
As detailed in my edit, I had two problems going on that I needed to fix:
I switched from django-endless-pagination to
django-digg-paginator so that pagination is handled within my view/URL pattern,
not at the template level.
I had to ensure the redirect reloads by omitting the slash
between the page number and the anchor, ie. /full/1903/04/29/7#54969
repositions the page successfully on load.

retrieving URLs from functions within HTML (python)

I need to scrape some URLs from some retailer product pages, but the specific URLs I need to get aren't in the html part of the page. The html looks like this for each of the items on which one would click to get to the page with the URL I need to grab:
<div id="name" class="hand bold" onclick="AVON.productcontrol.Go(45714);">ADVANCE TECHNIQUES Color Protection Conditioner Bonus Size</div>
I wrote the following to get URLs from the page, but since the actual URLs I need don’t seem to be stored in the page, it doesn’t get what I need:
def getUrls(URL):
"""input: product page url
output: list of urls to products
"""
connection = urllib.urlopen(URL)
dom = lxml.html.fromstring(connection.read())
selAnchor = CSSSelector('a')
foundElements = selAnchor(dom)
urlList = [e.get('href') for e in foundElements]
return urlList
Is there a way to get the link that the function after ‘onclick’ (I guess AVON.productcontrol.Go(#);) takes you to? I don’t fully understand html, and while I’ve read a bit about onclick, I can’t figure out how the function after 'onclick' works.
In order to find the URL that you are taken to on click, you need to find the JavaScript source code of the 'Go' function and read and understand it. It's buried somewhere within a tag or some JavaScript .js file that is referenced directly or indirectly by the HTML page. Happy digging!
Or: you automate the interaction with the web page with a tool like Selenium (http://docs.seleniumhq.org/) and just check where it takes you if you click.