How do you redirect to a specific position on a page in Django?

Evelyn Chin :

If I were listing out posts on a page through Django, and say I wanted to give users the option to bookmark a post. I would create a view "bookmark_post", but I would have to redirect them somewhere after the function is completed, right? How would I redirect them to exactly the same place they were on the page before they bookmarked it, so they wouldn't have to always keep scrolling down to find it after bookmarking?

I feel like I'm somehow overcomplicating this, or maybe I don't have to create an entirely new view in order to bookmark a post. Do you have any ideas? Thanks again.

Edit: Main Question now: is there a way to bookmark a post without redirecting the page? I currently have a User model, with a ManyToManyField of 'bookmarks'. Is it possible to not use a view to add a bookmark to the User's profile? I feel it would be tedious to have to wait for the page to redirect every time you bookmarked something, similar to how it would be tedious if you had to wait for the page to redirect everytime you "liked" something.

Ahmed I. Elsayed :

Redirecting to a page can be done with django. Scrolling to a certain part, can't be done with django alone, However, It can be done with a human brain and django, Let's see how does scrolling work..

*{
  padding:0;
  margin: 0;
  box-sizing: border-box;
}

.container {
  height: 2000px;
  background-color: green;
}

.btn {
  text-decoration: none;
  color: black;
  width: 100px;
  padding: 20px;
  background: yellow;
}

.post{
  width: 400px;
  height: 150px;
  background-color: blue;
  margin-bottom: 10px;
  border: 1px solid white;
}
<a class="btn" href="#post_11">Go to post 11</a>

<div class="container">
  <div class="post" id="post_1"></div>
  <div class="post" id="post_2"></div>
  <div class="post" id="post_3"></div>
  <div class="post" id="post_4"></div>
  <div class="post" id="post_5"></div>
  <div class="post" id="post_6"></div>
  <div class="post" id="post_7"></div>
  <div class="post" id="post_8"></div>
  <div class="post" id="post_9"></div>
  <div class="post" id="post_10"></div>
  <div class="post" id="post_11"></div>
  <div class="post" id="post_12"></div>
</div>

We use a tag to scroll to a specific element by it's css id, This means you want you create a dynamic css id for each post. This is best done using the pk. Say it's f"post_{post.pk}"

So you can redirect the user to fwww.example.com/#{post.pk}` and this will redirect and scroll to that specific post.

For redirections, use a RedirectView.

To prevent refreshes, See this ref

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=379449&siteId=1