As you can see the photo above, I have displayed all "CATEGORY_CHOICES" (in forms of (Groceries, Groceries) in category.html.
views.py
def category(request):
context = {'CATEGORY_CHOICES': Category.CATEGORY_CHOICES}
return render(request, 'category.html', context)
def view_category(request, category_id):
category = Category.objects.get(category_id=category_id)
return render(request, 'view_category.html', {'category':category})
models.py
class Category(models.Model):
CATEGORY_CHOICES = [
('Groceries', 'Groceries'),
('Salary', 'Salary'),
('Bills', 'Bills'),
('Rent', 'Rent'),
('Gym', 'Gym'),
('Restaurant', 'Restaurant'),
('Vacation', 'Vacation'),
('Travel', 'Travel'),
('Gift', 'Gift'),
('Investments', 'Investments'),
('Savings', 'Savings'),
('Entertainment', 'Entertainment'),
('Internet', 'Internet'),
('Healthcare', 'Healthcare'),
('Lifestyle', 'Lifestyle'),
('Insurance', 'Insurance'),
('Other', 'Other'),
]
category_choices = models.CharField(max_length=50, blank=False, choices=CATEGORY_CHOICES)
budget = models.DecimalField(max_digits=10, decimal_places=2)
start_date = models.DateField(blank=False)
end_date = models.DateField(blank=False)
spending_limit = models.DecimalField(max_digits=10, decimal_places=2)
category.html
{% extends 'base_content.html' %}
{% block content %}
<div class="container">
<div class="row justify-content-center">
<div class="row" style="width:90%">
<div class="col-sm-4 text-center my-3">
<div class="card card-block" style="height : 300px">
<div class="card-body align-items-center d-flex justify-content-center">
<h5 class="card-title">Overall</h5>
<a href="" class="stretched-link"></a>
</div>
</div>
</div>
{% for item in CATEGORY_CHOICES %}
<div class="col-sm-4 text-center my-3">
<div class="card card-block" style="height : 300px">
<div class="card-body align-items-center d-flex justify-content-center">
<h5 class="card-title">{{ item.1 }}</h5>
<a href="{% url 'view_category' item ***this part is tricky for me*** %}" class="stretched-link"></a>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
</div>
{% endblock %}
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.home_page, name ='home_page'),
path('feed/',views.feed, name ='feed'),
path('sign_up/', views.SignUpView.as_view(), name='sign_up'),
path('log_in/', views.LogInView.as_view(), name='log_in'),
path('log_out/', views.log_out, name='log_out'),
path('profile/', views.ProfileUpdateView.as_view(), name='profile'),
path('new_transaction/',views.new_transaction, name ='new_transaction'),
path('category',views.category, name = 'category'),
path('category/view/<str:???***confused this part too***>/',views.view_category, name
= 'view_category'),
As you can see in the code, I'm trying to pass category name to make url to be category/view/'category_name'. However, this category name is not a field of Category model but from category_choices. I need to fetch the exact category_choices to pass into url. Two things are confusing to me. First, how am I going to extract category choice into url as an ID? Second is how am I going to configure the url for 'view_category'?
error message
NoReverseMatch at /category
Reverse for 'view_category' with arguments '(('Groceries', 'Groceries'),)' not found.
1 pattern(s) tried: ['category/view/(?P<category_id>[0-9]+)/\\Z']
Of course the error is found at
<a href="{% url 'view_category' item %}" class="stretched-link"></a>