For anyone who wants to know how to get the foreign key related instances, you have to use model_name_set.all function in your template. For example, a blog post has many comments, so when you try to get the comments through the post, you need to use "{% for comment in post.comment_set.all %}" in your template. If you do not understand what I am saying, go to the site: developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Generic_views This is the most useful and detailed tutorial I have seen so far! Good job!
Great tutorials man. Keep up the good work but here's a side note. For the IndexView the code is simple enough to just use the default implementation. So instead of overriding get_queryset all you have to do is override the model attribute to use the model you want. INSTEAD OF: class IndexView(generic.ListView): template_name = 'template.html' def get_queryset(self): return Album.objects.all() YOU CAN JUST DO: class IndexView(generic.ListView): model = Album template_name = 'template.html' And that's it!!! ALSO(from the Django docs): " If context_object_name is not set, the context name will be constructed from the model_name of the model that the queryset is composed from. For example, the model Article would have context object named 'article'."
don't follow the tuts blindly or you will just be entering what he enters in. try to customize it to your own so you are learning as you go. you might run into some problems but fixing the problems and learning from them is what really makes you understand the workings of Django (probably anything for that matter).
Everytime I learn something, I think "But what if this or that?" and I try it, in that way I learn more, for example, "What if instead of passing just context_object_name = 'album_list'" you want to pass multiple objects to the template like before when we had a dictionary, I´ll try to learn this even if it´s not part of the tutorial (at least so far)
Hey ! (5:13) - in vews, class IndexViews, how can we repesent Album and Song models in the same class ? I tried to do : def get_queryset(self): return Album.objects.all() def get_queryset(self): return Song.objects.all() But it doesn't work Please, help !
I'm not sure why this worked for Bucky without adding a context_object_name = album to the DetailView class. I'm not following his music/albums format, but mine is similar and I had to add the context object name to show a valid details page.
i didn't notice that bucky removed if all_albums condition and debugged django's work a little. when you give another name to object_list it doesn't disappear from code and you still can use it as well as new given name. just for your information.
Anyone getting this error: 'as_view() takes 1 positional argument but 2 were given' was caused from omitting the parentheses in the urls.py details as_view.... It should look like this, if you use re_path. re_path(r'^(?P[0-9]+)/$', views.DetailView.as_view(), name='detail'),
Hello everyone..Please can anyone help me out... When I use object_list I am getting error.. As NoReverseMatch Reverse for detail with arguments'(",)' not found. 1 pattern (s) tried : ['music/(?P[0-9]+)/$]
The biggest challenge to me in following this old tutorial today Dec, 2018. I'm having issues with "urlpatterns" and now the requirement to use "pk" or "slug" which wasn't required here. or maybe i'm just doing something wrong. Either way this tutorial is very good.
what if i need to send an error or warning message to html page? earlier we used dictionary for the saem.. {'album': all_albums , 'error_message' : 'no album found'} ?
how do you add songs, I have the SongCreate modelform made - added song_add to my urls and have tried everything I can think of in song_form.html? I am sure it is simple but I can't see it help?
Are pk and id really interchangeable? I tried to use id instead of pk in the urlpatterns, but it didn't work. When I use pk it works but it didn't work with id. Why is that? please help me
Hi Bucky, How did you add a custom icon on title bar. I tried to add in base.html file and its not working. :( when opening the base.html it shows but not through Django server page
You said that pk and id are interchangeable and we can use either one. I tried to use id instead of pk in the urlpatterns, but it didn't work. When I use pk it works but it didn't work with id. Why is that?
I am assuming you have the problem with DetailView. Since I don't have your code I can't tell you what's wrong. Here is my code for your reference. music/urls.py # /music// url(r'^(?P[0-9]+)/$', views.DetailView.as_view(), name='detail'), music/views.py class DetailView(generic.DetailView): model = Album template_name = 'music/detail.html' If this doesn't help you then you will have to provide your code to check.
I struggled to get a detail view. The cause of the blank detail view page was the wrong object name used in the for loop in detail.html. Mine said refer.song_set.all which was pointing to an object that was no longer an instance of the class Album because the DetailView function set model = Album. This is the new instance of the class Album. So change your for loop in detail.html to {% for song in album.song_set.all%} and that should fix it.
Thank you very much Mr Bucky. Very Nice videos. I am the Beginner for Python and DJango. Very nice explanation in all the videos. 29th video is more confusing. The pattern for the LIST VIEW is completely changed from Video-28 to Video-29. How did you get this change ? Please explain. We will be very much thankful to you. Any advanced DJango online courses you r offering? what is the cost ?
django.urls.exceptions.NoReverseMatch: Reverse for 'favorite' not found. 'favorite' is not a valid view function or pattern name. [10/Apr/2020 14:58:23] "GET /music/1/ HTTP/1.1" 500 159601
This doesnt work for me. I am getting this error when i click any of the album NoReverseMatch at /music/3/ Reverse for 'favorite' not found. 'favorite' is not a valid view function or pattern name.
When I click on any of albums it shows like NoReverseMatch at /2/ Reverse for 'favorite' not found. 'favorite' is not a valid view function or pattern name. why this error comes?
Whilst its all good having a more compact code, alot of the code that got deleted from views.py was error checking incase no records were found in the database, unless Bucky goes on to add this in a later the tutorial i do not see the benefit of this if it means there is no error checking included
I thought this was the case too, but I tested out the same things we did a while back and all of what we already did with view functions is handled. Its almost like magic!
I used comments to explain the optional renaming of object_list in my views.py right under "template_name". Funny thing: Then it generates an error message. If I delete the comment, everything works. Does anyone have an explanation for that?
Im following along using Django 2 , and the object list doesnt work so use the all_albums with the context if anyone else is having issues with that too
now i figures how bucky can do so much coding without making mistakes or stutter. now you probably just gonna say 'nah i think you are just bad', It appears that the recorded window is slightly smaller than the desktop size, which is abnormal. I think he have the readied script outside of the recording area, and code according to the precoded code :) nice trick haha
how come the detail view know that the object name is 'album' in detail.html ?? is it the lower case of the "model" name mentioned in DetailView class ???
Django automatically chooses an appropriate context variable name based on the model name ('Album' in this case, which is converted to the lowercase 'album' when being passed to the template). See here for more details, about 3/4 of the way down: docs.djangoproject.com/en/1.10/intro/tutorial04/
I am using similar code but i am not getting albums view . Here's my code : view.py from django.views import generic from models import Album class IndexView(generic.ListView): template_name = "music/index.html" def get_queryset(self): return Album.objects.all() class DetailView(generic.DetailView): model = Album template_name = "music/detail.html"
i am wondering what happened using generic . passing the id of album , the def function how to deal with querying in all albums . i can't figure the code model = Album out ?
Great tutorial, but only if i wanted to make exactly what you're showing. Can't make after hours what i want to make. People should write documentation to be readable by people that don't actually already know wtf something is...
Oh god! I lost a lot of time going back and forth. Man, why didn't you start with Generic Views since the beginning?. It is like if you were doing OOP and moving everything again to Functional Programming. I feel so frustrated. LOL
For anyone who wants to know how to get the foreign key related instances, you have to use model_name_set.all function in your template. For example, a blog post has many comments, so when you try to get the comments through the post, you need to use "{% for comment in post.comment_set.all %}" in your template. If you do not understand what I am saying, go to the site: developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Generic_views
This is the most useful and detailed tutorial I have seen so far! Good job!
Well done imitation of designer :)
8:40 - 8:50
Great tutorials man. Keep up the good work but here's a side note. For the IndexView the code is simple enough to just use the default implementation. So instead of overriding get_queryset all you have to do is override the model attribute to use the model you want.
INSTEAD OF:
class IndexView(generic.ListView):
template_name = 'template.html'
def get_queryset(self):
return Album.objects.all()
YOU CAN JUST DO:
class IndexView(generic.ListView):
model = Album
template_name = 'template.html'
And that's it!!!
ALSO(from the Django docs):
" If context_object_name is
not set, the context name will be constructed from the model_name
of the model that the queryset is composed from. For example, the model
Article would have context object named 'article'."
Thanks. Didn't know about the context_object_name part.
Never everrrr have I seen a clearer explanation of Generic views than this oneeee✨
Simply the best programming channel on YT
Love Django, love Buckey, you're the man, best Django tutorial on the net!
I like your impression of a web designer.
I reflectively hit Ctrl-z on keyboard after you delete all the original views...
Those Darn Web Designers! Lol I laughed so much at that.
Is it weird that I still think Django is confusing by the time 29 tuts have passed?
+Mr_Teacher _Man It is complex at the start but it is awesome when you will get it.
+Yuriy Reshetylo I''ll just continue watching Bucky's tutorials. Hopefull I''l understand it after a certain amount of time.
+Mr_Teacher _Man
i know your feeling, don't give up! :)
suddenly it became much more clear to me
don't follow the tuts blindly or you will just be entering what he enters in. try to customize it to your own so you are learning as you go. you might run into some problems but fixing the problems and learning from them is what really makes you understand the workings of Django (probably anything for that matter).
Everytime I learn something, I think "But what if this or that?" and I try it, in that way I learn more, for example, "What if instead of passing just context_object_name = 'album_list'" you want to pass multiple objects to the template like before when we had a dictionary, I´ll try to learn this even if it´s not part of the tutorial (at least so far)
model = Album is the same thing as query_set = Album.objects.all and it's the shortcut way of def get_queryset()
Thank you, I was missing out on some points and your video took away my doubt.
Finally I understand generic views! :)
hey man! thanks for all these.. I really enjoy these trainings, and learned a lot over the last 2 weeks following your tutorials ;)
Hey !
(5:13) - in vews, class IndexViews, how can we repesent Album and Song models in the same class ?
I tried to do :
def get_queryset(self):
return Album.objects.all()
def get_queryset(self):
return Song.objects.all()
But it doesn't work
Please, help !
very good presentation, you cleared up a troublesome subject for me...thanks
thanks for tutorials ..
quik question.....what if out detail view depends on many models not just one???
following!
Amazing video thank you I've been looking forever for this!!
Loved this video , thanks for your clear explanation
I felt the generic views as simple rather than the earlier complex design.....😅 Thanks bucky your Tutorials are amazing
NoReverseMatch at /music/1/
Reverse for 'favorite' not found. 'favorite' is not a valid view function or pattern name.
I know this is old, but just remove the stuff for favouriting like he did.
Thanks for an excellent set of tutorials!
I'm not sure why this worked for Bucky without adding a context_object_name = album to the DetailView class. I'm not following his music/albums format, but mine is similar and I had to add the context object name to show a valid details page.
I also got sae issue ... your comment saved me .... Billion Thanks :) even I wonder how his code was working fine ???
:/
Thanks dude, great explanation and helped lot. Thanks a lott
i didn't notice that bucky removed if all_albums condition and debugged django's work a little. when you give another name to object_list it doesn't disappear from code and you still can use it as well as new given name. just for your information.
if you want to import reverse, import it from django.urls
from django.urls import reverse
Anyone getting this error:
'as_view() takes 1 positional argument but 2 were given'
was caused from omitting the parentheses in the urls.py details as_view....
It should look like this, if you use re_path.
re_path(r'^(?P[0-9]+)/$', views.DetailView.as_view(), name='detail'),
That is beautiful, thanks for showing to us!
im having a rough time validating a form that has a "fk" of a class i have already created using a createclassview class. Any tutorial on that matter?
Thus is just what i wanted, thanks a lot
Git's really helpful, when bucky's doing some big changes, you can save a snapshot of that code by commiting.
Or you could just comment it out, otherwise the next commit will overwrite it.
i think this is kind of not logical especially DetailView how can URL know the pk = album.id
Hello everyone..Please can anyone help me out... When I use object_list I am getting error..
As NoReverseMatch Reverse for detail with arguments'(",)' not found. 1 pattern (s) tried : ['music/(?P[0-9]+)/$]
The biggest challenge to me in following this old tutorial today Dec, 2018. I'm having issues with "urlpatterns" and now the requirement to use "pk" or "slug" which wasn't required here. or maybe i'm just doing something wrong. Either way this tutorial is very good.
I'm having the same problem, help me!
what if i need to send an error or warning message to html page? earlier we used dictionary for the saem.. {'album': all_albums , 'error_message' : 'no album found'} ?
how do you add songs, I have the SongCreate modelform made - added song_add to my urls and have tried everything I can think of in song_form.html?
I am sure it is simple but I can't see it help?
I have 3-week experience in Django but still get confused by its relationship among files and variables
Thank you for the lesson! very informative
I had an issue while wanting to display a detail of an object from DB. Instead of object_list put simply object and that is it. It will be displayed.
Strange. In my code all_album returns the list of albums but object_list doesn't. I didn't have to set context_object_name.
Are pk and id really interchangeable? I tried to use id instead of pk in the urlpatterns, but it didn't work.
When I use pk it works but it didn't work with id. Why is that? please help me
Hi Bucky,
How did you add a custom icon on title bar. I tried to add in base.html file and its not working. :( when opening the base.html it shows but not through Django server page
You said that pk and id are interchangeable and we can use either one. I tried to use id instead of pk in the urlpatterns, but it didn't work.
When I use pk it works but it didn't work with id. Why is that?
as_view() takes 1 positional argument but 2 were given
found this error can you help me?
TypeError at /music/1/
as_view() takes 1 positional argument but 2 were given
I am assuming you have the problem with DetailView. Since I don't have your code I can't tell you what's wrong.
Here is my code for your reference.
music/urls.py
# /music//
url(r'^(?P[0-9]+)/$', views.DetailView.as_view(), name='detail'),
music/views.py
class DetailView(generic.DetailView):
model = Album
template_name = 'music/detail.html'
If this doesn't help you then you will have to provide your code to check.
just use "pk" instead of "id" keyword... Turns out it actually matters which one u use...
What does view:detail mean in the url-link of index.html? In which video is it? I don't have much time and that is why i am asking. Cheers.
I struggled to get a detail view. The cause of the blank detail view page was the wrong object name used in the for loop in detail.html. Mine said refer.song_set.all which was pointing to an object that was no longer an instance of the class Album because the DetailView function set model = Album. This is the new instance of the class Album. So change your for loop in detail.html to {% for song in album.song_set.all%} and that should fix it.
Thanks, Bucky. How do you go about learning quickly and efficiently?
Hi Bucky, Please show us how to add songs, its hard for me to do it since it has a foreign key.
why there is no return in DetailView class as same as in IndexView??
Would you do a more in depth tutorial on CBVs? I'd be really interested in heavily customized form control.
Thank you very much Mr Bucky. Very Nice videos. I am the Beginner for Python and DJango. Very nice explanation in all the videos. 29th video is more confusing. The pattern for the LIST VIEW is completely changed from Video-28 to Video-29. How did you get this change ? Please explain. We will be very much thankful to you. Any advanced DJango online courses you r offering? what is the cost ?
Hey nice nav bar,, how can I get something like that,, refer me to a tutorial where this was made please
django.urls.exceptions.NoReverseMatch: Reverse for 'favorite' not found. 'favorite' is not a valid view function or pattern name.
[10/Apr/2020 14:58:23] "GET /music/1/ HTTP/1.1" 500 159601
in your detail.html page change that favorite to details
Thanks for perfect teaching Bucky!
How does django recognize where to place "Album" model, declared in DetailView, in template 'music/detail.html'?
At 8:16 he explains that the queryset is automatically stored in the variable object_list for use in the html-files
Inhale Death: I think object_list is used only for ListView. My question was related to DetailView.
detail.html doesn't have Form code in video 29 which was there in 28
Reverse for 'favorite' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: [] ... i m facing this error .. please help
had the same problem, look at detail.html and compare to his detail.html at 2:10 made my page work now
what you did to resolve it ?
Have you deleted your old code? If you comment out all it will not work :D
Thank you very much
Thanks, It really helped, but can you tell why it behaves that way???????
i still don't get the generic views... i understand how it works but i don't know why is better than simply get object and render() it...
Its just a little less code to type
oh my god ! what are you doing !!
Its not displaying any logo for my album. What should I do?
This doesnt work for me. I am getting this error when i click any of the album
NoReverseMatch at /music/3/
Reverse for 'favorite' not found. 'favorite' is not a valid view function or pattern name.
did you delete the favourite function?
yes
( i am also getting same error)
hey ..It solved i was forgot to clean detail
might be you too
change that -from django.core.urlresolvers import reverse- to that -from django.urls import reverse-
When I click on any of albums it shows like
NoReverseMatch at /2/
Reverse for 'favorite' not found. 'favorite' is not a valid view function or pattern name.
why this error comes?
Whilst its all good having a more compact code, alot of the code that got deleted from views.py was error checking incase no records were found in the database, unless Bucky goes on to add this in a later the tutorial i do not see the benefit of this if it means there is no error checking included
I thought this was the case too, but I tested out the same things we did a while back and all of what we already did with view functions is handled. Its almost like magic!
I used comments to explain the optional renaming of object_list in my views.py right under "template_name". Funny thing: Then it generates an error message. If I delete the comment, everything works. Does anyone have an explanation for that?
so where the favorite function go?
u'music' is not registered namespace
can anyone help me with this
Thank you, Bucky.
AttributeError: module 'music.views' has no attribute 'detail'
Im following along using Django 2 , and the object list doesnt work so use the all_albums with the context if anyone else is having issues with that too
I'm facing the same. Have you found the solution?
thank u Bucky.very useful
Why didnt you make a video for creating songs using foreign key?
after making this changes it show me No Album.....Why???
pls help
If you used {% if all_albums %} in index.html , replace it with {% if object_list %}
+Aqib Zaheer,
Did u fix it? I am also having the same problem
I had the same error and after I made the changes in the index.html file, it was fixed. the if-else block should be removed.
@@sam4077kr You saved me, cheers!
@@sam4077kr YOU SAVED MEEEEEEEEEEEEEEEEEEEEEEE!!!!!!!!!!!!!!! thanks thanks thanks
I can see all the albums but for some reason the details page remains blank even after object_list explanation. Anyone with a similar issue?
I'm also having the same issue
same for me
just review your codes, Bucky has some changes in detail.html
Tommy Alfarabi
yup..i overlooked it thinking it was just bootstrap styling. Thanks!
mixins are still impossible for me to grasp though in class based views
2:40 i will still love u no matter what u do.
Hey bucky how can I add an icon with the title of the page????
Look up favicons and how to use them
now i figures how bucky can do so much coding without making mistakes or stutter. now you probably just gonna say 'nah i think you are just bad', It appears that the recorded window is slightly smaller than the desktop size, which is abnormal. I think he have the readied script outside of the recording area, and code according to the precoded code :) nice trick haha
Exis Zhang everyone does that. That other code is reference code, which he didn't blindly copy. It takes effort and understanding.
i have problems with my details.html
i did not delete i commented out all of them, they mean a lot to me :(
QuerySet' object has no attribute '_default_manager'
help me
Generic detail view DetailsView must be called with either an object pk or a slug.
how come the detail view know that the object name is 'album' in detail.html ?? is it the lower case of the "model" name mentioned in DetailView class ???
Django automatically chooses an appropriate context variable name based on the model name ('Album' in this case, which is converted to the lowercase 'album' when being passed to the template). See here for more details, about 3/4 of the way down: docs.djangoproject.com/en/1.10/intro/tutorial04/
still dont get it. i watched every single one in youtube. i dont get it. please someone make a dummies version of this nightmare..
How about now?
Django tutorial 29 Generic Views
apps used in this tutorial
\music\templates\music\index.html
\music\templates\music\detail.html
\music\urls .py
\music\views .py
Django makes me want to make a cool and useful website but i never have any ideas :/
thats your first problem, help me build mine. Give me your email, and ill email you.
@Flavio if you are looking for volunteers am in.
akagilnc@gmail.com
Ningchuan Li I dont think I want to get a partner right now for this cause I am linda busy. Sorry guys
+Flavio Andrade never mind.
yes btu he didnt mention updating objects and deleting objects ...he only told part a story
everything will be correct ,we just have to remove/delete this part
{% csrf_token %}
{% for song in album.song_set.all %}
{{ song.song_title}}
{% if song.is_favorite %}
{% endif %}
{% endfor %}
from detail.html file
I am using similar code but i am not getting albums view . Here's my code :
view.py
from django.views import generic
from models import Album
class IndexView(generic.ListView):
template_name = "music/index.html"
def get_queryset(self):
return Album.objects.all()
class DetailView(generic.DetailView):
model = Album
template_name = "music/detail.html"
i am getting albums view , but not DetailView ....
i am wondering what happened using generic . passing the id of album , the def function how to deal with querying in all albums . i can't figure the code model = Album out ?
i have fixed my problem , any code runs ok now . 'cause generic.DateDetailView i wrote , it should be generic.DetailView. my bad .
Use "from .models import Album" you are missing the dot there. Hope it helps.
Why on the git these class didnt appear?
github.com/buckyroberts/Viberr/blob/master/music/views.py
now I'm completely lost !!
Great tutorial, but only if i wanted to make exactly what you're showing. Can't make after hours what i want to make. People should write documentation to be readable by people that don't actually already know wtf something is...
S M A R T I G O
Thankyou sir
I love Django. If you have any project I can make it for you. I am freelancer, cheap cause I am leaving in Central Europe.
Who on Earth found out Django ????????
I like and hate django simultaneously!
nice video
Oh god! I lost a lot of time going back and forth. Man, why didn't you start with Generic Views since the beginning?. It is like if you were doing OOP and moving everything again to Functional Programming. I feel so frustrated. LOL
讲的还算可以,希望下次少讲一些知识,多多传授方法,比如怎么通过一步一步阅读django官方文档来实现和完善一个小应用
Bucky forgot to tell us to add context_object_name under DetailView
How to add that?
EVERYTHING I LEARN IS DELETEDDDDDDDDD
hehe
let me delete every sh.... umm thing😂😂😂🤣🤣🤣
Delete favorite class.......Nooooooooooooo...
NoReverseMatch at /music/1/
Reverse for 'favorite' not found. 'favorite' is not a valid view function or pattern name.
In detail.html