0

The problem is I can't type http://localhost:8000/1 to see spesific ViedoLibrary in my database. Can anyone help me to find the solution?

urls.py

urlpatterns = [ path('admin/', admin.site.urls), path('', views.index, name='index'), path('main/', views.main, name='main'), path('create_view/', views.create_view, name='create_view'), path('list_view/', views.list_view, name='list_view'), path('<id>', views.detail_view), ] 

views.py

def detail_view(request, id): context = {} context['data'] = VideoLibrary.objects.get(shop_id=id) return render(request, 'app/detail_view.html', context) 

models.py

class VideoLibrary(models.Model): shop_id = models.AutoField(primary_key=True, unique=True) shop_name = models.CharField(max_length=264) adress = models.TextField(max_length=264, default='') 

Also if I type id=id in views.py the next error pops up : Cannot resolve keyword 'id' into field. Choices are: adress, attendance, equipment, films, genre, income, shop_id, shop_name, staff.

This are all my classes in models.py but there are also adress, shop_id and shop_name that are from specific model

1 Answer 1

1

Updated.

You have overriden the id field of your model that is why it won't work with id=id since the latter is actually shop_id. Change it accordingly and it should work.

The id error is telling you that you cannot use id as a field and giving you the list of available model fields you can use for filtering. See this answer for more details. Error: Cannot resolve keyword 'id' into field

Update 2

Your views:

from . models import VideoLibrary def videoLibrary(request): context = {} context['data'] = VideoLibrary.objects.all() return render(request, 'appname/list_view.html', context) def detail_view(request, id): context = {} context['data'] = VideoLibrary.objects.get(shop_id=id) return render(request, 'appname/detail_view.html', context) 

Your urls.py:

urlpatterns = [ path('admin/', admin.site.urls), path('videos/', views.videoLibrary, name='list_view'), path('videos/<int:id>', views.detail_view), ] 

Now, if you enter http://127.0.0.1:8000/videos this gives you the list view. And http://127.0.0.1:8000/videos/1 gives you VideoLibrary with id 1.

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

7 Comments

this didn't help. what do you mean by saying that I don't have vidlibrary with id 0?
@unhaten see updated answer. Try adding a trailing slash too in your url path '<int:shop_id>/'
after editing my url path i got next error - detail_view() got an unexpected keyword argument 'shop_id @fkay
@unhaten I think I know why. Your URL should be <int:id> not shop_id as I mistyped in my previous comment. The id part of the argument in your url should match the argument given to the view you are rendering which in your case is id.
@unhaten see the updated answer with a sample code that works. As you can see it works as you intended in the first place.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.