A
A
authoraytee2021-06-24 09:43:23
Django
authoraytee, 2021-06-24 09:43:23

How to get value from GET request in django controller?

I have this request

path('file/<int:event>', CreateFileView.as_view(), name='add_file')

You need to get the event_id from here to the controller

The controller looks like this:

class CreateFileView(CreateView):
    model = FileFeedbackEventToSubscriber
    form_class = FileFieldForm
    template_name = 'file.html'
    
    success_url = reverse_lazy('filefeedback_events_home')

    def get_context_data(self, **kwargs):
        context = super(CreateFileView, self).get_context_data(**kwargs)

        context['current_user'] = self.request.user
        return context


What do I need to add where or how to make it work?
Or is there some other way to get the value of the current object without a request immediately from the controller?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
authoraytee, 2021-06-24
@authoraytee

self.kwargs['event']

M
Misha Tarasov, 2021-06-24
@Miha_Tarasov

Get the value from the kwargs dictionary stored by url and get parameters. And before using it in any methods, you need to pass it (**kwargs), but since it already exists in the get_context_data method, you just have to do this: (here you just pass the parameter name). self.kwargs['event']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question