0

I am listing data from a search result matching the name parameter of my Model. For each result I have a link to the detail page with the key parameter (which is a string) passed in the url, Like this

<ul>
    {% for x in results %}
    <li><a href = 'login/index/{{x.key}}'>{{x.name}}</a></li>
    {% endfor %}
</ul>

My url.py looks like this

app_name = 'kpi'
urlpatterns = [
    path('login/index/search', views.SearchView.as_view(), name="search"),
    path('login/index/<slug:key>', views.EpicDetailView.as_view(), name="detail")
]

And my views.py look like this:

class SearchView(LoginRequiredMixin, TemplateView):
    template_name = 'KPI/search.html'

    def get(self, request, *args, **kwargs):
        self.q = request.GET.get('q','')
        self.results = Epic.objects.filter(name__icontains= self.q)
        return super().get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(SearchView, self).get_context_data(**kwargs)
        context['results'] = self.results
        return context


class EpicDetailView(LoginRequiredMixin, TemplateView):
    template_name = 'KPI/epic_detail.html'

    def get_context_data(self, **kwargs):
        context = super(EpicDetailView, self).get_context_data(**kwargs)
        context['lone_epic2'] = Epic.objects.get(key=self.kwargs['key'])

I know I am missing a step here, probably how i created the url path, or needing logic in my views. With the code above i get a page not found error because the template view link doesn't recoginze/match the EpicDetailView url

What I'm Trying to Accomplish

The purpose is to be able to click on the search result, push the key to the url, and store that key value as a variable to use in the EpicDetailView page

NOTE: Although I'm not showing it in the code above, my detail view will be displaying data from multiple model querysets so that is why i'm using TemplateView instead of DetailView for my EpicDetailView

1 Answer 1

1

You should pass the slug into your URL like so in your template:

<li><a href = "{% url 'kpi:detail' x.key %}">{{x.name}}</a></li>

Make sure that kpi is properly registered as the namespace for your KPI app. More info on the URL dispatcher here: https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#url

Sign up to request clarification or add additional context in comments.

2 Comments

Ok this worked(it is registered as the namespace), how am i able to grab that parameter from the url to use in the detail page? with a request.GET.get()?
Nvm i did it with self.kwargs['key']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.