Passing object title to html form

Hi, I have a dynamic class based product list that links to a dynamic detail view. I need to pass the title of the product object in the detail view to my html request a quote form page. I want the title of the product to be listed on the contract form. I can’t figure out how to pass the title to the new form. It is all done dynamically so I can’t hard code anything. What is the correct way of doing this? Thanks in advance.

There are a number of different ways, and it all really depends upon your models and how your data is structured. (Where does this title come from?)

If you want a more specific answer, please post the appropriate models, and the views for your detail view and quote form page.

(When posting code here, enclose the code between lines consisting of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum to keep the proper formatting of that code, which is critical in python.)

Here is the product model…

class Product(models.Model):
    image = models.ImageField(upload_to='product-images')
    short_title = models.CharField(max_length=255, default='MODEL')
    title = models.CharField(max_length=255, default='MODEL')
    description = models.TextField(null=False)
    category = models.ManyToManyField(Category)
    industry = models.ManyToManyField(Industry)
    data_sheet = models.FileField(upload_to='product-data-sheets')

    def __str__(self) -> str:
        return self.title

    def get_absolute_url(self):
        return reverse("product-detail", kwargs={"pk":})

    class Meta:
        ordering = ['title']

Here are the ProductListView, ProductDetailView, and quote view…

def quote(request):
    queryset = Product.objects.all()
    return render(request, 'quote.html', {'products': list(queryset)})

class ProductDetailView(DetailView): #  Keep this one!!!
    template_name = 'products/product_Detail.html'
    queryset = Product.objects.all()

def contact(request):
    return render(request, 'contact.html')

The ProductListView shows all products, the ProductDetailView shows the details from the selected product. I need to pass the product.title of the selected product from the ProductDetailView to the quote view. I hope this explained everything well enough.

Generally, you would pass the pk of the product in the URL as a named parameter to the other view.

How are you navigating from the detail view to the quote view?

Just linking with a url to the quote view. I don’t know how to pass the pk. The pk changes based on the selected product so I don’t know how to pass it in the view.

How are you constructing the urls?

(Have you worked your way through either the Official Django Tutorial or the Django Girls tutorial? Both of them provide a lot of valuable information, knowledge and techniques useful when working with Django.)

Here are the urls

path('products/all-products', ProductListView.as_view(), name='product-list'),
path('products/<int:pk>', ProductDetailView.as_view(), name='product-detail'),
path('quote', views.quote, name='quote'),

I’m using this to get the path to the detail view…

def get_absolute_url(self):
        return reverse("product-detail", kwargs={"pk":})

To answer you other questions. I have not done the tutorials. I use to learn new skills but he has only released the first part of his Django course so I have just been winging it for all of the more advanced issues I’ve faced.

Well, if you examine the HTML being rendered by the list view and the URLs being generated for the links, combined with the URL definition for your detail view, along with looking at the DetailView itself, you’ll see that the process used there is a precise parallel for what you’re trying to do in passing the same information along to the quotes page.

I’m sorry but I don’t understand. I tried to do it the same way before I asked for help on here but it just takes me to the product detail view again. Calling get absolute url just takes me to detail view. What am I missing?