Search code examples
djangoprimary-keydjango-generic-views

how to pass a primary key to a view in django?


I am trying to implement a class based view that should create a update form to update my model form but I don not know how to pass the pk from my base.html to my view:

viewvs.py:

from artdb.models import *

class UpdateForm(UpdateView):

    print('updateform')
    model=Date
    fields=['activity']
    template_name='updateForm.html'

updateForm.html:

{% extends "artdb/base.html" %}

{% block upd %}
    <form action="" method="post">
    {% csrf_token %}
    {{ form.as_p }}
     <input type="submit" value="Update">
     </form>
{% endblock upd %}

base.html:

<p><a class="btn btn-secondary"  href="{% url 'artdb:updateform' %}" role="button">update form &raquo;</a></p
{% block upd %}
{% endblock upd %}

urls.py:

urlpatterns = [
path('<pk>/updateform/',views.UpdateForm.as_view(),name='updateform'),
]

I think that pk should be passed in the base.html but I am not shure how. Any suggestions?


Solution

  • I think you've probably got it by now, but if you didn't, try it like this in the app url.py file:

    urlpatterns = [
    path('updateform/<int:pk>',views.UpdateForm.as_view(),name='updateform'),
    ]