Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

Django: Won't Delete Post

I am trying to get my posts to delete on the click of a button that pops up on the screen instead of sending the user to a different delete page. The form works but it won't actually delete the posts.

Template

    {% block head %}

    <head>
        <link rel="stylesheet" type="text/css" href="{% static 'css/my_posts.css' %}">
        <title>Create - My Posts</title>

        <script>
            function togglePopup(){document.getElementById("popup-1").classList.toggle("active")}
        </script>

    </head>

    {% endblock head %}


    {% block content %}
    <div class="content_section">
        <div class="btn_container">
            <a class="new_post" href="{% url 'newpost' %}"><button class="new_post_btn">New Post</button></a>
        </div>
        <div class="content_section_two">
            {% for product in products %}
                <div class="post_short_container">
                    <div class="title_container">
                    
                        <a href="#" class="post_link">
                            <div><b>{{ product.title }}</b></div>
                        </a>
                        
                    </div>
                    <div class="right_btns">
                        <a href="{% url 'editpost' product.id %}" class="edit_link">Edit</a>
                        <a href="#" class="stats_link">Stats</a>
                        <div class="ad_btn_container">
                            <div class="ad_btn">Ad</div>
                        </div>
                        <div class="delete_btn_container">
                            <div class="delete_btn" onclick="togglePopup()">Delete</div>
                        </div>
                    </div>
                </div>

                <div class="delete_prompt_container" id="popup-1" action="{% url 'deletepost' product.pk %}">
                    <form method="POST">
                        {% csrf_token %}
                        <p class="delete_prompt">Are you sure you want to delete this post?</p>
                        <div class="cancel_delete_container">
                            <div class="cancel_button" onclick="togglePopup()">Cancel</div>
                            <input value="Delete" type="submit" name="confirm" class="confirm_delete_button">
                        </div>

                    </form>
                </div>
            {% endfor %}
        </div>

    </div>

    

    {% endblock content %}

views.py

def deletePost(request, pk):
    post = Post.objects.get(id=pk)
    if request.method == 'POST':
        post.delete()
        return HttpResponseRedirect(reverse('myposts'))

urls.py

urlpatterns=[
    path('deletepost/<int:pk>', views.deletePost, name='deletepost'),
]
question from:https://stackoverflow.com/questions/65546523/django-wont-delete-post

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...