Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to pass 2 pks of different models in the same url django

how do i add primary keys of 2 different models of post model and comment model in the same url of editing comments

when im adding 2 int pks in the same url it shows an error and when im giving 1 custom name then it doesnt work

views.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class EditCommentView(UpdateView):
    model = Comment
    form_class = EditCommentForm
    template_name = "edit_comment.html"

urls.py

urlpatterns = [
    path("post/<int:pk>/comments/", login_required(CommentsView.as_view(), login_url='signin'), name="comments"),
    path("post/<int:pk>/comments/add/", login_required(AddCommentView.as_view(), login_url='signin'), name="add-comment"),
    path("post/<int:pk>/comments/<int:pk>/edit/", login_required(EditCommentView.as_view(), login_url='signin'), name="edit-comment"),
]

template

{% extends "base.html" %}


{% block content %}


<section class="text-gray-600 body-font overflow-hidden">
    <div class="container px-5 py-24 mx-auto">
        <div class="flex flex-col text-center w-full mb-12">
            <h1 class="sm:text-3xl text-2xl font-medium title-font mb-4 text-gray-900">Comments</h1>
            <p class="lg:w-2/3 mx-auto leading-relaxed text-base">
                <a class="text-indigo-500 inline-flex items-center mt-4" href="{% url 'add-comment' post_id %}">Add Comment
                </a>
            </p>

        </div>
        <div class="-my-8 divide-y-2 divide-gray-100">
            {% for comment in object_list %}
            <div class="py-8 flex flex-wrap md:flex-nowrap">
                <div class="md:w-64 md:mb-0 mb-6 flex-shrink-0 flex flex-col">
                    <!-- <span class="font-semibold title-font text-gray-700">CATEGORY</span> -->
                    <span class="mt-1 text-gray-500 text-sm">{{comment.date_created}}</span>
                </div>
                <div class="md:flex-grow">
                    <h2 class="text-2xl font-medium text-gray-900 title-font mb-2">{{comment.user.username}}</h2>
                    <p class="leading-relaxed">{{comment.body}}</p>

                    <a class="text-indigo-500 inline-flex items-center mt-4" href="{% url 'edit-comment' post_id comment.id %}"><svg xmlns="http://www.w3.org/2000/svg"
                            width="16" height="16" fill="currentColor" class="bi bi-pencil-square" viewBox="0 0 16 16">
                            <path
                                d="M15.502 1.94a.5.5 0 0 1 0 .706L14.459 3.69l-2-2L13.502.646a.5.5 0 0 1 .707 0l1.293 1.293zm-1.75 2.456-2-2L4.939 9.21a.5.5 0 0 0-.121.196l-.805 2.414a.25.25 0 0 0 .316.316l2.414-.805a.5.5 0 0 0 .196-.12l6.813-6.814z" />
                            <path fill-rule="evenodd"
                                d="M1 13.5A1.5 1.5 0 0 0 2.5 15h11a1.5 1.5 0 0 0 1.5-1.5v-6a.5.5 0 0 0-1 0v6a.5.5 0 0 1-.5.5h-11a.5.5 0 0 1-.5-.5v-11a.5.5 0 0 1 .5-.5H9a.5.5 0 0 0 0-1H2.5A1.5 1.5 0 0 0 1 2.5v11z" />
                        </svg>

                    </a>

                    <a class="text-indigo-500 inline-flex items-center mt-4 ml-3"><svg
                            xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
                            class="bi bi-archive-fill" viewBox="0 0 16 16">
                            <path
                                d="M12.643 15C13.979 15 15 13.845 15 12.5V5H1v7.5C1 13.845 2.021 15 3.357 15h9.286zM5.5 7h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1 0-1zM.8 1a.8.8 0 0 0-.8.8V3a.8.8 0 0 0 .8.8h14.4A.8.8 0 0 0 16 3V1.8a.8.8 0 0 0-.8-.8H.8z" />
                        </svg>

                    </a>
                </div>
            </div>
            {% endfor %}
        </div>
    </div>
</section>

{% endblock %}

>Solution :

You can’t have two URL variables with the same name, in this case 'pk' – but when you change the name to something else, the form doesn’t recognise the new name. Because your form is editing the comment, I would suggest changing the name of the post PK because you don’t need to pass that into the form.

path("post/<int:post_pk>/comments/<int:pk>/edit/"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading