# create django site that has snippets

**URL:** <https://forum.djangoproject.com/t/create-django-site-that-has-snippets/15927>\
**Category:** Templates & Frontend\
**Created:** [September 11, 2022, 3:04pm UTC](https://forum.djangoproject.com/t/create-django-site-that-has-snippets/15927 "2022-09-11T15:04:35Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![rwahdan1978](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rwahdan1978/32/9932_2.png) [@rwahdan1978](https://forum.djangoproject.com/u/rwahdan1978)\
**Post date:** [September 11, 2022, 3:04pm UTC](https://forum.djangoproject.com/t/create-django-site-that-has-snippets/15927/1 "2022-09-11T15:04:35Z")

</div>

Hi,

I want to create a django site that will have many coding shortcuts for many languages just like the site below. when clicking a link it will show you the code and you can then copy or download from there.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/1/1674fa0098e896491425191589b8896ccca80337.png)

How to do that in django? Please give me hints for links to tutorials that can help.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [September 11, 2022, 3:09pm UTC](https://forum.djangoproject.com/t/create-django-site-that-has-snippets/15927/2 "2022-09-11T15:09:23Z")

</div>

That’s a really broad question.

What specifically about this do you not know to do?

What have you done with Django so far?

---

<div class="post-metadata">

**Author:** ![rwahdan1978](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rwahdan1978/32/9932_2.png) [@rwahdan1978](https://forum.djangoproject.com/u/rwahdan1978)\
**Post date:** [September 11, 2022, 4:01pm UTC](https://forum.djangoproject.com/t/create-django-site-that-has-snippets/15927/3 "2022-09-11T16:01:30Z")

</div>

I have a lot of code that i use a lot and sometimes i forget part of it (memorizing) so i need to have a site that holds my codes in a text editor that allows copying what is inside it.

I have not started doing anything yet i just need a kick start.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [September 11, 2022, 4:03pm UTC](https://forum.djangoproject.com/t/create-django-site-that-has-snippets/15927/4 "2022-09-11T16:03:02Z")

</div>

That’s all fine.

What is your previous experience with Django?

What part of creating this site do you not know how to do?

---

<div class="post-metadata">

**Author:** ![rwahdan1978](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rwahdan1978/32/9932_2.png) [@rwahdan1978](https://forum.djangoproject.com/u/rwahdan1978)\
**Post date:** [September 11, 2022, 5:21pm UTC](https://forum.djangoproject.com/t/create-django-site-that-has-snippets/15927/5 "2022-09-11T17:21:18Z")

</div>

I have this code but even if i have more records it is copying the first one only.

```auto
{% extends 'mysite/main.html' %}
{% block content %}

<!DOCTYPE html>
<html>
<body>

<h3>Coding snippets</h3>

{% for data in userData %}

<textarea readonly id="data" rows="4" cols="50">{{data.code_details}}</textarea>
<button onclick="myFunction()">Copy text</button>

<script>
function myFunction() {
  // Get the text field
  var copyText = document.getElementById("data");

  // Select the text field
  copyText.select();
  copyText.setSelectionRange(0, 99999); // For mobile devices

  // Copy the text inside the text field
  navigator.clipboard.writeText(copyText.value);
  
  // Alert the copied text
  alert("Copied the text: " + copyText.value);
}
</script>

{% endfor %}

</body>
</html>

{% endblock content %}

why is that?

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [September 11, 2022, 5:23pm UTC](https://forum.djangoproject.com/t/create-django-site-that-has-snippets/15927/6 "2022-09-11T17:23:48Z")

</div>

It is not valid to have multiple elements on a page with the same `id` attribute.

If you’re going to iterate over a list of items to be rendered on a page, you need to ensure that each instance has a unique id.

(One common pattern is to generate the id by concatenating the pk of the individual instance to the name of the model.)

You also have your JavaScript function definition inside your loop which means it is going to be repeated for each element in `userData`.

---

<div class="post-metadata">

**Author:** ![rwahdan1978](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rwahdan1978/32/9932_2.png) [@rwahdan1978](https://forum.djangoproject.com/u/rwahdan1978)\
**Post date:** [September 11, 2022, 5:48pm UTC](https://forum.djangoproject.com/t/create-django-site-that-has-snippets/15927/7 "2022-09-11T17:48:46Z")

</div>

how to generate the id?

```auto
<textarea readonly id="{{forloop.counter}}" rows="4" cols="50">{{data.code_details}}</textarea>
  <button onclick="myFunction()">Copy text</button>

```

but how to refernce that in java:

```auto
var copyText = document.getElementById("{{forloop.counter}}")

```

this will not work as the loop has exited but the values are there if i put:

```auto
var copyText = document.getElementById("1")

```

this works but how to reference them?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [September 11, 2022, 6:11pm UTC](https://forum.djangoproject.com/t/create-django-site-that-has-snippets/15927/8 "2022-09-11T18:11:38Z")

</div>

Your `onclick` function call in the `button` object can pass the `id` as a parameter to the function. The function definition can then accept that `id` as a parameter to the function.

---

<div class="post-metadata">

**Author:** ![rwahdan1978](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rwahdan1978/32/9932_2.png) [@rwahdan1978](https://forum.djangoproject.com/u/rwahdan1978)\
**Post date:** [September 12, 2022, 3:59am UTC](https://forum.djangoproject.com/t/create-django-site-that-has-snippets/15927/9 "2022-09-12T03:59:28Z")

</div>

I tried this still not working:

```auto
<h3>Coding snippets</h3>

{% for data in userData %}

  <textarea readonly rows="4" cols="50">{{data.code_details}}</textarea>
  <button id = "{{data.id}}" onclick="myFunction('{{data.id}}')">Copy text</button>

{% endfor %}

  <script>
  function myFunction(str num) {
    // Get the text field
    var copyText = document.getElementById("num")

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [September 12, 2022, 12:00pm UTC](https://forum.djangoproject.com/t/create-django-site-that-has-snippets/15927/10 "2022-09-12T12:00:43Z")

</div>

What specifically is not working? Are you getting any JavaScript errors in the browser’s JavaScript console? Have you verified that the rendered html is what you expect it to be?

---

<div class="post-metadata">

**Author:** ![rwahdan1978](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rwahdan1978/32/9932_2.png) [@rwahdan1978](https://forum.djangoproject.com/u/rwahdan1978)\
**Post date:** [September 13, 2022, 6:38am UTC](https://forum.djangoproject.com/t/create-django-site-that-has-snippets/15927/11 "2022-09-13T06:38:49Z")

</div>

When i click the button nothing is happening.

---

<div class="post-metadata">

**Author:** ![rwahdan1978](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rwahdan1978/32/9932_2.png) [@rwahdan1978](https://forum.djangoproject.com/u/rwahdan1978)\
**Post date:** [September 14, 2022, 4:40am UTC](https://forum.djangoproject.com/t/create-django-site-that-has-snippets/15927/12 "2022-09-14T04:40:54Z")

</div>

```auto
{% for data in userData %}

  <textarea readonly rows="4" cols="50">{{data.code_details}}</textarea>
  <button id = "{{data.code_title}}" onclick="myFunction('{{data.code_title}}')">Copy text</button>

{% endfor %}

  <script>
  function myFunction(num) {
    // Get the text field

    alert(num);
    var copyText = document.getElementById(num)

    // Select the text field
    copyText.select();
    copyText.setSelectionRange(0, 99999); // For mobile devices

    // Copy the text inside the text field
    navigator.clipboard.writeText(copyText.value);
    
    // Alert the copied text
    alert("Copied the text: \n" + copyText.value);
  }
  </script>

```

the alert will come out right but when trying to access the id it is not giving anything or any errors!

---

<div class="post-metadata">

**Author:** ![rwahdan1978](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rwahdan1978/32/9932_2.png) [@rwahdan1978](https://forum.djangoproject.com/u/rwahdan1978)\
**Post date:** [September 17, 2022, 7:04am UTC](https://forum.djangoproject.com/t/create-django-site-that-has-snippets/15927/13 "2022-09-17T07:04:38Z")

</div>

I found the problem. I was accessing the id of the button not the textarea which has the text.
