Search code examples
pythondjangomodel-view-controlleradmindjango-1.8

How can admin post content visible by other users in django 1.8?


First of all, I'm new to django, this is my first projet so I have minimum knowledge.

I'm working with django 1.8 and I have made a basic website. My problem is the following: You know how when you visit a website and the content is updated by the admin ? (news, schedule or reminder of deadline if you're a uni student) Is there a way to do that so the admin uses only an interface without touching the code ? I mean, supposing that the admin knows nothing about django and has a website in which he wants to uploads "news" or "announcements" that will be visible by all users or he can edit/delete old posts...

I would appreiate it if you can guide me by giving me useful links to doumentations, tutorials or existing projects on github to see how it atually works. Thank you for your help.


Solution

  • You can use the Django Admin to create/edit models through a web interface with zero programming.

    Of course, for this to work you'd have to keep your editable content in models instead of hardcoding them in your templates.

    Let's assume our site is a small blog and our posts are instances of the following model:

    class Post(models.Model):
        title = models.CharField(max_length=50)
        content = models.TextField()
    

    And you are displaying them like so on the homepage:

    <ul>
    {% for post in posts %}
        <li>
            <p>{{ post.content }}</p>
        </li>
    {% endfor %}
    </ul>
    

    In <app>/admin.py you need to register the model into the admin, so the admin interface knows about the model and displays it:

    from .models import Post # import your model
    
    admin.site.register(Post)
    

    Now you should be able to easily create and modify instances of Post from the admin interface, and they should automatically appear on the homepage.

    I suggest taking a look at the official Django tutorial or the Django Girls blog tutorial which show in detail how to use the admin to update what's displayed on the customer-facing webpages.