I have a page in django, I even brought it into the application, it has blocks and the like. Here's an example:

I need all this text to be editable through the admin panel by a non-technically savvy user. And then display it on a page for everyone. The task is simple, but I have been solving it for a day. I created a model, but I don't understand how to display it on the page, I use a cycle and blocks are duplicated because of this. In PHP, I did it in half an hour, according to this algorithm:

In a table with columns: Content, title

And into the page I inserted content by name from the array that loaded ajax.

How can I implement this in django? As I understand it, models are not needed here?

My code is now


from django.db import models
class Content_index (models.Model):
    name= models.CharField ('Name (do not delete)', max_length= 32)
    content= models.TextField ('Block Content')
    def __str __ (self):
        return self.name
    class Meta:
        verbose_name= 'Content'
        verbose_name_plural= 'Content' 


from django.contrib import admin
from .models import Content_index
admin.site.register (Content_index) 


from django.shortcuts import render
from .models import Content_index
def index (request):
    content_index= Content_index.objects.all ()
    return render (request, 'main /index.html', {'content': content_index}) 

P.S ideally be able to change images through the admin panel too

  • Answer # 1

    In this case, content_index is not even quite a list, namely that QuerySet. However, it can be iterated over in the same way as over a list:

    {% if content%}
            {% for post in content%}
    div >
    h1 >
    {{post.name}} <
    /h1 >
    p >
    {{post.content}} <
    /p >
    hr >
    /div >
            {% endfor%}
        {% else%}
    h2 >
    No published articles! <
    /h2 >
    {% endif%}

    UPD: If I understand the updated question correctly, then you need to display different blocks in different places on the page. In this case, you need to transfer specific blocks separately in the display:

    def index (request):
        content_index= Content_index.objects.all ()
        blocks= []
        for e in list (Articles.objects.all (). values ​​("name")):
            blocks.append (e ["name"])
        content= []
        for e in list (Articles.objects.all (). values ​​("content")):
            content.append (e ["content"])
        bc= {b: c for b, c in zip (blocks, content)}
        return render (request, 'main /index.html', bc)

    Next, just indicate the names of the blocks in the places on the page where they need to be displayed. And for administration, as I said, you can use the built-in Django admin panel, or write a form using jQuery.

    Yes, I know about this application, but I need to do it differently. I just need to display the content of a specific element by its name

    artemgh2022-01-02 22:12:12

    I just want to make an admin panel on the site so that you can change the text and headers on the site through the admin panel, apparently I did not implement it correctly, I teach django one day

    artemgh2022-01-02 22:12:12

    What does it mean to display content by its name? If there is a field in which name is entered, and content should appear below after the input, then you need to intercept these values, and already on the server side, hook the desired value, for example, using jQuery, or through a redirect you can do it using standard Django tools, but it will be less conveniently. Plus, you don't have to reinvent the wheel, unless you really need to (and if you learn Django for one day, it is very unlikely), and use the built-in Django admin panel

    onlikerop2022-01-02 22:12:12

    apparently I did not explain correctly, in general it is a terribly simple task in php it is written in minutes, I can not figure out how to do it on django.

    artemgh2022-01-02 22:12:12

    Completed the answer.

    onlikerop2022-01-01 22:43:12