I am doing CS50 Project 2 and have a django model called Listing that has an object called Category that the user can pick using a dropdown list. All the categories are listed on the categories page and need to link to the categories_page page that displays all the listings in that category. Can you help me with the href and variable for the categories page to link it to the categories_page page?
categories views.py
def categories(request):
return render(request, "auctions/categories.html",{
"Miscellaneous": Listings.objects.all().filter(category=Miscellaneous)
})
categories.html (I need to link all the categories on this page to their own categories_page.)
{% block body %}
<h1>Categories</h1>
<a href="{% url 'categories' Miscellaneous.category %}" style = "color: rgb(58, 58, 58);"><h5>Miscellaneous</h5></a>
<h5>Movies and Television</h5>
<h5>Sports</h5>
<h5>Arts and Crafts</h5>
<h5>Clothing</h5>
<h5>Books</h5>
{% endblock %}
categories_page views.py
def categories_page(request, category):
listings = Listings.objects.all().filter(category=category)
return render(request, "auctions/categories_page.html",{
"listings": listings,
"heading": category
})
categories_page.html
{% block body %}
<h1>{{ heading }}</h1>
{% for listing in listings %}
<a href="{% url 'listing' listing.id %}" style = "color: rgb(58, 58, 58);">
<img src ="{{ listing.image }}" style = "height: 10%; width: 10%;">
<h4 class = "text">{{ listing.title }}</h4>
<h6>Description: {{ listing.description }}</h6>
<h6>Category: {{ listing.category }}</h6>
<h6>Price: ${{ listing.bid }}</h6>
</a>
{% endfor %}
{% endblock %
categories_page views and html both work. I am having trouble with the categories views and html.
Let me know if you need any more code. Thank you so much!
urls.py
from django.urls import path
from .models import User, Listings
from .forms import ListingsForm
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("login", views.login_view, name="login"),
path("logout", views.logout_view, name="logout"),
path("create", views.create, name="create"),
path("watchlist", views.watchlist, name="watchlist"),
path("categories", views.categories, name = "categories"),
path("categories/<str:category>/", views.categories_page, name = "categories_page"),
path("listing/<int:id>/", views.listing, name = "listing"),
path("register", views.register, name="register")
]
models.py
class Listings(models.Model):
CATEGORY = [
("Miscellaneous", "Miscellaneous"),
("Movies and Television", "Movies and Television"),
("Sports", "Sports"),
("Arts and Crafts", "Arts and Crafts"),
("Clothing", "Clothing"),
("Books", "Books"),
]
title = models.CharField(max_length=64)
description = models.CharField(max_length=500)
bid = models.DecimalField(max_digits=1000000000000, decimal_places=2)
image = models.URLField(null=True, blank=True)
category = models.CharField(max_length=64, choices=CATEGORY, default=None)
It is not actually necessary to try to get the category object from the model. This code worked for me.
category.views
def categories(request):
return render(request, "auctions/categories.html")
categories.html
<a href="{% url 'categories_page' 'Miscellaneous' %}" style = "color: rgb(58, 58, 58);"><h5>Miscellaneous</h5></a>
<a href="{% url 'categories_page' 'Movies and Television' %}" style = "color: rgb(58, 58, 58);"><h5>Movies and Television</h5></a>
<a href="{% url 'categories_page' 'Sports' %}" style = "color: rgb(58, 58, 58);"><h5>Sports</h5></a>
<a href="{% url 'categories_page' 'Arts and Crafts' %}" style = "color: rgb(58, 58, 58);"><h5>Arts and Crafts</h5></a>
<a href="{% url 'categories_page' 'Clothing' %}" style = "color: rgb(58, 58, 58);"><h5>Clothing</h5></a>
<a href="{% url 'categories_page' 'Books' %}" style = "color: rgb(58, 58, 58);"><h5>Books</h5></a>
All you have to do is put the name of the url then the name of the argument you want.