I have problems passing kwargs parameters to class based views
urls.py
path('convertST', views.ConvertST.as_view(machine_model = None, machineST = None), name='convertST'),
views.py:
class ConvertST( FormView):
template_name = 'app_fabric/machineST_create.html'
form_class = bmf.CommunitySTGlobalForm
machine_model = None
machineST = None
in another view, success url is to redirect to convertST
def get_success_url(self):
.....
return revers('app_name:convertST', kwargs = {'machineST' : '1'})
This raises the exception django.urls.exceptions.NoReverseMatch: Reverse for ‘convertST’ with keyword arguments ‘{‘machineST’: ‘1’}’ not found. 1 pattern(s) tried: [‘app_fabric/convertST\\Z’]
What is the proper way to pass kwargs to a class based_view?
It’s the same as how you pass them to FBVs - through the URL.
For this to work, your url should be something like: path('convertST/<str:machineST>/', views.ConvertST.as_view(), name='convertST'),
It’s more appropriate to set values for attributes like machine_model and machineST in the __init__ method instead of as class variables. Doing it the way you have it can create some very unpleasant side-effects.
Thanks for your reply. The view consertST requires 2 parameters which I dynamically collect through redirections. This means duplicating the line in the urls.py (oneline with 0 parameter, with 1 ad with 2 parameters).
Or, identify a default (sentinel) value that is always supplied in the absence of an override.
Or, you could use query variables instead of url parameters.
Keep in mind that there is no “direct” connection between your use of the reverse function and the view being called. You use reverse to create a url, which is then sent to the browser. It’s that url which the browser uses to request the page. If the variable isn’t in the URL, then the view doesn’t get it.
A class attribute applies to all instances of a class. For example, if you subclass TemplateView, and assign a value to the template_name attribute, all instances of that view will use the identified template.
Example:
class MyTemplateView_1(TemplateView):
template_name = 'template_1.html'
class MyTemplateView_2(TemplateView):
template_name = 'template_2.html'
Every URL resolving to MyTemplateView_1 will render template_1.html.
Every URL resolving to MyTemplateView_2 will render template_2.html.
If this is not clear, then you might want to review the Python documentation on classes, and the differences between class variables and instance variables.
Hello Thanks for taking the time to clarify. The **initkwargs which are in the viewname.as_view() are the parameters encoded in the url?
I now have
path('convertST/<machineST>/<machine_model>', views.ConvertST.as_view(), name='convertST'),
path('convertST/<machineST>', views.ConvertST.as_view(), name='convertST'),
path('convertST', views.ConvertST.as_view(), name = 'convertST'),parameters in a dictionnary which django function will encode the get_success_url
and I have the parameters in a dictionnary.
In get_successs_url I have
return redirect(view_name, kwargs=kwargs)
and it still returns the error. How should I encode the url?
For future reference, it always helps if you post the complete code with the complete error message and not try to send excerpts that may be lacking important context for understanding what’s happening. Also, copy/pasting the complete class or function reduces the possibility of introducing errors caused by a mistaken edit.