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