简体   繁体   中英

How do I make the link clickable in the rendered template in Django?

I am doing a Django project, named PhoneRadar. I have a template page ( details.html ), which looks like this: 在此处输入图像描述 As you can see, the link is not clickable. How can I make it clickable?

Here are my codes of models.py :

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)
    slug = models.SlugField(max_length=150, 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)
    slug = models.SlugField(max_length=150, null=True, blank=True)

    def __str__(self):
        return self.model_name

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

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)
    link = models.TextField(max_length=150, null=True, blank=True)


    def __str__(self):
        return self.review_article

Here are my codes of details.html located inside 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.link }}</p>
{% endblock %}
</html>

How can I fix this issue?

Because you use p but need a for clickable link. More info link .

<h2>News Link:</h2>
<a href={{ review.link }}>{{ review.link }}</a>

In HTML, paragraph tag doesn't directly support link, instead you should use the a tag as shown below

<a href={{ review.link }}>{{ review.link }}</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM