- Import necessary modules:
from django.urls import path
from . import views
- Define a URL pattern with multiple primary keys:
urlpatterns = [
path('model_name/<int:pk1>/<int:pk2>/', views.your_view_name, name='your_url_name'),
]
- Update the view to accept multiple primary keys:
def your_view_name(request, pk1, pk2):
# Your view logic here
- In your template, use the URL pattern with multiple primary keys:
<a href="{% url 'your_url_name' pk1=obj1.pk pk2=obj2.pk %}">Link Text</a>
- Access the parameters in your view:
def your_view_name(request, pk1, pk2):
obj1 = YourModel1.objects.get(pk=pk1)
obj2 = YourModel2.objects.get(pk=pk2)
# Your view logic here
- In your models, ensure proper definitions for primary keys:
class YourModel1(models.Model):
pk1 = models.AutoField(primary_key=True)
# Other model fields
class YourModel2(models.Model):
pk2 = models.AutoField(primary_key=True)
# Other model fields
- Ensure proper inclusion of the app's URLs in the project's
urls.py
:
from django.urls import include
urlpatterns = [
path('your_app/', include('your_app.urls')),
]