Home > front end >  how to pass 2 pks of different models in the same url django
how to pass 2 pks of different models in the same url django

Time:07-17

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

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 >
    <div >
        <div >
            <h1 >Comments</h1>
            <p >
                <a  href="{% url 'add-comment' post_id %}">Add Comment
                </a>
            </p>

        </div>
        <div >
            {% for comment in object_list %}
            <div >
                <div >
                    <!-- <span >CATEGORY</span> -->
                    <span >{{comment.date_created}}</span>
                </div>
                <div >
                    <h2 >{{comment.user.username}}</h2>
                    <p >{{comment.body}}</p>

                    <a  href="{% url 'edit-comment' post_id comment.id %}"><svg xmlns="http://www.w3.org/2000/svg"
                            width="16" height="16" fill="currentColor"  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 ><svg
                            xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
                             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 %}

CodePudding user response:

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/"

  • Related