Django - 外部 URL Link

Django - External URL Link

我是 Python 和 Django 的新手,我刚刚创建了一个网站。 基本上我想在网站上有一个页面来显示我们公司的合作伙伴。 我创建了一个应用程序 'partners',在模型中,我将 'website' 作为字段之一。

在合作伙伴 html 文件中,我有一个按钮,用户可以单击该按钮,这会将他们带到新选项卡中的合作伙伴网站。

我尝试通过以下方式 link 该网站: {{ partner.website }} 然而,这最终是这样的: www.mydomain.com/partners/www.partnerwebsite.com

我只想在新标签页中打开合作伙伴网站 (www.partnerwebsite.com)。

感谢任何帮助。如果已经有另一个post,请重定向我。

views.py

from django.shortcuts import render, redirect
from .models import Partner

def index(request):
    partners = Partner.objects.order_by('-date_added').filter(is_published=True)

     context = {
    'partners': partners,
}

     return render(request, 'partners/partners.html', context)

models.py

from django.db import models
from datetime import datetime

class Partner(models.Model):
    name = models.CharField(max_length=200)
    description = models.TextField()
    website = models.CharField(max_length=100)
    email = models.CharField(max_length=200)
    contact_person = models.CharField(max_length=200)
    phone = models.CharField(max_length=100)
    mobile = models.CharField(max_length=100)
    address = models.CharField(max_length=200)
    photo_main = models.ImageField(upload_to='photos/partners/%Y/%m/%d/')
    promo_code = models.CharField(max_length=20, blank=True)
    is_published = models.BooleanField(default=True)
    date_added = models.DateTimeField(default=datetime.now, blank=True)
    def __str__(self):
        return self.name

partners.html

{% extends 'base.html' %}

{% block title %} | Partners {% endblock %}

{% block content %}

<section id="showcase-inner" class="py-5 text-white">
     <div class="container">
       <div class="row text-center">
         <div class="col-md-12">
           <h1 class="display-4">Partners</h1>
        </div>
      </div>
    </div>
  </section>

  <!-- Breadcrumb -->
  <section id="bc" class="mt-3">
    <div class="container">
      <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
          <li class="breadcrumb-item">
            <a href="{% url 'index' %}">
              <i class="fas fa-home"></i> Home</a>
          </li>
          <li class="breadcrumb-item active"> Partners</li>
        </ol>
      </nav>
    </div>
  </section>

<!-- Partners -->
<section id="partners" class="py-4">
    <div class="container">
      <div class="row">
            {% if partners %}
                {% for partner in partners %}

                <!-- Partner 1 -->
                <div class="col-md-6 col-lg-6 mb-4">
                    <div class="card listing-preview">
                    <img class="card-img-top-project" src="{{ partner.photo_main.url }}" alt="">
                        <div class="card-body">
                            <div class="listing-heading text-center">
                                <h4 class="text-primary">{{ partner.name | title }}</h4>
                                <p><i class="fas fa-map-marker text-secondary"></i> &ensp; {{ partner.address }}</p>
                                </div>
                            <hr>
                            <div class="row py-2 text-secondary">
                                <div class="col-12 text-center">
                                    {{ partner.description }}
                                </div>   
                            </div>    
                            {% if partner.promo_code %}
                            <hr>
                            <div class="row py-2 text">
                                <div class="col-12 text-center">
                                    Use the following code to obtain 10% discount: {{ partner.promo_code }}
                                </div>   
                            </div>   
                            {% endif %}                          
                            <div class="container">
                            <hr>
                                <a href="{{ partner.website }}" target="_blank"><button class="btn btn-secondary btn-block">Visit Website</button></a> 
                            </div> 
                        </div>
                    </div>
                </div>
            {% endfor %}
        {% else %}
            <div class="container">
                <p><h5 class="text-center">No Partners Available</h5></p> 
            </div>
        {% endif %}

      </div>
    </div>
  </section>

{% endblock %}

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('',views.index, name='partners'),
]

我能够从您的视图文件中推断出以下解决方案。在您的模板中添加以下代码 "partners.html"

{% for value in partners %}

<a href="https://{{ value.website }}" target="_blank"><button>my button </button></a>

{% endfor%}