Shawn
Shawn

Reputation: 163

Django: Problem with Adding Clickable Link in the Second Template

I am a beginner in Django. I am building a Django app, named PhoneReview. It will store reviews related to the latest mobile phone. It will also display phone brands, along with the associated phone models.

I have already created models and views. I have also managed to add clickable link in the first template (brandlist.html). In the first template, when you click on the brand name, like Samsung, you will be taken to the page of the phone model, like Galaxy S10. Here is the screenshot of the first template: enter image description here

When you click the link, you will be taken to the second template (phonemodel.html). But now, I am facing an issue. There is no clickable link on the phone model ("Galaxy S10") that will direct you to details.html. Here is the screenshot. enter image description here

Here are the codes of models.py inside the "PhoneReview" folder:

from django.db import models
from django.template.defaultfilters import slugify

# Create your models here.
class Brand(models.Model):
    brand_name = models.CharField(max_length=100)
    origin = models.CharField(max_length=100)
    manufacturing_since = models.CharField(max_length=100, null=True, blank=True)

    def __str__(self):
        return self.brand_name

    def save(self, *args, **kwargs):
        self.slug = slugify(self.brand_name)
        super().save(*args, **kwargs)

class PhoneModel(models.Model):
    brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
    model_name = models.CharField(max_length=100)
    launch_date = models.CharField(max_length=100)
    platform = models.CharField(max_length=100)

    def __str__(self):
        return self.model_name

class Review(models.Model):
    phone_model = models.ManyToManyField(PhoneModel, related_name='reviews')
    review_article = models.TextField()
    date_published = models.DateField(auto_now=True)
    slug = models.SlugField(max_length=150, null=True, blank=True)

    def __str__(self):
        return self.review_article

Here are the codes of urls.py inside the "PhoneReview" folder:

from . import views
from django.urls import path

urlpatterns = [
    path('index', views.BrandListView.as_view(), name='brandlist'),
    path('phonemodel/<int:pk>/', views.ModelView.as_view(), name='modellist'),
    path('details/<int:pk>/', views.ReviewView.as_view(), name='details'),
]

Here are the codes of views.py inside the "PhoneReview" folder:

from django.views import generic
from .models import Brand, PhoneModel, Review


class BrandListView(generic.ListView):
    template_name = 'PhoneReview/brandlist.html'
    context_object_name = 'all_brands'

    def get_queryset(self):
        return Brand.objects.all()


class ModelView(generic.DetailView):
    model = PhoneModel
    template_name = 'PhoneReview/phonemodel.html'

class ReviewView(generic.DetailView):
    model = Review
    template_name = 'PhoneReview/details.html'

Here are the codes of apps.py inside the "PhoneReview" folder:

from django.apps import AppConfig


class PhonereviewConfig(AppConfig):
    name = 'PhoneReview'

Here are the codes of details.html inside the "templates" folder:

{% extends 'PhoneReview/base.html' %}
{% load static %}

<html>

<link rel="stylesheet" type="text/css" href="{% static "css/style.css" %}">


<html lang="en">

{% block title%}Details{% endblock %}

{% block content %}

<h1>This is the Details Page</h1>

<h2>Review:</h2>
<p>{{ review.review_article }}</p>

<h2>News Link:</h2>
<p>{{ review.slug }}</p>
{% endblock %}
</html>

Here are the codes of phonemodel.html inside the "templates" folder:

{% extends 'PhoneReview/base.html' %}

{% load static %}

{% block title%}
Phone Model Page
{% endblock %}

{% block content %}
<!--Page content-->
<h1>This is Phone Model Page</h1>
<h2>Here is the phone model</h2>
    <ul>
        <li>{{ phonemodel.model_name }}</li>
    </ul>
<img src="{% static "images/brandlist.jpg" %}" alt="Super Mario Odyssey" /> <!-- New line -->
{% endblock %}

I tried replacing <li>{{ phonemodel.model_name }}</li> with <li><a href = "{% url 'details' brand.id %}">{{ phonemodel.model_name }}</a></li>. But I get an error, which looks like this:

NoReverseMatch at /phonemodel/1/
Reverse for 'details' with arguments '('',)' not found. 1 pattern(s) tried: ['details/(?P<pk>[0-9]+)/$']

How can I fix the issue?

Upvotes: 0

Views: 45

Answers (1)

Selcuk
Selcuk

Reputation: 59315

There is no context variable named brand and you don't need it anyway. You should use the id of the phonemodel:

<li>
  <a href = "{% url 'details' phonemodel.id %}">
    {{ phonemodel.model_name }}
  </a>
</li>

Upvotes: 1

Related Questions