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.










share|improve this question



























    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.










    share|improve this question

























      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.










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 9:34









      qiAlex

      2,0201723




      2,0201723










      asked Nov 10 at 9:10









      Tamir

      32




      32






















          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)





          share|improve this answer




















          • 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










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          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

























          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)





          share|improve this answer




















          • 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














          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)





          share|improve this answer




















          • 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












          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)





          share|improve this answer












          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)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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
















          • 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

















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo