Hello together,
my first own post here. A few weeks ago I started with Django and my project shows already good progress and I am having great fun with Python, HTML, CSS and now also with Django, learning every day (in my middle 50ties…). But now I got stuck at a certain point regarding a link in a template. Please let me explain:
The template does not work for the expression de.quote.pk within the {% url %} tag:
{% for de in deliv %}
<tr>
...
<td><a href="{% url 'app01:quote_details' de.quote.pk %}">{{ de.quote.pk }}</a></td>
...
</tr>
{% endfor %}
But it works as soon as I directly bring in the expression pk=2 (the 2 is just an example for the pk. It could also be any other pk-number):
{% for de in deliv %}
<tr>
...
<td><a href="{% url 'app01:quote_details' pk=2 %}">{{ de.quote.pk }}</a></td>
...
</tr>
{% endfor %}
So obviously I am wrong in my thinking that the expression de.quote.pk within the {% url %} tag is getting resolved as the pk which I’d like to get handed over to the app01:quote_details template.
Here’s the urls.py,
the last line contains the path I’d like to jump in with my pk of the quote-table which is assigned via its pk as ForeignKey in the deliverables-table. Models see further below.
from django.urls import path
from . import views
app_name = "app01"
urlpatterns = [
path("", views.index, name="index"),
...
path("deliverables", views.deliverables, name="deliverables"),
path("deliverable-details/<int:pk>/", views.deliverable_details, name="deliverable_details"),
path("quotes", views.quotes, name="quotes"),
path("quote-details/<int:pk>/", views.quote_details, name="quote_details"),
]
I also tried an additional line with <int:quote_pk>
urlpatterns = [
...
path("quote-details/<int:pk>/", views.quote_details, name="quote_details"),
path("quote-details/<int:quote_pk>/", views.quote_details, name="quote_details"), path("quote-details/<int:quote_pk>/", views.quote_details, name="quote_details"),
]
But it does not work either. Maybe I just haven’t applied the correct syntax or I have a lack of understanding at a certain point how this is supposed to work. Yes, I read a lot of documentation, did much G-search etc but so success so far.
To make it complete, here’s the models:
class Quote(models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE)
number = models.CharField(max_length=20, default="QTExxxxxx", unique=True) #QT000001
name = models.CharField(max_length=120, default="TBD")
descriptshort = models.CharField(max_length=200, blank=True, default="")
contact = models.CharField(max_length=200, blank=True, default="")
descriptlong = models.TextField(blank=True, default="")
content = models.TextField(blank=True, default="")
dateinitiated = models.DateField(null=True, blank=True)
dateoffered = models.DateField(null=True, blank=True)
dateordered = models.DateField(null=True, blank=True)
totalamount_offered = models.DecimalField(max_digits=12, decimal_places=0)
totalamount_ordered = models.DecimalField(max_digits=12, decimal_places=0)
status = models.ForeignKey(Gatestatus, related_name="+", default=1, blank=True, on_delete=models.CASCADE)
isactive = models.BooleanField(default=True)
def __str__(self):
return f"Quote ID.{self.pk} {self.number}: {self.name}"
class Deliverable(models.Model):
"""z.B. Kundenmodelle"""
project = models.ForeignKey(Project, on_delete=models.CASCADE)
quote = models.ForeignKey(Quote, on_delete=models.CASCADE, null=True, blank=True)
name = models.CharField(max_length=120)
number = models.CharField(max_length=20, default="26DEL1234")
datedue = models.DateField(null=True, blank=True)
datedelivered = models.DateField(null=True, blank=True)
status = models.ForeignKey(Gatestatus, related_name="+", default=1, blank=True, on_delete=models.CASCADE)
descriptshort = models.CharField(max_length=200, blank=True, default="")
descriptlong = models.TextField(blank=True, default="")
isactive = models.BooleanField(default=True)
def __str__(self):
return f"Deliverable {self.number}: {self.datedue} {self.name}"
And finally this is the view for the deliverables which is assigned to the template as described at the top of this post:
def deliverables(request: HttpRequest) -> HttpResponse:
deliverables = Deliverable.objects.all().order_by("datedue")
ctx = {"deliv": deliverables}
return render(request, "app01/deliverables.html", ctx)
And finally this is the view for the quote_details:
def quote_details(request: HttpRequest, pk) -> HttpResponse:
"""Detailansicht Quote mit Parameteruebergabe"""
quote = get_object_or_404(Quote, pk=pk)
ctx = {"myquot": quote}
return render(request, "app01/quote_details.html", ctx)
Sorry for this long post but I have tried to provide all the relevant information.
Thanks in advance!
Mirko