django navbar how I do pass the product pk to each different link
up vote
0
down vote
favorite
This is a question about how to pass a Product model pk to a navbar where the call can originate from different pages / views, ie not always from the product details page.
I'm using a base template for my navbar and then I have an html page and a view for each link in the navbar like this:
Product Details - product_form.html
- views.ProductUpdateView.as_view()
Colours - productcolours_list.html
- views.ProductColourListView.as_view()
my nav bar
The idea of the navbar is that once I'm looking at Product A for example, I can click through between the product details page, the product colours, and then the bill of materials. This is what the navbar code looks like:
<li class="nav-item">
<a class="nav-link" href="% url 'ProductUpdateView' pk=product.pk %">Product Details</a>
</li>
<li class="nav-item">
<a class="nav-link" href="% url 'ProductColourListView' pk=product.pk %">Colours</a>
</li>
The problem is that this works only when I'm on the Product Details page because the base template can interpret what product.pk is. But as soon as I click to go to the Colours link in the navbar, it tries to render the base template and all of the sudden it has no idea what product.pk is.
This is because the Product Details page uses the Product model through ProductUpdateView (which I can then reference product.pk with) but the Colours page uses the ProductColour model through ProductColourListView which in that case it would be colours.product.pk.
It's like I need the base template to understand where to grab the pk from depending on whether I'm coming from the Product Details page, the Colours page or some other page.
django django-templates
add a comment |
up vote
0
down vote
favorite
This is a question about how to pass a Product model pk to a navbar where the call can originate from different pages / views, ie not always from the product details page.
I'm using a base template for my navbar and then I have an html page and a view for each link in the navbar like this:
Product Details - product_form.html
- views.ProductUpdateView.as_view()
Colours - productcolours_list.html
- views.ProductColourListView.as_view()
my nav bar
The idea of the navbar is that once I'm looking at Product A for example, I can click through between the product details page, the product colours, and then the bill of materials. This is what the navbar code looks like:
<li class="nav-item">
<a class="nav-link" href="% url 'ProductUpdateView' pk=product.pk %">Product Details</a>
</li>
<li class="nav-item">
<a class="nav-link" href="% url 'ProductColourListView' pk=product.pk %">Colours</a>
</li>
The problem is that this works only when I'm on the Product Details page because the base template can interpret what product.pk is. But as soon as I click to go to the Colours link in the navbar, it tries to render the base template and all of the sudden it has no idea what product.pk is.
This is because the Product Details page uses the Product model through ProductUpdateView (which I can then reference product.pk with) but the Colours page uses the ProductColour model through ProductColourListView which in that case it would be colours.product.pk.
It's like I need the base template to understand where to grab the pk from depending on whether I'm coming from the Product Details page, the Colours page or some other page.
django django-templates
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This is a question about how to pass a Product model pk to a navbar where the call can originate from different pages / views, ie not always from the product details page.
I'm using a base template for my navbar and then I have an html page and a view for each link in the navbar like this:
Product Details - product_form.html
- views.ProductUpdateView.as_view()
Colours - productcolours_list.html
- views.ProductColourListView.as_view()
my nav bar
The idea of the navbar is that once I'm looking at Product A for example, I can click through between the product details page, the product colours, and then the bill of materials. This is what the navbar code looks like:
<li class="nav-item">
<a class="nav-link" href="% url 'ProductUpdateView' pk=product.pk %">Product Details</a>
</li>
<li class="nav-item">
<a class="nav-link" href="% url 'ProductColourListView' pk=product.pk %">Colours</a>
</li>
The problem is that this works only when I'm on the Product Details page because the base template can interpret what product.pk is. But as soon as I click to go to the Colours link in the navbar, it tries to render the base template and all of the sudden it has no idea what product.pk is.
This is because the Product Details page uses the Product model through ProductUpdateView (which I can then reference product.pk with) but the Colours page uses the ProductColour model through ProductColourListView which in that case it would be colours.product.pk.
It's like I need the base template to understand where to grab the pk from depending on whether I'm coming from the Product Details page, the Colours page or some other page.
django django-templates
This is a question about how to pass a Product model pk to a navbar where the call can originate from different pages / views, ie not always from the product details page.
I'm using a base template for my navbar and then I have an html page and a view for each link in the navbar like this:
Product Details - product_form.html
- views.ProductUpdateView.as_view()
Colours - productcolours_list.html
- views.ProductColourListView.as_view()
my nav bar
The idea of the navbar is that once I'm looking at Product A for example, I can click through between the product details page, the product colours, and then the bill of materials. This is what the navbar code looks like:
<li class="nav-item">
<a class="nav-link" href="% url 'ProductUpdateView' pk=product.pk %">Product Details</a>
</li>
<li class="nav-item">
<a class="nav-link" href="% url 'ProductColourListView' pk=product.pk %">Colours</a>
</li>
The problem is that this works only when I'm on the Product Details page because the base template can interpret what product.pk is. But as soon as I click to go to the Colours link in the navbar, it tries to render the base template and all of the sudden it has no idea what product.pk is.
This is because the Product Details page uses the Product model through ProductUpdateView (which I can then reference product.pk with) but the Colours page uses the ProductColour model through ProductColourListView which in that case it would be colours.product.pk.
It's like I need the base template to understand where to grab the pk from depending on whether I'm coming from the Product Details page, the Colours page or some other page.
django django-templates
django django-templates
edited Nov 10 at 9:34
qiAlex
2,0201723
2,0201723
asked Nov 10 at 9:10
Tamir
32
32
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Why don't you put the product in the context in the view that does not manipulate your product as the single object ?
class ProductColourListView(generic.ListView):
model = models.ProductColour
def get_context_data(self, **kwargs):
kwargs.setdefault('product', put_here_your_product)
return super().get_context_data(**kwargs)
Thank you @mistiru. I didn't realise that you could do that. I'm a bit new to class based views and that these classes have predefined methods that you can override. Thanks again that was really helpful.
– Tamir
Nov 10 at 22:09
You're welcome! Class based view can be a little confusing at first as there is a lot of hidden work done in a lot of different places, but you'll quickly get used ;)
– mistiru
Nov 10 at 23:54
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Why don't you put the product in the context in the view that does not manipulate your product as the single object ?
class ProductColourListView(generic.ListView):
model = models.ProductColour
def get_context_data(self, **kwargs):
kwargs.setdefault('product', put_here_your_product)
return super().get_context_data(**kwargs)
Thank you @mistiru. I didn't realise that you could do that. I'm a bit new to class based views and that these classes have predefined methods that you can override. Thanks again that was really helpful.
– Tamir
Nov 10 at 22:09
You're welcome! Class based view can be a little confusing at first as there is a lot of hidden work done in a lot of different places, but you'll quickly get used ;)
– mistiru
Nov 10 at 23:54
add a comment |
up vote
0
down vote
accepted
Why don't you put the product in the context in the view that does not manipulate your product as the single object ?
class ProductColourListView(generic.ListView):
model = models.ProductColour
def get_context_data(self, **kwargs):
kwargs.setdefault('product', put_here_your_product)
return super().get_context_data(**kwargs)
Thank you @mistiru. I didn't realise that you could do that. I'm a bit new to class based views and that these classes have predefined methods that you can override. Thanks again that was really helpful.
– Tamir
Nov 10 at 22:09
You're welcome! Class based view can be a little confusing at first as there is a lot of hidden work done in a lot of different places, but you'll quickly get used ;)
– mistiru
Nov 10 at 23:54
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Why don't you put the product in the context in the view that does not manipulate your product as the single object ?
class ProductColourListView(generic.ListView):
model = models.ProductColour
def get_context_data(self, **kwargs):
kwargs.setdefault('product', put_here_your_product)
return super().get_context_data(**kwargs)
Why don't you put the product in the context in the view that does not manipulate your product as the single object ?
class ProductColourListView(generic.ListView):
model = models.ProductColour
def get_context_data(self, **kwargs):
kwargs.setdefault('product', put_here_your_product)
return super().get_context_data(**kwargs)
answered Nov 10 at 9:45
mistiru
40012
40012
Thank you @mistiru. I didn't realise that you could do that. I'm a bit new to class based views and that these classes have predefined methods that you can override. Thanks again that was really helpful.
– Tamir
Nov 10 at 22:09
You're welcome! Class based view can be a little confusing at first as there is a lot of hidden work done in a lot of different places, but you'll quickly get used ;)
– mistiru
Nov 10 at 23:54
add a comment |
Thank you @mistiru. I didn't realise that you could do that. I'm a bit new to class based views and that these classes have predefined methods that you can override. Thanks again that was really helpful.
– Tamir
Nov 10 at 22:09
You're welcome! Class based view can be a little confusing at first as there is a lot of hidden work done in a lot of different places, but you'll quickly get used ;)
– mistiru
Nov 10 at 23:54
Thank you @mistiru. I didn't realise that you could do that. I'm a bit new to class based views and that these classes have predefined methods that you can override. Thanks again that was really helpful.
– Tamir
Nov 10 at 22:09
Thank you @mistiru. I didn't realise that you could do that. I'm a bit new to class based views and that these classes have predefined methods that you can override. Thanks again that was really helpful.
– Tamir
Nov 10 at 22:09
You're welcome! Class based view can be a little confusing at first as there is a lot of hidden work done in a lot of different places, but you'll quickly get used ;)
– mistiru
Nov 10 at 23:54
You're welcome! Class based view can be a little confusing at first as there is a lot of hidden work done in a lot of different places, but you'll quickly get used ;)
– mistiru
Nov 10 at 23:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237506%2fdjango-navbar-how-i-do-pass-the-product-pk-to-each-different-link%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown