Reputation: 23
I am creating a website where i am using django database.The database will have one def called categories and one called gifi.So a gif will belong to a category and when one category will be clicked it will send the user to a page where all gifs that belong to that category will show up.I know that i should do something like requesting ID but i dont know the code
This is the models:
from django.db import models
class categorite(models.Model):
name = models.CharField(max_length=100)
id = models.AutoField(primary_key=True)
class Gifi(models.Model):
foto = models.ImageField(upload_to='website/static/')
emri = models.CharField(max_length=100)
Source = models.CharField(max_length=100)
Kodet = models.CharField(max_length=12)
categoryId = models.ForeignKey(categorite, on_delete=models.CASCADE)
id = models.AutoField(primary_key=True)
This is views:
from django.shortcuts import render,get_object_or_404
from .models import Gifi,categorite
# Create your views here.
def home(request):
return render(request, 'website/home.html')
def categories(request):
content = {
'view': categorite.objects.all()
}
return render(request, 'website/categories.html',content)
def PostaCode(request):
return render(request, 'website/PostaCode.html')
def Contact(request):
return render(request, 'website/Contact.html')
def category(request,id):
content = {
'view': categorite.objects.get(id=id)
}
return render(request, 'website/category.html',content)
This is urls:
from django.urls import path
from . import views
urlpatterns = [
path('Home/',views.home, name='home'),
path('Categories/', views.categories, name='categories'),
path('PostaCode/', views.PostaCode, name='PostaCode'),
path('Contact/', views.Contact, name='Contact'),
path('Category/<int:id>/', views.category, name='category'),
]
This is the code that i use to show categories on my categories.html:
<div class="row p">
{%for categorite in view%}
<a class="c" href="{% url 'category' categorite.id %}">{{categorite.name}}</a>
{% endfor %}
</div>
This video gives a quick show, how website is working now: https://vimeo.com/user122082575/review/451987151/da98e3f75c
Upvotes: 2
Views: 117
Reputation: 520
In your views when you are asking for Gifi
you should use the <int:id>
which you provided from the url. So it will be something like this in your views: Gifi.objects.filter(categoryId_id=id)
then you can pass that in your context.
Remarks that you have to fix:
categorite
is lower case it should Categorite
also name always in english.Source, Kodet
should be lower case, following the python convention.id
is obsolete since that you always have pk
and django knows when to convert id
to pk
while filtering results.categoryId
should be just category
PostaCode
-> posta_code
Being consistent with your code is mandatory always follow the conventions for each language. For python that would be pep8 or black please make sure you know these two.
Upvotes: 2
Reputation: 52028
You can get the data using filter
like this:
# view
def category(request,id):
content = {
'view': Gifi.objects.filter(categoryId_id=id)
}
return render(request, 'website/category.html',content)
# template
<div class="row p">
{% for gifi in view %}
{{ gifi.emri }}
{% endfor %}
</div>
Alternatively, you can use related objects to get data(using reverse relation between Gifi
to Category
):
# view
def category(request,id):
content = {
'view': Category.objects.get(id=id)
}
return render(request, 'website/category.html',content)
# template
<div class="row p">
{{ view.name }} // category name
{% for gifi in view.gifi_set.all %}
{{ gifi.emri }} // gifi information
{% endfor %}
</div>
Upvotes: 1