Have a template view determine model data in subsequent view

I would like the link that a user clicks on to instantiate a model differently on a subsequent view.

I have a page ‘tools.html’ that is a templateView:

Urls.py

    path(
        "tools/",
        TemplateView.as_view(template_name="pages/tools.html"),
        name="tools",
    )

tools.html has two links (AP and AD) on it - both should go to the same template ’se_balance.html’ a page where users complete a form.

The view for se_balance.html is:

class CaseView(TemplateView):
    model = Case
    template_name = "../templates/se_balance.html"
    
    def get(self, *args, **kwargs):
        print('getting')
        case_form = CaseForm
        sideeffect_formset = SideeffectFormSet(queryset=SideEffect.objects.none())

        return self.render_to_response(
            { "case_form": case_form,
              "sideeffect_formset": sideeffect_formset,
              "sideeffect_formsethelper": SideEffectFormSetSetHelper,
            }
        )

The ‘Case ‘ model is as follows:

class Case(TimeStampedModel):
    # get a unique id for each patient - could perhaps use this as slug if needed
    case_id = models.UUIDField(
        primary_key=True, unique=True, default=uuid.uuid4, editable=False
    )
    # Remove blank=True if we want to make login compulsory
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.SET_NULL
    )

    P_CHOICES = [(‘AD’, ‘AD’), 
                 (‘AP’, ‘AP’),]

Depending on the link that was clicked at tools.html I would like the model instantiated differently (i.e.e with P_CHOICEs as Either ‘AD’ or ‘AP’ but I am not sure of the correct way to do this?

Thanks!

You could store the selection in the session, database, cookies, local storage or add it to the querystring. If you want to prevent the users from manipulating it, storing in the session or database would be better since you control when that gets set. Session, cookies and localstorage could impact subsequent users if it’s not tied to a specific user. The database storage would likely require you to have the user be authenticated. If it’s all the same, I’d use the querystring because it’s the simplest in terms of overall impact of the web app.

In any case, you should determine what happens if they browse to the subsequent page without making a selection and handle that flow gracefully.