Dear django-users,
In an effort ot learn Django, i am writing a blog post, "Simple Django
Tutorial
<https://joecodeswell.wordpress.com/2016/05/19/simple-django-tutorial/#untested>",
to remind myself & possibly others of some Django Basics.
I ALMOST finished when i noticed some strange (to me) behaviour of the
`template_name` in a CBV.
Here are the relevant CBVs:
class SimpleSearchListContactView(ListView):
model = Contact
template_name = 'contacts/contact_list.html'
def get_queryset(self):
qs = super(SimpleSearchListContactView, self).get_queryset()
return qs.filter(first_name__contains=self.kwargs['fname'])
# "first_name" is the first_name field in our Contacts model
# "contains" is one of Django's "Field lookups"
# "'fname'" is passed in from our url <fname> Regex Named Group
class ParametricSearchListContactView(ListView):
model = Contact
template_name = 'contacts/contact_list.html'
def get_queryset(self):
qs = super(ParametricSearchListContactView, self).get_queryset()
field_lookups_dict = {
'%s__%s'%(self.kwargs['field_name'], self.kwargs['match_cmd']):
self.kwargs['value'],
# for a def of each match_cmd see Field lookups -
https://docs.djangoproject.com/en/1.9/ref/models/querysets/#field-lookups
}
print ("ParametricSearchListContactView")
print (ParametricSearchCRUDListContactView.template_name)
print (ParametricSearchListContactView.template_name)
print (ListView.template_name)
return qs.filter(**field_lookups_dict)
class SimpleSearchCRUDListContactView(ListView):
model = Contact
template_name = 'contacts/crudlist.htm'
def get_queryset(self):
qs = super(SimpleSearchCRUDListContactView, self).get_queryset()
return qs.filter(first_name__contains=self.kwargs['fname'])
# "first_name" is the first_name field in our Contacts model
# "contains" is one of Django's "Field lookups"
# "'fname'" is passed in from our url <fname> Regex Named Group
class ParametricSearchCRUDListContactView(ListView):
model = Contact
template_name = 'contacts/crudlist.htm'
def get_queryset(self):
qs = super(ParametricSearchCRUDListContactView, self).get_queryset()
field_lookups_dict = {
'%s__%s'%(self.kwargs['field_name'], self.kwargs['match_cmd']):
self.kwargs['value'],
# for a def of each match_cmd see Field lookups -
https://docs.djangoproject.com/en/1.9/ref/models/querysets/#field-lookups
}
print ("ParametricSearchCRUDListContactView")
print (ParametricSearchCRUDListContactView.template_name)
print (ParametricSearchListContactView.template_name)
print (ListView.template_name)
return qs.filter(**field_lookups_dict)
The 2nd pair of classes are the *same as* the first pair EXCEPT:
template_name = ‘contacts/crudlist.html’ NOT
‘contacts/contact_list.html’
Class names are new in 2 places in each class:
“SimpleSearchCRUDListContactView”
“ParametricSearchCRUDListContactView”
Here are the relevant URLs:
url(r'^ssearch/(?P<fname>[\w-]+)/$', contacts.views.
SimpleSearchListContactView.as_view(), name='simplesearchlistcontactview',
),
url(r
'^psearch/(?P<field_name>[\w-]+)/(?P<match_cmd>[\w-]+)/(?P<value>[\w-]+)/$',
contacts.views.ParametricSearchListContactView.as_view(), name=
'parametricsearchlistcontactview', ),
url(r'^ssearchc/(?P<fname>[\w-]+)/$', contacts.views.
SimpleSearchCRUDListContactView.as_view(), name='ssearchc', ),
url(r
'^psearchc/(?P<field_name>[\w-]+)/(?P<match_cmd>[\w-]+)/(?P<value>[\w-]+)/$'
, contacts.views.ParametricSearchCRUDListContactView.as_view(), name=
'psearchc', ),
*I EXPECT* the Views with CRUD in their names (CRUD Views) to behave THE
SAME AS the others EXCEPT items are listed with read, update & delete
links, because that is how `contacts/crudlist.htm` displays them.
*THE PROBLEM IS *that the CRUD Views display the items using the Plain
Template (contacts/contact_list.html) NOT the CRUD Template (
'contacts/crudlist.htm')
I.E. Navigating to
`http://127.0.0.1:8000/contacts/psearchc/last_name/contains/d/`
*YIELDS *
Plain Contacts
- Joe Codeswell [email protected]
- Jayne Mansfield [email protected]
*NOT AS EXPECTED*
CRUD Contacts Create <http://127.0.0.1:8000/contacts/create>
- Joe Codeswell [email protected] Read | <http://127.0.0.1:8000/contacts/read/1>
Update
| <http://127.0.0.1:8000/contacts/update/1> Delete
<http://127.0.0.1:8000/contacts/delete/1>
- Jayne Mansfield [email protected] Read |
<http://127.0.0.1:8000/contacts/read/3> Update |
<http://127.0.0.1:8000/contacts/update/3> Delete
<http://127.0.0.1:8000/contacts/delete/3>
Also, for the same navigation URL, in the Server Status Window the prints
yield
[18/Jun/2016 11:16:26] "GET /contacts/psearchc/last_name/contains/d/
HTTP/1.1" 200 218
ParametricSearchCRUDListContactView
contacts/crudlist.htm
contacts/contact_list.html
None
I hope someone can tell me what's going on here.
Thanks in advance.
Love and peace,
Joe
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/5cf7a8fd-2415-42d0-bc6b-b042a02e8c7f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.