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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0