简体   繁体   中英

Modifiable bullet list in Django

I currently have a list generated in views.py that is displayed by bullet points on the wanted page by the template:

views.py :

bullet_list = ['Apple starts with A','Banana starts with B','Carrot starts with C']
context = {'bullet_list': bullet_list}
return render(request, 'example.html', context)

example.html :

{% if bullet_list %}
    <ul style="width: 800px;">
    {% for bullet in bullet_list %}
        <li style="list-style-type: circle;">{{bullet}}</li>
    {% endfor %}
    </ul>
{% endif %}

I would like it so the user can add/remove/adjust the bullet points and save the adjusted bullet points. Ideally, when they go back on the page, their new changes are there instead.

I was able to get it to delete a bullet (shown below), but I was unsure of how to get it to save that it was deleted and how to get input entered to be another bullet:

example.html :

<li onclick="this.parentNode.removeChild(this);  onmouseover="this.setAttribute('style', 'text-decoration: line-through')" onmouseout="this.setAttribute('style', 'text-decoration: none;')" class="bullet">{{bullet}}</li>

I know there would have to be an input text field to add a new one and a save button, but other than that I have no idea how to implement this, nor could I find much on it. Would I use a model for this? JS? Just views.py/html and css within the template?

You need to change:

bullet_list = {'Apple starts with A','Banana starts with B','Carrot starts with C'}

to:

bullet_list = ['Apple starts with A','Banana starts with B','Carrot starts with C']

The context is an object ' {} ' but you want your bullet_list to be an array/list ' [] '

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM