7

I have created a Listings Page where all listings can be searched. From there, a user can click on any particular listing to see further detail. When I go to individual listing page, I am displaying Breadcrumb to provide an option to go back to Search results (which was previous Listings page).

The issue is I am not able to pass 2 parameters (one containing the previous URL so that I can use that for Breadcrumb back to search results) while calling individual listing page from listings page. Please see main code:

views.py

def listing(request, listing_id, query):
  listing = get_object_or_404(Listing, pk=listing_id)
  

  context = {
    'listing': listing,
    'query':query
  }

  return render(request, 'listings/listing.html', context)

HTML template

{% url 'listing' listing_id=listing.id path=12 %}

I tried using {{ request.get_full_path }} to find path and then add to parameter but its not working. Any suggestions on how to capture URL will be appreciated.

3 Answers 3

4

You can work with:

{% url 'listing' listing_id=listing.id path=request.get_full_path %}

but then the path will have to accept this. So it will look like:

path('foo/<int:listing_id>/<path:path>/', …)

It also looks quite chaotic, especially since it will percentage-escape all sorts of question marks and ampersands.

Probably a slighly cleaner solution is to encode this in the querystring, so then it looks like:

path('foo/<int:listing_id>/', …)

the view looks like:

def listing(request, listing_id):
  listing = get_object_or_404(Listing, pk=listing_id)
  

  context = {
    'listing': listing,
    'query': request.GET.get('query')
  }

  return render(request, 'listings/listing.html', context)

and we render the link with to the listing with:

{% url 'listing' listing_id=listing.id %}?query={{ request.get_full_path|urlencode }}

But it simply does not look very pleasant to encode the entire path in the URL. If you have a search page, you can perhaps simply encode the search query itself (so not the path, but the content the person has written in the search bar). This makes the URL shorter, and more visually pleasant.

Sign up to request clarification or add additional context in comments.

6 Comments

My usecase is I am on listings page whose URL looks like this "127.0.0.1:8000/listings/…" and then I was also passing only listing id to see more about listing and its URL looks like this "127.0.0.1:8000/listings/listing/9" but I have a Breadcrumb on this page which should take user back to previous listings page with selected search parameters. That's the reason I was trying to save URL of listings page so that I can leverage that to use for Breadcrumb URL. Can you please suggest how can I acheive this?
@CharanjitKaur: just look at the path you retrieve, so through path or (or the request.GET.get('query') in the second case), and determine the breacrum based on that.
Thanks. It's working now. The only issue is the URL looks too long. When I am passing 2 parameters <a href="{% url 'listing' path=request.get_full_path listing_id=listing.id %}, it is expecting me to add both path and listing_id in the URL. URL file looks like "path('listing/<int:listing_id>/<path:path>', views.listing, name='listing'),". Views.py looks like "def listing(request, path, listing_id):". Is there any way we can still use path variable to pass and I can use it in views.py to pass as context to the listing page but I don't have to add path in my urls.py file?
@CharanjitKaur: yes, probably it is better to add it to the sessions, or cookies.docs.djangoproject.com/en/3.1/topics/http/sessions instead of through the URL.
Thanks again for all your help.
|
3

Try this in your urls.py:

urlpatterns = [
    path('listing/<int:pk>/<int:path>/', views.listing, name='listing')
]

3 Comments

I think the idea is not to pass 12 (this was if I understood it correctly a placeholder), but to encode the entire path of the page where the links comes from in the URL.
Why not just get the category of a listing and add a url for that in html then? If i understand there has to be a category or a user that has those listings so then you can just use the ForeignKey of that object and display the link to the list of all its related listings in html like <a href={% url 'category' listing.category.pk %}>{{category.name}}</a>
because apparently it is a search page, and the OP wants to encode the "search query". I don't think that is very elegant either. But the idea is that if the link is thus displayed on /search?q=foo or /othersearch?que=bar, then we "encode" that in the new URL.
0
request.GET.getlist("get_full_path []", [])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.