S
S
serhiops2022-02-12 03:14:00
Django
serhiops, 2022-02-12 03:14:00

How do I make it so that only the author can delete a post?

To do this, I take the current authorized user in the template and compare it with the author of the post, and if they match, then the "Delete" button is visible to the user. It seems that everything is simple in words, but for some reason it does not work.
views.py:

def view_news(request, new_id):
    news_item = get_object_or_404(News, pk = new_id)
    categories = Category.objects.annotate(cnt=Count('get_news')).filter(cnt__gt=0) 
    curent_post_coments =  News.objects.get(pk = new_id).get_post.filter(active=1)
    if request.method == 'POST':
        form = ComentForm(request.POST)
        if form.is_valid():
            form = form.save(commit=False)
            form.author = request.user
            form.post = news_item
            form.post.id = new_id
            form.save()
        else:
            messages.error(request,'Что-то пошло не так:(')
    else:
        form = ComentForm()
    context = {
        'news_item':news_item,
        'side':1,
        'categories':categories,
        'form':form,
        'curent_post_coments':curent_post_coments,
        'form':form,
    }
    return render(request,'news/detail.html', context )

template:
{% extends 'news/base.html' %}

{% block title %}{{ news_item.title }}{% endblock %}



{% block content %}

<div class="card-mb-3">
    <div class="card-header">
        Категория: <a href="{{ news_item.category.get_absolute_url }}">{{news_item.category}}</a>
        <!--{{ i.category.get_absolute_url }} - используется обращение к таблице Category через внешний ключ поля в таблице News(category)-->
    </div>
    <div class="card-body">
        <div class="media">
            {% if news_item.photo %}
            <img src="{{news_item.photo.url}}" alt="Изображение" width='350' height="235"
                style="float:left;margin: 5px 17px 7px 0;">
            {% else %}
            <img src="https://picsum.photos/350/235?grayscale" alt="Изображение"
                style="float:left;margin: 7px 17px 7px 0;">
            {% endif %}
            <hr />
            <div class="media-body">
                {% autoescape off %}
                <h5 class="card-title">{{ news_item.title|escape }}</h5>
                <p class="card-text">{{ news_item.content|linebreaks }}</p>
                {% endautoescape %}
                <p style="text-align:right;"><b>Опубликовал: </b><a
                        href="{{ i.get_absolute_url_user }}">{{news_item.author}}</a><br><b>{{ news_item.created_at|timesince }} ago</b>{% if user.username == news_item.user %}<br><br><button type="button" class="btn btn-danger">Удалить</button>{% endif %}</p>
            </div> <!--{% if user.username == news_item.author %} True {{ user.username }} == {{ news_item.author }} {% else %} False {{ user.username }} != {{ news_item.author }}{% endif %} ///       
            {% if str.user.username == str.news_item.author %} True str.{{ user.username }} == str.{{ news_item.author }} {% else %} False str.{{ user.username }} != str.{{ news_item.author }}{% endif %}///-->
        </div>
        <br>
    {% include 'news/inc/_coment.html' %}
    <br>
    <div class="card-mb-3">
    {% for i in curent_post_coments %}
    <div class="card">
        <div class="card-header">{{ i.created }}</div>
        <div class="card-body">
          <h5 class="card-title"><a href="{% url 'about_user' i.author %}">{{ i.author }}</a></h5>
          {% autoescape off %}
          <p class="card-text">{{i.body}}</p>
          {% endautoescape %}
        </div>
        <!--{% include 'news/inc/_com_del.html' %}-->
      </div>
      <br>
    {% endfor %}
</div>
</div>

{% endblock %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2022-02-12
@serhiops

1. 2. in addition to this, such a check must be done when deleting {% if user == news_item.author %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question