How to create anchors-links on the top of page from content

Hello, i would like to create posts, where at the top will be little inner navbar-anchors by post. For example: Symptomps, causes, diet etc, which by clicking will move you to this part of article. The main problem is how to make this? If i have Title and content, is it possible to extract this h2 to the top automatically? I can’t create certain number of fields because it can be different from article to article etc.

It must be like at this screenshot: http://joxi.ru/xAeQzbMTXk4ekA

Thanks in advance for advices

That would be up to you to render in your templates. You can use the hash-fragment url to reference the id attribute of any appropriate html tag within your document.

See id - HTML: HyperText Markup Language | MDN and Identifying resources on the Web - HTTP | MDN

Thanks a lot for your response! Yes, I know about this reference to id. But how could i do this correctly. I have TextField in my model, and I want get subtitles(i create text due to ckeditor, which create subtitles as h2 tag) from TextField - is it possible? Or I have to create a new fields in admin panel?

Ok, so you’re rendering manually entered text. In that case, you would need to write some code that will examine that field to find the tags. Once you’ve identified the tags, you’ll need to add an id attribute to that tag - and that’s what you’ll use to create the relative link.

Thank you. Sorry for my stupidy coz i’m new in django, before i’ve worked only with cms. How could I examine text from min TextField and insert id for template? i can’t realize(( It seems to me as unreal

This wouldn’t be something specific to Django, this would be a general Python issue.

You’ve got a text string. You want to examine it for specific strings (html tags) that may exist in that string. When you find those, you want to make substitutions.

You can use the Python regular expressions module to find the substrings in the text. (Or, if you know it’s just a specific subset of tags you want to find, you could use the str.find function.) Then, once you’ve found the tags, you can use the replace function to change those tags, adding in a unique id attribute for each one.

Or, you could use the BeautifulSoup library to work with that field as if it were an html document.

Either way, it may take you a little bit of playing to get it working exactly the way you want.